From e94544447f7dd79fea85e9d42af5ee63869f0bca Mon Sep 17 00:00:00 2001 From: Renoise Date: Tue, 22 Jul 2014 14:56:37 +0400 Subject: [PATCH] GD added --- Module.php | 4 +++ composer.json | 3 ++- models/Image.php | 63 +++++++++++++++++++++++++++++++++--------------- 3 files changed, 49 insertions(+), 21 deletions(-) diff --git a/Module.php b/Module.php index 84db880..dbd5de3 100755 --- a/Module.php +++ b/Module.php @@ -12,9 +12,13 @@ class Module extends \yii\base\Module public $imagesCachePath = '@app/web/imgCache'; + public $graphicsLibrary = 'GD'; + public $controllerNamespace = 'rico\yii2images\controllers'; + + public function getImage($item, $dirtyAlias) { //Get params diff --git a/composer.json b/composer.json index ee524a1..4d5eec0 100755 --- a/composer.json +++ b/composer.json @@ -13,7 +13,8 @@ ], "minimum-stability": "dev", "require": { - "yiisoft/yii2": "*" + "yiisoft/yii2": "*", + "abeautifulsite/simpleimage": "*" }, "require-dev": { "yiisoft/yii2-codeception": "*", diff --git a/models/Image.php b/models/Image.php index 52e6303..721a331 100755 --- a/models/Image.php +++ b/models/Image.php @@ -123,25 +123,6 @@ public function createVersion($imagePath, $sizeString = false) throw new \Exception('Image without urlAlias!'); } - $image = new \Imagick($imagePath); - $image->setImageCompressionQuality(100); - - if($sizeString){ - $size = $this->getModule()->parseSize($sizeString); - //p($size);die; - if($size){ - if($size['height'] && $size['width']){ - $image->cropThumbnailImage($size['width'], $size['height']); - }elseif($size['height']){ - $image->thumbnailImage(0, $size['height']); - }elseif($size['width']){ - $image->thumbnailImage($size['width'], 0); - }else{ - throw new \Exception('Something wrong with this->module->parseSize($sizeString)'); - } - } - } - $cachePath = $this->getModule()->getCachePath(); $subDirPath = $this->getSubDur(); $fileExtension = pathinfo($this->filePath, PATHINFO_EXTENSION); @@ -156,7 +137,49 @@ public function createVersion($imagePath, $sizeString = false) BaseFileHelper::createDirectory(dirname($pathToSave), 0777, true); - $image->writeImage($pathToSave); + + if($sizeString) { + $size = $this->getModule()->parseSize($sizeString); + }else{ + $size = false; + } + + if($this->getModule()->graphicsLibrary == 'Imagick'){ + $image = new \Imagick($imagePath); + $image->setImageCompressionQuality(100); + + if($size){ + if($size['height'] && $size['width']){ + $image->cropThumbnailImage($size['width'], $size['height']); + }elseif($size['height']){ + $image->thumbnailImage(0, $size['height']); + }elseif($size['width']){ + $image->thumbnailImage($size['width'], 0); + }else{ + throw new \Exception('Something wrong with this->module->parseSize($sizeString)'); + } + } + + $image->writeImage($pathToSave); + }else{ + if($size){ + $image = new \abeautifulsite\SimpleImage($imagePath); + + if($size['height'] && $size['width']){ + + $image->thumbnail($size['width'], $size['height']); + }elseif($size['height']){ + $image->fit_to_height($size['height']); + }elseif($size['width']){ + $image->fit_to_width($size['width']); + }else{ + throw new \Exception('Something wrong with this->module->parseSize($sizeString)'); + } + } + + $image->save($pathToSave); + } + return $image;