From 3a08e8c22ca6ceaca21c1379879e7307ed801609 Mon Sep 17 00:00:00 2001 From: himiklab Date: Mon, 12 Nov 2018 14:42:06 +0300 Subject: [PATCH] new mode THUMBNAIL_INSET_BOX --- EasyThumbnailImage.php | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/EasyThumbnailImage.php b/EasyThumbnailImage.php index f4453a2..0aef2dd 100644 --- a/EasyThumbnailImage.php +++ b/EasyThumbnailImage.php @@ -7,6 +7,7 @@ namespace himiklab\thumbnail; +use Imagine\Image\Box; use Imagine\Image\ManipulatorInterface; use Yii; use yii\base\InvalidConfigException; @@ -24,6 +25,7 @@ class EasyThumbnailImage { const THUMBNAIL_OUTBOUND = ManipulatorInterface::THUMBNAIL_OUTBOUND; const THUMBNAIL_INSET = ManipulatorInterface::THUMBNAIL_INSET; + const THUMBNAIL_INSET_BOX = 'inset_box'; const QUALITY = 50; const MKDIR_MODE = 0755; @@ -59,6 +61,11 @@ class EasyThumbnailImage * [[Image::$thumbnailBackgroundColor]] or [[EasyThumbnail::$thumbnailBackgroundColor]], * and [[Image::$thumbnailBackgroundAlpha]] or [[EasyThumbnail::$thumbnailBackgroundAlpha]]. * + * If thumbnail mode is `self::THUMBNAIL_INSET_BOX`, the original image is scaled down so it is fully contained + * within the thumbnail dimensions. The specified $width and $height (supplied via $size) will be considered + * maximum limits. Unless the given dimensions are equal to the original image’s aspect ratio, one dimension in the + * resulting thumbnail will be smaller than the given limit. + * * @param string $filename the image file path or path alias or URL * @param integer $width the width in pixels to create the thumbnail * @param integer $height the height in pixels to create the thumbnail @@ -96,6 +103,11 @@ public static function thumbnail($filename, $width, $height, $mode = self::THUMB * [[Image::$thumbnailBackgroundColor]] or [[EasyThumbnail::$thumbnailBackgroundColor]], * and [[Image::$thumbnailBackgroundAlpha]] or [[EasyThumbnail::$thumbnailBackgroundAlpha]]. * + * If thumbnail mode is `self::THUMBNAIL_INSET_BOX`, the original image is scaled down so it is fully contained + * within the thumbnail dimensions. The specified $width and $height (supplied via $size) will be considered + * maximum limits. Unless the given dimensions are equal to the original image’s aspect ratio, one dimension in the + * resulting thumbnail will be smaller than the given limit. + * * @param string $filename the image file path or path alias or URL * @param integer $width the width in pixels to create the thumbnail * @param integer $height the height in pixels to create the thumbnail @@ -160,7 +172,11 @@ public static function thumbnailFile($filename, $width, $height, $mode = self::T } else { $image = Image::getImagine()->open($filename); } - $image = Image::thumbnail($image, $width, $height, $mode); + if ($mode === self::THUMBNAIL_INSET_BOX) { + $image = $image->thumbnail(new Box($width, $height), ManipulatorInterface::THUMBNAIL_INSET); + } else { + $image = Image::thumbnail($image, $width, $height, $mode); + } $options = [ 'quality' => $quality === null ? self::QUALITY : $quality @@ -189,6 +205,11 @@ public static function thumbnailFile($filename, $width, $height, $mode = self::T * [[Image::$thumbnailBackgroundColor]] or [[EasyThumbnail::$thumbnailBackgroundColor]], * and [[Image::$thumbnailBackgroundAlpha]] or [[EasyThumbnail::$thumbnailBackgroundAlpha]]. * + * If thumbnail mode is `self::THUMBNAIL_INSET_BOX`, the original image is scaled down so it is fully contained + * within the thumbnail dimensions. The specified $width and $height (supplied via $size) will be considered + * maximum limits. Unless the given dimensions are equal to the original image’s aspect ratio, one dimension in the + * resulting thumbnail will be smaller than the given limit. + * * @param string $filename the image file path or path alias or URL * @param integer $width the width in pixels to create the thumbnail * @param integer $height the height in pixels to create the thumbnail