我可以使用 php 和 gd 检测动画 gif 吗?
我目前在使用 GD 调整图像大小时遇到一些问题。
一切工作正常,直到我想调整动画 gif 的大小,该动画在黑色背景上提供第一帧。
我尝试过使用 getimagesize
但这只给了我尺寸,没有任何东西可以区分任何 gif 和动画。
动画 GIF 不需要实际调整大小,只需跳过它们就足以满足我们的目的。
有什么线索吗?
附言。 我无法访问 imagemagick。
亲切的问候,
克里斯
I'm currently running into some issues resizing images using GD.
Everything works fine until i want to resize an animated gif, which delivers the first frame on a black background.
I've tried using getimagesize
but that only gives me dimensions and nothing to distinguish between just any gif and an animated one.
Actual resizing is not required for animated gifs, just being able to skip them would be enough for our purposes.
Any clues?
PS. I don't have access to imagemagick.
Kind regards,
Kris
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
动画 GIF 必须具有以下字符串
我已经在一些动画 gif 上进行了测试,并且该字符串似乎位于文件的 pos 781 处(使用 file_get_contents 和 strpos 找到)
Animated GIF must have the following string
I've tested on a few animated gifs and it seem the string is at the pos 781 of a file (found with file_get_contents and strpos)
在寻找同一问题的解决方案时,我注意到 php.net 站点有 Davide 和 Kris 所引用的代码的后续版本,但是,根据作者的说法,内存密集程度较低,并且可能磁盘密集程度较低。
我将在这里复制它,因为它可能会引起兴趣。
来源: http://www.php.net/manual/en/function .imagecreatefromgif.php#88005
While searching for a solution to the same problem I noticed that the php.net site has a follow-up to the code Davide and Kris are referring to, but, according to the author, less memory-intensive, and possibly less disk-intensive.
I'll replicate it here, because it may be of interest.
source: http://www.php.net/manual/en/function.imagecreatefromgif.php#88005
在
imagecreatefromgif()
函数的 PHP 手册页中有一段简短的代码片段,应该是您所需要的:imagecreatefromgif
评论 #59787 作者:ZeBadgerThere is a brief snippet of code in the PHP manual page of the
imagecreatefromgif()
function that should be what you need:imagecreatefromgif
comment #59787 by ZeBadger这是工作函数:
Here's the working function:
这是对当前投票最高的答案的改进,但我还没有足够的声誉来发表评论。 该答案的问题在于,它以 100Kb 块的形式读取文件,并且帧标记的结尾可能会被分成 2 个块。 解决这个问题的方法是将前一帧的最后 20b 添加到下一帧:
This is an improvement of the current top voted answer but I don't have enough reputation to comment yet. The problem with that answer is that it reads the file in 100Kb chunks and the end of frame marker might be split in between 2 chunks. A fix for that is to add the last 20b of the previous frame to the next one:
如果给定文件太大,则使用 file_get_contents 读取整个文件可能会占用太多内存。 我重构了之前给出的函数,该函数读取足够的字节来检查帧,并在找到至少 2 个帧后立即返回。
Reading whole file with
file_get_contents
may take too much memory if the given file is too large. I've re-factored the function previously given which reads just enough bytes to check frames and returns as soon as it finds at least 2 frames.