简体   繁体   English

我可以检测 PNG 文件是否从多页 PDF 文件转换而来,并使用 ImageMagick 将图像拆分为单独的文件吗?

[英]Can I detect if a PNG file was converted from a multiple page PDF file and split the image into separate files using ImageMagick?

I'm having a problem where a PNG file that was created using the PDF export feature in OS X's Preview will display like an animated GIF in some browsers (Safari,Firefox) or display only the first page (Chrome) if the converted PDF file has more than one page (By animated GIF I mean it flickers between the different pages).我遇到了一个问题,即使用 OS X 预览中的 PDF 导出功能创建的 PNG 文件将在某些浏览器(Safari、Firefox)中显示为动画 GIF,或者如果转换的 PDF 文件仅显示第一页(Chrome)有不止一页(动画 GIF 我的意思是它在不同页面之间闪烁)。 Is there a way using PHP to detect if an uploaded PNG file was converted this way, and then split it into separate images and stitch them together using ImageMagick?有没有办法使用 PHP 来检测上传的 PNG 文件是否以这种方式转换,然后将其拆分为单独的图像并使用 ImageMagick 将它们拼接在一起?

It is an animated PNG, or Mozilla APNG format file.它是一个动画 PNG 或 Mozilla APNG格式的文件。 You can detect it by running pngcheck like this:您可以通过像这样运行pngcheck来检测它:

pngcheck -v npma33-png-convert-test.png

Output输出

File: npma33-png-convert-test.png (981954 bytes)
  chunk IHDR at offset 0x0000c, length 13
    1275 x 1650 image, 32-bit RGB+alpha, non-interlaced
  chunk acTL at offset 0x00025, length 8                   <--- APNG
    unknown private, ancillary, unsafe-to-copy chunk
  chunk iCCP at offset 0x00039, length 281
    profile name = ICC Profile, compression method = 0 (deflate)
    compressed profile = 268 bytes
  chunk pHYs at offset 0x0015e, length 9: 5906x5906 pixels/meter (150 dpi)
  chunk fcTL at offset 0x00173, length 26                  <--- APNG
    unknown private, ancillary, unsafe-to-copy chunk
  chunk IDAT at offset 0x00199, length 16384
    zlib: deflated, 32K window, superfast compression
  chunk IDAT at offset 0x041a5, length 16384
  chunk IDAT at offset 0x081b1, length 16384
  chunk IDAT at offset 0x0c1bd, length 16384
  chunk IDAT at offset 0x101c9, length 16384
  chunk IDAT at offset 0x141d5, length 16384
  chunk IDAT at offset 0x181e1, length 16384
  chunk IDAT at offset 0x1c1ed, length 16384
  chunk IDAT at offset 0x201f9, length 16384
  chunk IDAT at offset 0x24205, length 16384
  chunk IDAT at offset 0x28211, length 16384
  chunk IDAT at offset 0x2c21d, length 16384
  chunk IDAT at offset 0x30229, length 16384
  chunk IDAT at offset 0x34235, length 16384
  chunk IDAT at offset 0x38241, length 16384
  chunk IDAT at offset 0x3c24d, length 16384
  chunk IDAT at offset 0x40259, length 16384
  chunk IDAT at offset 0x44265, length 16384
  chunk IDAT at offset 0x48271, length 16384
  chunk IDAT at offset 0x4c27d, length 16384
  chunk IDAT at offset 0x50289, length 16384
  chunk IDAT at offset 0x54295, length 16384
  chunk IDAT at offset 0x582a1, length 16384
  chunk IDAT at offset 0x5c2ad, length 16384
  chunk IDAT at offset 0x602b9, length 16384
  chunk IDAT at offset 0x642c5, length 15969
  chunk fcTL at offset 0x68132, length 26                 <--- APNG
    unknown private, ancillary, unsafe-to-copy chunk
  chunk fdAT at offset 0x68158, length 16388
    unknown private, ancillary, unsafe-to-copy chunk
  chunk fdAT at offset 0x6c168, length 16388
    unknown private, ancillary, unsafe-to-copy chunk
  chunk fdAT at offset 0x70178, length 16388
    unknown private, ancillary, unsafe-to-copy chunk
  chunk fdAT at offset 0x74188, length 16388
    unknown private, ancillary, unsafe-to-copy chunk
  chunk fdAT at offset 0x78198, length 16388
    unknown private, ancillary, unsafe-to-copy chunk
  chunk fdAT at offset 0x7c1a8, length 16388
    unknown private, ancillary, unsafe-to-copy chunk
  chunk fdAT at offset 0x801b8, length 16388
    unknown private, ancillary, unsafe-to-copy chunk
  chunk fdAT at offset 0x841c8, length 16388
    unknown private, ancillary, unsafe-to-copy chunk
  chunk fdAT at offset 0x881d8, length 16388
    unknown private, ancillary, unsafe-to-copy chunk
  chunk fdAT at offset 0x8c1e8, length 16388
    unknown private, ancillary, unsafe-to-copy chunk
  chunk fdAT at offset 0x901f8, length 16388
    unknown private, ancillary, unsafe-to-copy chunk
  chunk fdAT at offset 0x94208, length 16388
    unknown private, ancillary, unsafe-to-copy chunk
  chunk fdAT at offset 0x98218, length 16388
    unknown private, ancillary, unsafe-to-copy chunk
  chunk fdAT at offset 0x9c228, length 16388
    unknown private, ancillary, unsafe-to-copy chunk
  chunk fdAT at offset 0xa0238, length 16388
    unknown private, ancillary, unsafe-to-copy chunk
  chunk fdAT at offset 0xa4248, length 16388
    unknown private, ancillary, unsafe-to-copy chunk
  chunk fdAT at offset 0xa8258, length 16388
    unknown private, ancillary, unsafe-to-copy chunk
  chunk fdAT at offset 0xac268, length 16388
    unknown private, ancillary, unsafe-to-copy chunk
  chunk fdAT at offset 0xb0278, length 16388
    unknown private, ancillary, unsafe-to-copy chunk
  chunk fdAT at offset 0xb4288, length 16388
    unknown private, ancillary, unsafe-to-copy chunk
  chunk fdAT at offset 0xb8298, length 16388
    unknown private, ancillary, unsafe-to-copy chunk
  chunk fdAT at offset 0xbc2a8, length 16388
    unknown private, ancillary, unsafe-to-copy chunk
  chunk fdAT at offset 0xc02b8, length 16388
    unknown private, ancillary, unsafe-to-copy chunk
  chunk fdAT at offset 0xc42c8, length 16388
    unknown private, ancillary, unsafe-to-copy chunk
  chunk fdAT at offset 0xc82d8, length 16388
    unknown private, ancillary, unsafe-to-copy chunk
  chunk fdAT at offset 0xcc2e8, length 16388
    unknown private, ancillary, unsafe-to-copy chunk
  chunk fdAT at offset 0xd02f8, length 16388
    unknown private, ancillary, unsafe-to-copy chunk
  chunk fdAT at offset 0xd4308, length 16388
    unknown private, ancillary, unsafe-to-copy chunk
  chunk fdAT at offset 0xd8318, length 16388
    unknown private, ancillary, unsafe-to-copy chunk
  chunk fdAT at offset 0xdc328, length 16388
    unknown private, ancillary, unsafe-to-copy chunk
  chunk fdAT at offset 0xe0338, length 16388
    unknown private, ancillary, unsafe-to-copy chunk
  chunk fdAT at offset 0xe4348, length 16388
    unknown private, ancillary, unsafe-to-copy chunk
  chunk fdAT at offset 0xe8358, length 16388
    unknown private, ancillary, unsafe-to-copy chunk
  chunk fdAT at offset 0xec368, length 14406
    unknown private, ancillary, unsafe-to-copy chunk
  chunk IEND at offset 0xefbba, length 0

The giveaway that it is APNG is the presence of the acTL and fcTL chunks which are animation control and frame control .它是APNG的赠品是存在acTLfcTL块,它们是动画控制帧控制

You can suppress output of those chunks (which will stop the flickering) using the following command:您可以使用以下命令抑制这些块的输出(这将停止闪烁):

convert npma33-png-convert-test.png -define png:exclude-chunk=acTL result.png

but I have not done any testing on whether that will affect other things such as the compression achieved or how any other meta-data is affected - so please do your own checks.但我没有对这是否会影响其他事情进行任何测试,例如实现的压缩或任何其他元数据如何受到影响 - 所以请自行检查。

So, if you use bash you will end up with something like this:所以,如果你使用bash你最终会得到这样的结果:

file="npma33-png-convert-test.png"
if pngcheck -v "$file" | grep -q acTL; then 
   convert "$file" -define png:exclude-chunk=acTL "$file"
fi

The expected way to separate and rejoin the frames side-by-side would probably be并排分离和重新加入框架的预期方法可能是

convert npma.... -separate +append result.png

or或者

convert npma... -coalesce +append result.png

but ImageMagick seems reluctant to do either - I can only guess APNG is not really supported.但 ImageMagick 似乎也不愿意这样做 - 我只能猜测APNG并没有得到真正的支持。

I also tried using Photoshop-style layer addressing, but that doesn't work either:我也尝试过使用Photoshop 风格的图层寻址,但这也不起作用:

convert npma...[0] page1.png
convert npma...[1] page2.png

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何转换具体的没有。 使用imagemagick从.pdf文件到.png图像的页面 - How to convert specific no. of pages from a .pdf file to .png image using imagemagick 使用PHP ImageMagick库将pdf文件的前n页转换为单个png图片文件 - Convert the first n pages of a pdf file to a single png Image file using PHP ImageMagick library 如何使用Imagemagick将转换后的JPG图像从PDF保存到数据库 - How to save converted JPG image from PDF using Imagemagick to database 如何使用 Imagemagick PHP API 将 .pdf 文件转换为 .png - How to convert .pdf file to .png using Imagemagick PHP API 如何使用php imagemagick从pdf获取转换后的图像的名称? - How can i get the name of resulting converted images from a pdf using php imagemagick? 当svg文件已嵌入图像时,如何使用Imagemagick将svg图像转换为png图像? - How can i convert a svg image to png image with Imagemagick when the svg file has embedded images? ImageMagick-从SVG文件渲染PNG,内容不在“页面”上 - ImageMagick - render PNG from SVG file with content off the “page” 如何使用imagemagick合成具有2个区域的PNG文件? - How I Can Composite PNG file with 2 area with imagemagick? 如何检测调色板的PNG文件? - How can I detect a paletted PNG file? 使用imagemagick将pdf文件转换为图像 - convert pdf files to image using imagemagick
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM