Is there a way to check if a GIF image has animation with PHP or Java?

六眼飞鱼酱① 提交于 2020-03-21 20:05:11

问题


You know that GIF files support animation, but a GIF image not necesarily must have an animation on it.

Is there a way to check if a GIF image has an animation using php or java?

Thanks.


回答1:


Here is a little PHP script that should be able to determine if an image is an animated gif or not. I have tested it and it works for me.

<?php
$img="your_image";
$file = file_get_contents($img);
$animated=preg_match('#(\x00\x21\xF9\x04.{4}\x00\x2C.*){2,}#s', $file);
if ($animated==1){
    echo "This image is an animated gif";
} else {
    echo "This image is not an animated gif";
}
?>

Simply edit the $img variable to whatever image you want to test (e.g. image.gif, image.jpg).




回答2:


There is a brief snippet of code in the php manual page of the imagecreatefromgif() functions the should be what you need:

<?php

    function is_ani($filename)
    {
            $filecontents=file_get_contents($filename);

            $str_loc=0;
            $count=0;
            while ($count < 2) # There is no point in continuing after we find a 2nd frame
            {

                    $where1=strpos($filecontents,"\x00\x21\xF9\x04",$str_loc);
                    if ($where1 === FALSE)
                    {
                            break;
                    }
                    else
                    {
                            $str_loc=$where1+1;
                            $where2=strpos($filecontents,"\x00\x2C",$str_loc);
                            if ($where2 === FALSE)
                            {
                                    break;
                            }
                            else
                            {
                                    if ($where1+8 == $where2)
                                    {
                                            $count++;
                                    }
                                    $str_loc=$where2+1;
                            }
                    }
            }

            if ($count > 1)
            {
                    return(true);

            }
            else
            {
                    return(false);
            }
    }

    exec("ls *gif" ,$allfiles);
    foreach ($allfiles as $thisfile)
    {
            if (is_ani($thisfile))
            {
                    echo "$thisfile is animated<BR>\n";
            }
            else
            {
                    echo "$thisfile is NOT animated<BR>\n";
            }
    }
    ?>

It could quite easily be modified to count the number of frames if you required.

See Here




回答3:


Try something like this:

public function getNumFramesFromGif(string $url): int{
    $image = file_get_contents($url);
    $imagick = new \Imagick();
    $imagick->readImageBlob($image);
    $numFrames = $imagick->identifyFormat("%n"); //https://www.php.net/manual/en/imagick.identifyformat.php https://davidwalsh.name/detect-gif-animated
    return $numFrames;
}

If it returns 1, it's not animated.

I'd be wary of coding a function without relying on a library like Imagick because of "gotchas" like this.



来源:https://stackoverflow.com/questions/8763647/is-there-a-way-to-check-if-a-gif-image-has-animation-with-php-or-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!