有没有办法用 PHP 或 Java 检查 GIF 图像是否有动画?

发布于 2024-12-25 02:49:12 字数 94 浏览 6 评论 0原文

您知道 GIF 文件支持动画,但 GIF 图像不一定必须有动画。

有没有办法使用 php 或 java 检查 GIF 图像是否有动画?

谢谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

猫烠⑼条掵仅有一顆心 2025-01-01 02:49:12

这里有一个 PHP 脚本,应该能够确定图像是否是 gif 动画。我已经测试过它并且它对我有用。

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

只需将 $img 变量编辑为您想要测试的任何图像(例如 image.gif、image.jpg)。

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

·深蓝 2025-01-01 02:49:12

imagecreatefromgif() 函数的 php 手册页中有一段简短的代码片段,它应该是您所需要的:

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

如果您需要,可以很容易地对其进行修改以计算帧数。

参见此处

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

不如归去 2025-01-01 02:49:12

尝试这样的事情:

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

如果它返回 1,则它不是动画的。

我会谨慎地在不依赖像 Imagick 这样的库的情况下编写函数,因为像 这个

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文