Last active
December 15, 2015 09:05
-
-
Save ahonymous/79c364e842be67b9e4ec to your computer and use it in GitHub Desktop.
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
<?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