From fe9dc8e90103e52af42dad5ae4ac533ef1c431f8 Mon Sep 17 00:00:00 2001 From: Mikael Nordfeldth Date: Wed, 4 Mar 2015 12:06:21 +0100 Subject: [PATCH] ImageMagickPlugin refactored so we can more easily add new formats --- plugins/ImageMagick/ImageMagickPlugin.php | 53 +++++++++++++---------- 1 file changed, 31 insertions(+), 22 deletions(-) diff --git a/plugins/ImageMagick/ImageMagickPlugin.php b/plugins/ImageMagick/ImageMagickPlugin.php index 20ea468c72..e6f2e22080 100644 --- a/plugins/ImageMagick/ImageMagickPlugin.php +++ b/plugins/ImageMagick/ImageMagickPlugin.php @@ -51,7 +51,7 @@ class ImageMagickPlugin extends Plugin * @param array $info The response from getimagesize() */ public function onFillImageFileMetadata(ImageFile $imagefile) { - if (is_null($imagefile->animated) && $imagefile->type === IMAGETYPE_GIF) { + if (is_null($imagefile->animated) && $imagefile->mimetype === 'image/gif') { $magick = new Imagick($imagefile->filepath); $magick = $magick->coalesceImages(); $imagefile->animated = $magick->getNumberImages()>1; @@ -60,31 +60,40 @@ class ImageMagickPlugin extends Plugin return true; } - public function onStartResizeImageFile(ImageFile $imagefile, $outpath, array $box) { - // So far we only take over the resize for IMAGETYPE_GIF - // (and only animated for gifs! (and only if we really want to resize the animation!)) - if ($imagefile->type == IMAGETYPE_GIF && $imagefile->animated && common_config('thumbnail', 'animated')) { - $magick = new Imagick($imagefile->filepath); - $magick = $magick->coalesceImages(); - $magick->setIteratorIndex(0); - do { - $magick->cropImage($box['w'], $box['h'], $box['x'], $box['y']); - $magick->thumbnailImage($box['width'], $box['height']); - $magick->setImagePage($box['width'], $box['height'], 0, 0); - } while ($magick->nextImage()); - $magick = $magick->deconstructImages(); - - // $magick->writeImages($outpath, true); did not work, had to use filehandle - // There's been bugs for writeImages in php5-imagick before, probably now too - $fh = fopen($outpath, 'w+'); - $success = $magick->writeImagesFile($fh); - fclose($fh); - - return !$success; + public function onStartResizeImageFile(ImageFile $imagefile, $outpath, array $box) + { + switch ($imagefile->mimetype) { + case 'image/gif': + // If GIF, then only for animated gifs! (and only if we really want to resize the animation!) + if ($imagefile->animated && common_config('thumbnail', 'animated')) { + return $this->resizeImageFileAnimatedGif($imagefile, $outpath, $box); + } + break; } return true; } + protected function resizeImageFileAnimatedGif(ImageFile $imagefile, $outpath, array $box) + { + $magick = new Imagick($imagefile->filepath); + $magick = $magick->coalesceImages(); + $magick->setIteratorIndex(0); + do { + $magick->cropImage($box['w'], $box['h'], $box['x'], $box['y']); + $magick->thumbnailImage($box['width'], $box['height']); + $magick->setImagePage($box['width'], $box['height'], 0, 0); + } while ($magick->nextImage()); + $magick = $magick->deconstructImages(); + + // $magick->writeImages($outpath, true); did not work, had to use filehandle + // There's been bugs for writeImages in php5-imagick before, probably now too + $fh = fopen($outpath, 'w+'); + $success = $magick->writeImagesFile($fh); + fclose($fh); + + return !$success; + } + public function onPluginVersion(&$versions) { $versions[] = array('name' => 'ImageMagick', -- 2.39.5