Skip to content

Instantly share code, notes, and snippets.

@pschfr
Last active September 7, 2018 04:27
Show Gist options
  • Save pschfr/97854bffabccf340575d0abf191c5a97 to your computer and use it in GitHub Desktop.
Save pschfr/97854bffabccf340575d0abf191c5a97 to your computer and use it in GitHub Desktop.
[ImageMagick Resize] #PHP #Imagemagick
<?php
try {
// a new imagick object
$im = new Imagick($imagePath);
// Resizes using the Lanczos resampling algorithm to whichever is larger, width or height
if($im->getImageHeight() <= $im->getImageWidth()) {
// width is larger, resize if larger than max
if ($im->getImageWidth() >= 800) {
$im->resizeImage($width, 0, Imagick::FILTER_LANCZOS, 1);
}
} else {
// height is larger, resize if larger than max
if ($im->getImageHeight() >= 800) {
$im->resizeImage(0, $height, Imagick::FILTER_LANCZOS, 1);
}
}
// Set to use jpeg compression
$im->setImageCompression(Imagick::COMPRESSION_JPEG);
// Set compression level (1 lowest quality, 100 highest quality)
$im->setImageCompressionQuality(75);
// Strip out unneeded meta data
$im->stripImage();
// Write the thumbnail to disk
$im->writeImage();
// Free resources associated with the Imagick object
$im->destroy();
// return
return $imagePath;
} catch(Exception $e) {
error_log($e);
return $imagePath;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment