使用ImageMagick调整动画gif的大小会产生40mb文件

时间:2014-04-08 08:45:58

标签: php image-processing imagemagick

我使用此代码调整300kb的动画gif大小,但生成的gif为40mb +。

$image = new \Imagick($sSourceFile);

// split
$image = $image->coalesceImages(); 

// resize each frame
foreach ($image as $key => $frame) {
    $frame->thumbnailImage(904, 678);
    $frame->setImagePage(904, 678, 0, 0);
}

// find areas that has changed between images
$image = $image->deconstructImages();

// save
$image->writeImages($sTargetFile, true);

我在阅读http://www.imagemagick.org/Usage/anim_opt/#frame_opt

之后尝试优化相框
$image = $image->optimizeImageLayers(); //instead of deconstructImages()

但这并没有帮助。

还有其他方法可以优化帧吗?

更新

我尝试在没有php插件的情况下修复它:

$command = "convert " . $sSourceFile . " -coalesce";
$command .= " -resize 904x678";
$command .= " -layers OptimizeFrame " .  $sTargetFile;
system($command);

这适用于50kb的小型GIF,但是在我的更大的那个上失败了这条消息:

PHP Warning: system(): Unable to fork [convert /var/www/my_folder/orig.gif -coalesce  -layers OptimizeFrame /var/www/my_folder/new.gif] in /var/www/my_folder/test.php on line 100

更新 - Glenn Randers-Pehrson的评论信息:

imagemagick identity命令在我的新旧GIF上给出了以下内容:

old.gif[0] GIF 904x678 904x678+0+0 8-bit sRGB 256c 321KB 0.010u 0:00.019
old.gif[1] GIF 2x3 904x678+516+430 8-bit sRGB 256c 321KB 0.040u 0:00.049
old.gif[2] GIF 4x5 904x678+515+429 8-bit sRGB 256c 321KB 0.040u 0:00.049
old.gif[3] GIF 6x7 904x678+514+428 8-bit sRGB 256c 321KB 0.040u 0:00.049
old.gif[4] GIF 8x9 904x678+513+427 8-bit sRGB 256c 321KB 0.040u 0:00.049
...
old.gif[480] GIF 236x45 904x678+288+552 8-bit sRGB 256c 321KB 0.030u 0:00.029
old.gif[481] GIF 232x45 904x678+288+552 8-bit sRGB 256c 321KB 0.030u 0:00.029
old.gif[482] GIF 1x1 904x678+0+0 8-bit sRGB 256c 321KB 0.030u 0:00.029
old.gif[483] GIF 1x1 904x678+0+0 8-bit sRGB 256c 321KB 0.030u 0:00.029
old.gif[484] GIF 1x1 904x678+0+0 8-bit sRGB 256c 321KB 0.030u 0:00.029

new.gif[0] GIF 904x678 904x678+0+0 8-bit sRGB 64c 48.27MB 0.030u 0:00.040
new.gif[1] GIF 904x678 904x678+0+0 8-bit sRGB 64c 48.27MB 0.060u 0:00.060
new.gif[2] GIF 904x678 904x678+0+0 8-bit sRGB 64c 48.27MB 0.060u 0:00.060
new.gif[3] GIF 904x678 904x678+0+0 8-bit sRGB 64c 48.27MB 0.060u 0:00.060
new.gif[4] GIF 904x678 904x678+0+0 8-bit sRGB 128c 48.27MB 0.060u 0:00.060
...
new.gif[480] GIF 904x678 904x678+0+0 8-bit sRGB 256c 48.27MB 0.030u 0:00.039
new.gif[481] GIF 904x678 904x678+0+0 8-bit sRGB 256c 48.27MB 0.030u 0:00.039
new.gif[482] GIF 904x678 904x678+0+0 8-bit sRGB 256c 48.27MB 0.030u 0:00.039
new.gif[483] GIF 904x678 904x678+0+0 8-bit sRGB 256c 48.27MB 0.030u 0:00.039
new.gif[484] GIF 904x678 904x678+0+0 8-bit sRGB 256c 48.27MB 0.030u 0:00.039

使用ImageMagick 6.8.7-7 Q16 x86_64 2013-11-27。

在识别中添加--verbose会给我:

Image: new.gif
  Format: GIF (CompuServe graphics interchange format)
  Mime type: image/gif
  Class: PseudoClass
  Geometry: 904x678+0+0
  Units: Undefined
  Type: Palette
  Endianess: Undefined
  Colorspace: sRGB
  Depth: 8-bit
  Channel depth:
    red: 8-bit
    green: 8-bit
    blue: 8-bit
  Channel statistics:
    Red:
      min: 199 (0.780392)      max: 250 (0.980392)      mean: 236.67 (0.928118)      standard deviation: 10.9591 (0.0429769)      kurtosis: 0.887823      skewness: -1.55982    
    Green:
      min: 199 (0.780392)      max: 250 (0.980392)      mean: 236.67 (0.928118)      standard deviation: 10.9591 (0.0429769)      kurtosis: 0.887823      skewness: -1.55982    
    Blue:
      min: 199 (0.780392)      max: 249 (0.976471)      mean: 236.647 (0.928027)      standard deviation: 10.9867 (0.0430852)      kurtosis: 0.857301      skewness: -1.5512  Image statistics:
    Overall:
      min: 199 (0.780392)      max: 250 (0.980392)      mean: 236.662 (0.928087)      standard deviation: 10.9683 (0.0430131)      kurtosis: 0.877615      skewness: -1.55695  Colors: 34
  Histogram:
         4: (199,199,199) #C7C7C7 grey78
       272: (203,203,203) #CBCBCB srgb(203,203,203)
     ... [long list removed by Tieme]
       199: (249,249,249) #F9F9F9 srgb(249,249,249)
        29: (250,250,246) #FAFAF6 srgb(250,250,246)
  Colormap entries: 64
  Colormap:
         0: (199,199,199) #C7C7C7 grey78
         1: (201,201,201) #C9C9C9 grey79
     ... [long list removed by Tieme]
        62: (  0,  0,  0) #000000 black
        63: (  0,  0,  0) #000000 black
  Rendering intent: Perceptual
  Gamma: 0.454545
  Chromaticity:
    red primary: (0.64,0.33)
    green primary: (0.3,0.6)
    blue primary: (0.15,0.06)
    white point: (0.3127,0.329)
  Background color: grey78
  Border color: srgb(223,223,223)
  Matte color: grey74
  Transparent color: black
  Interlace: None
  Intensity: Undefined
  Compose: Over
  Page geometry: 904x678+0+0
  Dispose: None
  Delay: 5x100
  Iterations: 0
  Scene: 0 of 485
  Compression: LZW
  Orientation: Undefined
  Properties:
    date:create: 2014-04-09T21:12:21+02:00
    date:modify: 2014-04-09T21:12:21+02:00
    signature: 6d2f4f19412c6187c28ee6ef8e71e7c59e166ab7bf151bb1ae2ab3701aff425c
  Artifacts:
    filename: new.gif
    verbose: true
  Tainted: False
  Filesize: 48.27MB
  Number pixels: 613K
  Pixels per second: 113KB
  User time: 5.370u
  Elapsed time: 0:06.430
  Version: ImageMagick 6.8.7-7 Q16 x86_64 2013-11-27 http://www.imagemagick.org

对于old.gif:

Image: old.gif
  Format: GIF (CompuServe graphics interchange format)
  Mime type: image/gif
  Class: PseudoClass
  Geometry: 904x678+0+0
  Units: Undefined
  Type: PaletteAlpha
  Endianess: Undefined
  Colorspace: sRGB
  Depth: 8-bit
  Channel depth:
    red: 8-bit
    green: 8-bit
    blue: 8-bit
    alpha: 1-bit
  Channel statistics:
    Red:
      min: 199 (0.780392)      max: 251 (0.984314)      mean: 236.67 (0.928118)      standard deviation: 10.8629 (0.0425998)      kurtosis: 0.97992      skewness: -1.63289    
    Green:
      min: 199 (0.780392)      max: 251 (0.984314)      mean: 236.67 (0.928118)      standard deviation: 10.8629 (0.0425998)      kurtosis: 0.97992      skewness: -1.63289    
    Blue:
      min: 199 (0.780392)      max: 251 (0.984314)      mean: 236.647 (0.928027)      standard deviation: 10.9006 (0.0427473)      kurtosis: 0.938884      skewness: -1.62081    
    Alpha:
      min: 255 (1)      max: 255 (1)      mean: 255 (1)      standard deviation: 0 (0)      kurtosis: 0      skewness: 0  Image statistics:
    Overall:
      min: 0 (0)      max: 251 (0.984314)      mean: 177.497 (0.696066)      standard deviation: 9.41846 (0.0369351)      kurtosis: 32894      skewness: -1470.41  Colors: 50
  Histogram:
         4: (199,199,199,255) #C7C7C7 grey78
         5: (201,201,201,255) #C9C9C9 grey79
     ... [long list removed by Tieme]
         1: (250,250,246,255) #FAFAF6 srgba(250,250,246,1)
         1: (251,251,251,255) #FBFBFB srgba(251,251,251,1)
  Colormap entries: 256
  Colormap:
         0: (225,225,223,255) #E1E1DF srgba(225,225,223,1)
         1: (214, 72, 68,255) #D64844 srgba(214,72,68,1)
     ... [long list removed by Tieme]
       254: (242,242,242,255) #F2F2F2 grey95
       255: (255,255,255,  0) #FFFFFF00 srgba(255,255,255,0)
  Rendering intent: Perceptual
  Gamma: 0.454545
  Chromaticity:
    red primary: (0.64,0.33)
    green primary: (0.3,0.6)
    blue primary: (0.15,0.06)
    white point: (0.3127,0.329)
  Background color: srgba(255,255,255,0)
  Border color: srgba(223,223,223,1)
  Matte color: grey74
  Transparent color: srgba(255,255,255,0)
  Interlace: None
  Intensity: Undefined
  Compose: Over
  Page geometry: 904x678+0+0
  Dispose: None
  Delay: 5x100
  Iterations: 0
  Scene: 0 of 485
  Compression: LZW
  Orientation: Undefined
  Properties:
    date:create: 2014-04-10T09:35:49+02:00
    date:modify: 2014-04-03T11:42:15+02:00
    signature: 5edcc0c3235de10a527f91b246054e2b15d17d16287ddc74001be7f7e9381c0a
  Artifacts:
    filename: old.gif
    verbose: true
  Tainted: False
  Filesize: 321KB
  Number pixels: 613K
  Pixels per second: 5.108MB
  User time: 0.080u
  Elapsed time: 0:01.120
  Version: ImageMagick 6.8.7-7 Q16 x86_64 2013-11-27 http://www.imagemagick.org

0 个答案:

没有答案