Skip to content

Instantly share code, notes, and snippets.

@webdawe
Created September 20, 2016 04:55
Show Gist options
  • Save webdawe/cf40a0ca8b1d92a6083666a494dd7afd to your computer and use it in GitHub Desktop.
Save webdawe/cf40a0ca8b1d92a6083666a494dd7afd to your computer and use it in GitHub Desktop.
Imagick Resize
function imagickResize($uploadDir, $fileFrom, $fileTo, $newWidth, $newHeight)
{
$thumb = new Imagick();
$thumb->readImage($uploadDir. $fileFrom);
$dimensions = $thumb->getImageGeometry();
$orginalWidth = $dimensions['width'];
$orginalHeight = $dimensions['height'];
$ratio = $orginalWidth/$orginalHeight;
if ($orginalWidth < $newWidth || $orginalHeight < $newHeight)
{
return '';
}
else if ($newWidth/$newHeight > $ratio)
{
$newWidth = $newHeight * $ratio;
}
else
{
$newHeight = $newWidth / $ratio;
}
$thumb->resizeImage($newWidth, $newHeight, Imagick::FILTER_LANCZOS,1);
$thumb->writeImage($uploadDir.$fileTo);
$thumb->clear();
$thumb->destroy();
return $fileTo;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment