Skip to content

Instantly share code, notes, and snippets.

@justincarlson
Created March 14, 2019 01:37
Show Gist options
  • Save justincarlson/f972f7ce88b89e031a016e3520e96237 to your computer and use it in GitHub Desktop.
Save justincarlson/f972f7ce88b89e031a016e3520e96237 to your computer and use it in GitHub Desktop.
PHP Imagick example image scaling to minimize blur and defects.
<?php
function ScaleToJpg($src,$width,$height,$dest) {
$im = new \Imagick($src);
$im->setImageCompression(\Imagick::COMPRESSION_JPEG);
$im->setImageCompressionQuality(100);
$im->stripImage();
$im->setInterlaceScheme(\Imagick::INTERLACE_PLANE);
$size = $im->getImageGeometry();
if($size['width'] >= $size['height']){
if($size['width'] > $width){
$im->resizeImage($width, 0, \Imagick::FILTER_SINC, 1);
}
} else{
if($size['height'] > $height){
$im->resizeImage(0, $height, \Imagick::FILTER_SINC, 1);
}
}
$im->writeimage($dest);
$im->clear();
$im->destroy();
$im = null;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment