Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
491 views
in Technique[技术] by (71.8m points)

php - Can I programmatically determine if a PNG is animated?

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.

Bouncy ball

(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)?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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;
    }
?>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...