我可以以编程方式确定PNG是否具有动画效果吗? [英] Can I programmatically determine if a PNG is animated?

查看:297
本文介绍了我可以以编程方式确定PNG是否具有动画效果吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经将PNG(以及JPEG)图像上传到了我的网站.

I have PNG (as well as JPEG) images uploaded to my site.

它们应该是静态的(即一帧).

They should be static (i.e. one frame).

存在 APNG 之类的东西.

(它将在Firefox中显示为动画).

(it will be animated in Firefox).

根据维基百科文章 ...

APNG以不知道APNG的应用程序忽略它们的方式将后续帧隐藏在PNG辅助块中,但是否则格式没有任何更改,以允许软件区分动画图像和非动画图像.

APNG hides the subsequent frames in PNG ancillary chunks in such a way that APNG-unaware applications would ignore them, but there are otherwise no changes to the format to allow software to distinguish between animated and non-animated images.

这是否意味着无法确定PNG是否使用代码制作动画?

Does this mean it is impossible to determine if a PNG is animated with code?

如果可能的话,您能为我指出正确的PHP方向(GD,ImageMagick)吗?

If it is possible, can you please point me in the right direction PHP wise (GD, ImageMagick)?

推荐答案

对于不支持APNG图像的读者,APNG图像被设计为伪装"为PNG.也就是说,如果阅读器不支持它们,它将仅假定它是普通的PNG文件,并且仅显示第一帧.这意味着它们具有与PNG(image/png)相同的MIME类型,具有相同的幻数(89 50 4e 47 0d 0a 1a 0a),并且通常以相同的扩展名保存(尽管这并不是检查的好方法)文件类型).

APNG images are designed to be "camouflaged" as PNG for readers that not support them. That is, if a reader does not support them, it will just assume it is a normal PNG file and display only the first frame. That means that they have the same MIME type as PNG (image/png), they have the same magic number (89 50 4e 47 0d 0a 1a 0a) and generally they're saved with the same extension (although that is not really a good way to check for a file type).

那么,您如何区分它们? APNG中有一个"acTL"块.因此,如果您搜索字符串acTL(或以十六进制表示61 63 54 4C)(大块标记(即00 00 00 08)之前的4个字节,则是大字节序格式的大块的大小,而不计算大小),标记,或字段末尾的CRC32)),您应该还不错.为了使它更好,请检查该块是否在"IDAT"块的第一次出现之前出现(只需查找IDAT).

So, how do you distinguish them? APNG have a "acTL" chunk in them. So, if you search for the string acTL (or, in hex, 61 63 54 4C (the 4 bytes before the chunk marker (i.e. 00 00 00 08) are the size of the chunk in big endian format, without counting the size, marker, or CRC32 at the end of the field)) you should be pretty good. To get it even better, check that this chunk appears before the first occurrence of the "IDAT" chunk (just look for IDAT).

此代码(摘自 http://foone.org/apng/identify_apng.php)就能解决问题:

This code (taken from http://foone.org/apng/identify_apng.php ) will do the trick:

<?php
# Identifies APNGs
# Written by Coda, functionified by Foone/Popcorn Mariachi#!9i78bPeIxI
# This code is in the public domain
# identify_apng returns:
# true if the file is an APNG
# false if it is any other sort of file (it is not checked for PNG validity)
# takes on argument, a filename.
function identify_apng($filename)
    {
    $img_bytes = file_get_contents($filename);
    if ($img_bytes)
        {
        if(strpos(substr($img_bytes, 0, strpos($img_bytes, 'IDAT')), 
                 'acTL')!==false)
            {
        return true;
        }
        }
    return false;
    }
?>

这篇关于我可以以编程方式确定PNG是否具有动画效果吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆