크로스 플랫폼 이미지 파일 크기, 동일합니까?

크로스 플랫폼 이미지 파일 크기, 동일합니까?

내 응용 프로그램은 *.png 이미지 파일의 정확한 파일 크기(바이트)를 알아야 합니다. 이는 Linux에서 예상대로 정확하게 작동합니다.

% !TeX TS-program = lualatex
% !TeX encoding = UTF-8
\documentclass{article}
\usepackage{fontspec}
\setmainfont{Latin Modern Roman}
\usepackage{mwe}
\usepackage{graphics}
\gdef\getfilesize#1{%
  \directlua{
    local filename = ("\luaescapestring{#1}")
    local foundfile = kpse.find_file(filename, "tex", true)
    if foundfile then
      local size = lfs.attributes(foundfile, "size")
      if size then
        tex.write(size)
      end
    end
  }%
}
\begin{document}
\includegraphics{example-image-4x3.png}\par
Above file size: \getfilesize{example-image-4x3.png}\par
\end{document}

파일 크기가 1247로 보고되는데 이는 정확합니다. 아쉽게도 다른 운영 체제(Windows, Mac)에 액세스할 수 없습니다. 이것이 거기에서도 작동할까요? 보고된 값이 다른 시스템에서도 동일합니까?

편집: 내 말은: 이 질문을 보고 Windows(MiKtex)를 사용하는 사람들 중에서 위 코드가 Linux에서 얻은 것과 동일한 숫자를 보고합니까? PDF로 인쇄되어 있습니다. 똑같아야 하지만 확실하게 알고 싶습니다.

이 작업을 수행하는 이유: 이미지 사용이 제한되어 있습니다. 이미지가 "누군가에 의해 편집된 것"이 아니라 "이전에 승인된 것과 동일"한지 확인하기 위해 파일 크기와 같은 몇 가지 검사가 필요합니다.

답변1

다음에 따라 시스템 호출을 사용 lfs.attributes()합니다 .stat()그 문서Linux, macOS, Windows 및 Windows의 파일에 대해 동일한 결과를 생성해야 합니다.POSIX 표준에 따라크기는 파일의 실제 크기입니다(심볼릭 링크가 해결된 후). 따라서 해당 방법을 사용하여 파일 크기를 얻는 것이 안전합니다. 검색을 사용하여 파일 크기를 얻기 위해 lua에서 사용되는 일반적인 방법을 사용할 수 있을 만큼 안전하지 않다고 생각되면 내가 게시한 다른 답변을 참조하세요.

답변2

일반적인 상황에서는 lfs.attributes()크로스 플랫폼 작업을 신뢰할 수 있습니다. 느리지만 작동이 보장되는 대안:

\documentclass{article}
\usepackage{fontspec}
\setmainfont{Latin Modern Roman}
%\usepackage{mwe}
\usepackage{graphics}
\gdef\getfilesize#1{%
  \directlua{
    function fsize (file)
      local h = io.open(file)
      local size = h:seek("end")
      assert(h:close())
      return size
    end
    local filename = ("\luaescapestring{#1}")
    local foundfile = kpse.find_file(filename, "tex", true)
    if foundfile then
      local size = fsize(foundfile)
      if size then
        tex.write(size)
      end
    end
  }%
}
\begin{document}
\includegraphics{example-image-4x3.png}\par
Above file size: \getfilesize{example-image-4x3.png}\par
\end{document}

다음에서 수정됨: https://www.lua.org/pil/21.3.html

관련 정보