0

我想使用 php 脚本调整 gif 文件的大小而不会丢失动画。我试过了,但动画在调整大小的 GIF 图像上失败了。

我试过这段代码

http://www.white-hat-web-design.co.uk/blog/resizing-images-with-php/

4

1 回答 1

0

我使用了这个功能:

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);
}
于 2017-05-09T12:19:46.533 回答