如何在不丢失动画的情况下使用标准 php 函数调整 gif 大小

How to resize gif with standard php functions without losing the animation

我有一个图片上传PHP脚本,可以很好地处理png、jpg和jpeg图像。问题是,由于我使用 imagealphablendingimagesavealphaimagecreatetruecolorimagecopyresampled 的组合来调整图片大小,gif 动画丢失了。我正在使用 imagecreatefromgif 将 gif 加载到内存中。

我知道可以使用 ImageMagick 来达到预期的效果,但我想知道是否有一种解决方案结合了标准 php 函数来调整 gif 的大小而不丢失动画。我有这样的解决方案,还是应该开始使用库?

可以使用 GD(您称之为 "standard PHP")调整动画 GIF 的大小。

您需要将图像拆分为单独的帧,调整它们的大小,然后将所有内容重新组合在一起。

可以看到更详细的解释here

不过,我真的建议您使用 imagick,因为它易于安装,而且更易于用于此类高级任务。

编辑 这是我在上面链接的答案中的相关部分:

If you don't have ImageMagick access, you should be able to use a combination of the following steps to resize an animated gif (assuming you have GD access):

  1. Detect if the image is an animated gif: (top answer)
  2. Split the animated gif into individual frames: [http://www.phpclasses.org/package/3234-PHP-Split-GIF-animations-into-multiple-images.html][2]
  3. Resize the individual frames: [http://www.akemapa.com/2008/07/10/php-gd-resize-transparent-image-png-gif/][3]
  4. Recomposite the frames into an animated gif again: [http://www.phpclasses.org/package/3163-PHP-Generate-GIF-animations-from-a-set-of-GIF-images.html][4]