Last active
September 23, 2023 17:27
-
-
Save jmheretik/4e2441b898e7be1aa664e890bcc19274 to your computer and use it in GitHub Desktop.
Kirby 2's ImageMagick thumb driver implementation rewritten using Imagick API instead of shell exec commands.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* ImageMagick Driver | |
*/ | |
thumb::$drivers['im-api'] = function($thumb) { | |
try { | |
$img = new Imagick($thumb->root()); | |
$img = $img->coalesceImages(); | |
do { | |
$img->stripImage(); | |
if($thumb->options['interlace']) { | |
$img->setInterlaceScheme(Imagick::INTERLACE_PLANE); | |
} | |
if($thumb->options['grayscale']) { | |
$frame->setImageColorspace(Imagick::COLORSPACE_GRAY); | |
} | |
$img->setImageCompressionQuality($thumb->options['quality']); | |
if($thumb->options['blur']) { | |
$img->blurImage(0, $thumb->options['blur']); | |
} | |
if($thumb->options['crop']) { | |
if(empty($thumb->options['height'])) { | |
$thumb->options['height'] = $thumb->options['width']; | |
} | |
$img->resizeImage($thumb->options['width'], $thumb->options['height'], Imagick::FILTER_LANCZOS, 1); | |
$img->setImageGravity(Imagick::GRAVITY_CENTER); | |
$img->cropImage($thumb->options['width'], $thumb->options['height'], 0, 0); | |
} else { | |
$dimensions = clone $thumb->source->dimensions(); | |
$dimensions->fitWidthAndHeight($thumb->options['width'], $thumb->options['height'], $thumb->options['upscale']); | |
$img->resizeImage($dimensions->width(), $dimensions->height(), Imagick::FILTER_LANCZOS, 1); | |
} | |
} while ($img->nextImage()); | |
$img = $img->deconstructImages(); | |
$img->writeImages($thumb->destination->root, true); | |
$img->clear(); | |
$img->destroy(); | |
} catch(Exception $e) { | |
$thumb->error = $e; | |
} | |
// OLD WAY USING SHELL EXEC | |
// $command = array(); | |
// $command[] = isset($thumb->options['bin']) ? $thumb->options['bin'] : 'convert'; | |
// $command[] = '"' . $thumb->source->root() . '"'; | |
// $command[] = '-strip'; | |
// if($thumb->options['interlace']) { | |
// $command[] = '-interlace line'; | |
// } | |
// if($thumb->source->extension() === 'gif') { | |
// $command[] = '-coalesce'; | |
// } | |
// if($thumb->options['grayscale']) { | |
// $command[] = '-colorspace gray'; | |
// } | |
// if($thumb->options['autoOrient']) { | |
// $command[] = '-auto-orient'; | |
// } | |
// $command[] = '-resize'; | |
// if($thumb->options['crop']) { | |
// if(empty($thumb->options['height'])) { | |
// $thumb->options['height'] = $thumb->options['width']; | |
// } | |
// $command[] = $thumb->options['width'] . 'x' . $thumb->options['height'] . '^'; | |
// $command[] = '-gravity Center -crop ' . $thumb->options['width'] . 'x' . $thumb->options['height'] . '+0+0'; | |
// } else { | |
// $dimensions = clone $thumb->source->dimensions(); | |
// $dimensions->fitWidthAndHeight($thumb->options['width'], $thumb->options['height'], $thumb->options['upscale']); | |
// $command[] = $dimensions->width() . 'x' . $dimensions->height() . '!'; | |
// } | |
// $command[] = '-quality ' . $thumb->options['quality']; | |
// if($thumb->options['blur']) { | |
// $command[] = '-blur 0x' . $thumb->options['blurpx']; | |
// } | |
// $command[] = '-limit thread 1'; | |
// $command[] = '"' . $thumb->destination->root . '"'; | |
// exec(implode(' ', $command)); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment