From dc558b4e3f05c2c7d14582035258148bd226110a Mon Sep 17 00:00:00 2001 From: Andrey Girnik Date: Sat, 12 Sep 2020 23:10:20 +0700 Subject: [PATCH] Allow set null for one width or height in ThumbConfig --- README.md | 2 +- changelog.md | 3 +++ src/Module.php | 8 ++++---- src/components/ThumbConfig.php | 12 ++++++------ src/interfaces/ThumbConfigInterface.php | 8 ++++---- src/models/upload/LocalUpload.php | 12 ++++++------ 6 files changed, 24 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index be8b11f..fd44653 100644 --- a/README.md +++ b/README.md @@ -44,7 +44,7 @@ Addition module description you can see in my [Personal site](https://pack-devel Via composer: -`composer require itstructure/yii2-multi-format-uploader ~3.2.0` +`composer require itstructure/yii2-multi-format-uploader ~3.2.1` ### If you are testing this package from local server directory diff --git a/changelog.md b/changelog.md index e593455..361685c 100644 --- a/changelog.md +++ b/changelog.md @@ -1,5 +1,8 @@ ### CHANGE LOG: +**3.2.1 September 12, 2020:** +- Allow set `null` for one width or height in ThumbConfig. + **3.2.0 September 12, 2020:** - Optimize `deleteMediafiles()` method in `MediaFilesTrait`. Add protection to physical delete multiplied files, which are related more then one owner. - Optimize owner's entity classes. diff --git a/src/Module.php b/src/Module.php index 3791cfe..3942368 100644 --- a/src/Module.php +++ b/src/Module.php @@ -323,9 +323,9 @@ public static function configureThumb(string $alias, array $config): ThumbConfig if (!isset($config['name']) || !isset($config['size']) || !is_array($config['size']) || - !isset($config['size'][0]) || - !isset($config['size'][1])) { - + (!isset($config['size'][0]) && !is_null($config['size'][0])) || + (!isset($config['size'][1]) && !is_null($config['size'][1])) + ) { throw new InvalidConfigException('Error in thumb configuration.'); } @@ -335,7 +335,7 @@ public static function configureThumb(string $alias, array $config): ThumbConfig 'name' => $config['name'], 'width' => $config['size'][0], 'height' => $config['size'][1], - 'mode' => (isset($config['mode']) ? $config['mode'] : ImageInterface::THUMBNAIL_OUTBOUND), + 'mode' => (!empty($config['mode']) ? $config['mode'] : ImageInterface::THUMBNAIL_OUTBOUND), ]; /* @var ThumbConfigInterface $object */ diff --git a/src/components/ThumbConfig.php b/src/components/ThumbConfig.php index 7d42ac6..0d8c315 100644 --- a/src/components/ThumbConfig.php +++ b/src/components/ThumbConfig.php @@ -36,14 +36,14 @@ class ThumbConfig implements ThumbConfigInterface /** * Thumb width. * - * @var + * @var int|null */ public $width; /** * Thumb height. * - * @var + * @var int|null */ public $height; @@ -77,9 +77,9 @@ public function getName(): string /** * Get thumb width. * - * @return int + * @return int|null */ - public function getWidth(): int + public function getWidth() { return $this->width; } @@ -87,9 +87,9 @@ public function getWidth(): int /** * Get thumb height. * - * @return int + * @return int|null */ - public function getHeight(): int + public function getHeight() { return $this->height; } diff --git a/src/interfaces/ThumbConfigInterface.php b/src/interfaces/ThumbConfigInterface.php index 4c7cef4..2d23404 100644 --- a/src/interfaces/ThumbConfigInterface.php +++ b/src/interfaces/ThumbConfigInterface.php @@ -28,16 +28,16 @@ public function getName(): string ; /** * Get thumb width. * - * @return int + * @return int|null */ - public function getWidth(): int ; + public function getWidth(); /** * Get thumb height. * - * @return int + * @return int|null */ - public function getHeight(): int ; + public function getHeight(); /** * Get thumb mode. diff --git a/src/models/upload/LocalUpload.php b/src/models/upload/LocalUpload.php index 34e4119..f2b5e4b 100644 --- a/src/models/upload/LocalUpload.php +++ b/src/models/upload/LocalUpload.php @@ -155,15 +155,15 @@ protected function createThumb(ThumbConfigInterface $thumbConfig) DIRECTORY_SEPARATOR . $this->getThumbFilename($originalFile['filename'], $originalFile['extension'], - $thumbConfig->alias, - $thumbConfig->width, - $thumbConfig->height + $thumbConfig->getAlias(), + $thumbConfig->getWidth(), + $thumbConfig->getHeight() ); Image::thumbnail($this->uploadRoot . DIRECTORY_SEPARATOR . ltrim(ltrim($this->mediafileModel->url, '\\'), '/'), - $thumbConfig->width, - $thumbConfig->height, - $thumbConfig->mode + $thumbConfig->getWidth(), + $thumbConfig->getHeight(), + $thumbConfig->getMode() )->save($this->uploadRoot . DIRECTORY_SEPARATOR . ltrim(ltrim($thumbUrl, '\\'), '/')); return $thumbUrl;