Skip to content

Instantly share code, notes, and snippets.

@ahonymous
Last active December 15, 2015 09:05
Show Gist options
  • Save ahonymous/79c364e842be67b9e4ec to your computer and use it in GitHub Desktop.
Save ahonymous/79c364e842be67b9e4ec to your computer and use it in GitHub Desktop.
<?php
class ImageHelper
{
public function cropImage($content, $width, $height)
{
$image = new \Imagick();
$image->readImageBlob($content);
$image->setImageFormat('jpeg');
$imageRatio = $image->getImageWidth()/$image->getImageHeight();
$customRation = $width/$height;
if ($customRation < $imageRatio) {
$widthThumb = 0;
$heightThumb = $height;
} else {
$widthThumb = $width;
$heightThumb = 0;
}
$image->thumbnailImage($widthThumb, $heightThumb);
$image->cropImage(
$width,
$height,
($image->getImageWidth() - $width)/2,
($image->getImageHeight() - $height)/2
);
return $image->getImageBlob();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment