ImageMagickPlugin refactored so we can more easily add new formats

This commit is contained in:
Mikael Nordfeldth 2015-03-04 12:06:21 +01:00
parent 284705eeb8
commit fe9dc8e901

View File

@ -51,7 +51,7 @@ class ImageMagickPlugin extends Plugin
* @param array $info The response from getimagesize() * @param array $info The response from getimagesize()
*/ */
public function onFillImageFileMetadata(ImageFile $imagefile) { 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 = new Imagick($imagefile->filepath);
$magick = $magick->coalesceImages(); $magick = $magick->coalesceImages();
$imagefile->animated = $magick->getNumberImages()>1; $imagefile->animated = $magick->getNumberImages()>1;
@ -60,31 +60,40 @@ class ImageMagickPlugin extends Plugin
return true; return true;
} }
public function onStartResizeImageFile(ImageFile $imagefile, $outpath, array $box) { 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!)) switch ($imagefile->mimetype) {
if ($imagefile->type == IMAGETYPE_GIF && $imagefile->animated && common_config('thumbnail', 'animated')) { case 'image/gif':
$magick = new Imagick($imagefile->filepath); // If GIF, then only for animated gifs! (and only if we really want to resize the animation!)
$magick = $magick->coalesceImages(); if ($imagefile->animated && common_config('thumbnail', 'animated')) {
$magick->setIteratorIndex(0); return $this->resizeImageFileAnimatedGif($imagefile, $outpath, $box);
do { }
$magick->cropImage($box['w'], $box['h'], $box['x'], $box['y']); break;
$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;
} }
return true; 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) public function onPluginVersion(&$versions)
{ {
$versions[] = array('name' => 'ImageMagick', $versions[] = array('name' => 'ImageMagick',