diff --git a/README.md b/README.md index b097901..6ba0650 100644 --- a/README.md +++ b/README.md @@ -35,6 +35,7 @@ Add types. The type contains the key, and the image's size. $image->setType('large', 640, 480); $image->setType('medium', 320, 240); $image->setType('thumbnail', 160, 120); +$image->setType('content', 500, null); // You can add 'null' as height. It won't be cropped. ``` Add the source file. diff --git a/src/ImageResize.php b/src/ImageResize.php index b968734..be84b51 100644 --- a/src/ImageResize.php +++ b/src/ImageResize.php @@ -403,56 +403,74 @@ public function resize() $imageRotation = 'square'; } - switch ($imageRotation) { - case 'landscape': - $width = \round($imageWidth/($imageHeight/$imageType['sizeHeight'])); - $height = $imageType['sizeHeight']; + if ($imageType['height'] == null) { - if ($width < $imageType['sizeWidth']) { - $width = $imageType['sizeWidth']; - $height = \round($imageHeight/($imageWidth/$imageType['sizeWidth'])); - } + $width = $imageType['width']; + $height = \round($imageHeight/($imageWidth/$imageType['width'])); - break; + $img->resizeImage( + $width, + $height, + \Imagick::FILTER_LANCZOS, + 0.9, + true + ); - case 'portrait': - $width = $imageType['sizeWidth']; - $height = \round($imageHeight/($imageWidth/$imageType['sizeWidth'])); + } else { - if ($height < $imageType['sizeHeight']) { + switch ($imageRotation) { + case 'landscape': $width = \round($imageWidth/($imageHeight/$imageType['sizeHeight'])); $height = $imageType['sizeHeight']; - } - break; + if ($width < $imageType['sizeWidth']) { + $width = $imageType['sizeWidth']; + $height = \round($imageHeight/($imageWidth/$imageType['sizeWidth'])); + } - case 'square': - $width = $imageType['sizeWidth']; - $height = \round($imageHeight/($imageWidth/$imageType['sizeWidth'])); + break; - if ($height < $imageType['sizeHeight']) { - $width = \round($imageWidth/($imageHeight/$imageType['sizeHeight'])); - $height = $imageType['sizeHeight']; - } + case 'portrait': + $width = $imageType['sizeWidth']; + $height = \round($imageHeight/($imageWidth/$imageType['sizeWidth'])); + + if ($height < $imageType['sizeHeight']) { + $width = \round($imageWidth/($imageHeight/$imageType['sizeHeight'])); + $height = $imageType['sizeHeight']; + } + + break; + + case 'square': + $width = $imageType['sizeWidth']; + $height = \round($imageHeight/($imageWidth/$imageType['sizeWidth'])); + + if ($height < $imageType['sizeHeight']) { + $width = \round($imageWidth/($imageHeight/$imageType['sizeHeight'])); + $height = $imageType['sizeHeight']; + } + + break; + } + + $img->resizeImage( + $width, + $height, + \Imagick::FILTER_LANCZOS, + 0.9, + true + ); + + // Crop the image + $img->cropImage( + $imageType['sizeWidth'], + $imageType['sizeHeight'], + 0, + 0 + ); - break; } - $img->resizeImage( - $width, - $height, - \Imagick::FILTER_LANCZOS, - 0.9, - true - ); - - // Crop the image - $img->cropImage( - $imageType['sizeWidth'], - $imageType['sizeHeight'], - 0, - 0 - ); // Sharpen image $img->adaptiveSharpenImage(2, 1);