我可以以编程方式确定PNG是否已设置动画? [英] Can I programatically determine if a PNG is animated?

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

问题描述

我上传到我网站的PNG(以及JPEG)图片。



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



有 ...


APNG以APNG的方式隐藏PNG辅助块中的后续帧-unaware应用程序会忽略它们,但是格式没有变化,软件可以区分动画和非动画图像。


这是否意味着无法确定PNG是否使用代码进行动画处理?



如果有可能,请指出我正确的方向PHP智慧( GD,ImageMagick)?

解决方案

APNG图像被设计为伪装为不支持它们的读者的PNG。也就是说,如果一个阅读器不支持它们,它只会假设它是一个普通的PNG文件而只显示第一帧。这意味着它们具有与PNG(image / png)相同的MIME类型,它们具有相同的幻数( 89 50 4e 47 0d 0a 1a 0a )并且通常它们'使用相同的扩展名保存(虽然这不是检查文件类型的好方法)。



那么,你如何区分它们?
APNG中有一个acTL块。所以,如果你搜索字符串 acTL (或者,十六进制, 61 63 54 4C (之前的4个字节)块标记(即 00 00 00 08 )是大端格式的块大小,不计算字段末尾的大小,标记或CRC32)你应该很好。为了更好地做到这一点,请检查此块在第一次出现IDAT块之前出现(只需查找 IDAT )。



此代码(取自 http://foone.org/apng/identify_apng.php )会做的伎俩:

 <?php 
#识别APNGs
#由Coda撰写,由Foone / Popcorn Mariachi提供功能#!9i78bPeIxI
#此代码在公共领域
#identify_apng返回:
#如果文件是APNG
,则为真# false,如果它是任何其他类型的文件(它没有检查PNG有效性)
#接受参数,文件名。
函数identify_apng($ filename)
{
$ img_bytes = file_get_contents($ filename);
if($ img_bytes)
{
if(strpos(substr($ img_bytes,0,strpos($ img_bytes,'IDAT')),
'acTL')!= = false)
{
返回true;
}
}
返回false;
}
?>


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

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

There is such thing as APNG.

(it will be animated in Firefox).

According to the Wikipedia article...

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.

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

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

解决方案

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).

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).

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天全站免登陆