简体   繁体   English

php ImageMagick gif resize保持动画

[英]php ImageMagick gif resize keep animation

After upload, the gif is resized but the animation is lost. 上传后,gif会调整大小,但动画会丢失。 What am I doing wrong? 我究竟做错了什么?

try
{
   $animation = new Imagick($this->image_filename);

   foreach ($animation as $frame)
   {
      $frame->thumbnailImage($width, $height);
      $frame->setImagePage($width, $height, 0, 0);
   }

   $animation->writeImages($this->image_filename, true);

   echo "<img src='".$this->image_filename."' />";

   $this->image = imagecreatefromgif($this->image_filename);
}
catch(Exception $e){ echo $e->getMessage(); }

No exception caught. 没有例外。

Array
(
    [versionNumber] => 1608
    [versionString] => ImageMagick 6.4.8 2011-03-20 Q16 OpenMP http://www.imagemagick.org
)

Try this: http://www.php.net/manual/en/imagick.coalesceimages.php 试试这个: http//www.php.net/manual/en/imagick.coalesceimages.php
The first comment seems like what u need. 第一条评论看起来像你需要的。 And u should never mix GD2 library ("imagecreatefromgif") and Imagick. 你永远不应该混合GD2库(“imagecreatefromgif”)和Imagick。

I used this function: 我用过这个函数:

function gifResize($file_origin,$file_dest,$percent){
   $percent = $percent*100;
   $crop_w = 0;
   $crop_h = 0;
   $crop_x = 0;
   $crop_y = 0;
   $image = new Imagick($file_origin);
   $originalWidth = $image->getImageWidth();
   $originalHeight = $image->getImageHeight();
   $size_w = ($originalWidth*$percent)/100;
   $size_h = ($originalHeight*$percent)/100;
   if(($size_w-$originalWidth)>($size_h-$originalHeight)){
       $s = $size_h/$originalHeight;
       $size_w = round($originalWidth*$s);
       $size_h = round($originalHeight*$s);
   }else{
       $s = $size_w/$originalWidth;
       $size_w = round($originalWidth*$s);
       $size_h = round($originalHeight*$s);
   }
   echo "$originalWidth $size_w - $originalHeight $size_h";
   $image = $image->coalesceImages();

   foreach ($image as $frame) {
       $frame->cropImage($crop_w, $crop_h, $crop_x, $crop_y);
       $frame->thumbnailImage($size_h, $size_w);
       $frame->setImagePage($size_h, $size_w, 0, 0);
   }
   $imageContent = $image->getImagesBlob();
   $fp = fopen($file_dest,'w');
   fwrite($fp,$imageContent);
   fclose($fp);

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM