From 6676fbc712d9016f4bcf8c3f1580914e2c7b8af7 Mon Sep 17 00:00:00 2001 From: Vitaliy Date: Thu, 28 Apr 2016 11:13:53 +0300 Subject: [PATCH] 28.04.16 перенес функционал по ужиманию картинок в UploaderComponent если с ними буту траблы ищи причину в этом --- common/behaviors/ShowImage.php | 16 ++++++++++++++++ common/modules/file/components/UploaderComponent.php | 71 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ common/modules/file/controllers/UploaderController.php | 64 ++++------------------------------------------------------------ composer.lock | 298 +++++++++++++++++++++++++++++++++++++++++++++++++++++++--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 4 files changed, 146 insertions(+), 303 deletions(-) create mode 100644 common/modules/file/components/UploaderComponent.php diff --git a/common/behaviors/ShowImage.php b/common/behaviors/ShowImage.php index 2b9b812..e8565f0 100644 --- a/common/behaviors/ShowImage.php +++ b/common/behaviors/ShowImage.php @@ -2,6 +2,7 @@ namespace common\behaviors; +use common\modules\file\components\UploaderComponent; use yii; use yii\base\Behavior; @@ -24,6 +25,21 @@ class ShowImage extends Behavior preg_match('/\.(.[^.]*)$/', $dir, $type); if(isset($type[1])){ $dir = preg_replace( $preg, '/'.$width.'x'.$height.'.'.$type[1], $dir); + $storage = dirname(yii\helpers\Url::to('@storage')); + $filename = $storage.$dir; + if (!file_exists($filename)) { + + $original = $storage.dirname($dir).'/original.'.$type[1]; + if (file_exists($original)) { + $resizer = new UploaderComponent(); + $resizer->resizeImg($width,$height, $original,$filename); + + } else { + throw new \Exception("Файл $original не существует"); + } + + } + } } diff --git a/common/modules/file/components/UploaderComponent.php b/common/modules/file/components/UploaderComponent.php new file mode 100644 index 0000000..50e5507 --- /dev/null +++ b/common/modules/file/components/UploaderComponent.php @@ -0,0 +1,71 @@ +$w){ + return true; + }else if($height >$h) { + return true; + } + return false; + } + + public function resizeImg($w, $h, $imageAlias,$imageAliasSave){ + $img = Image::getImagine()->open(Yii::getAlias($imageAlias)); + + $size = $img->getSize(); + + $width = $size->getWidth(); + $height = $size->getHeight(); + + $e_width = $w/$h; + $e_height = $h/$w; + + $e1_width = $width/$height; + $e1_height = $height/$width; + + + + if($e_width<$e1_width){ + + $new_width = $width*($e_width/$e1_width); + + $y = 0; + $x = $width/ 2-($new_width/2); + $width = $new_width; + + }else { + + $new_height = $height*($e_height/$e1_height); + $x = 0; + $y = $height/2-($new_height/2); + $height = $new_height; + } + + + + + Image::crop($imageAlias, $width, $height,[$x,$y]) + ->save(Yii::getAlias($imageAliasSave), ['quality' => + 100]); + + + $imagine = new Imagine(); + $imagine->open($imageAliasSave) + ->resize(new Box($w, $h)) + ->save($imageAliasSave, array('flatten' => false)); + + + } + + +} \ No newline at end of file diff --git a/common/modules/file/controllers/UploaderController.php b/common/modules/file/controllers/UploaderController.php index d9e4e2c..ec3e925 100755 --- a/common/modules/file/controllers/UploaderController.php +++ b/common/modules/file/controllers/UploaderController.php @@ -6,26 +6,15 @@ * Time: 9:58 */ namespace common\modules\file\controllers; +use common\modules\file\components\UploaderComponent; use Yii; use yii\helpers\ArrayHelper; use yii\web\UploadedFile; use common\modules\file\models\ImageSizerForm; use yii\web\Controller; -use Imagine\Gd\Imagine; -use Imagine\Image\Box; -use yii\imagine\Image; class UploaderController extends Controller { - public function isBigger($width,$height,$w,$h) - { - if($width>$w){ - return true; - }else if($height >$h) { - return true; - } - return false; - } private function getUserPath(){ @@ -36,55 +25,8 @@ class UploaderController extends Controller { } } - private function resizeImg($w, $h, $imageAlias,$imageAliasSave){ - $img = Image::getImagine()->open(Yii::getAlias($imageAlias)); - - $size = $img->getSize(); - - $width = $size->getWidth(); - $height = $size->getHeight(); - - $e_width = $w/$h; - $e_height = $h/$w; - - $e1_width = $width/$height; - $e1_height = $height/$width; - - if($e_width<$e1_width){ - - $new_width = $width*($e_width/$e1_width); - - $y = 0; - $x = $width/ 2-($new_width/2); - $width = $new_width; - - }else { - - $new_height = $height*($e_height/$e1_height); - $x = 0; - $y = $height/2-($new_height/2); - $height = $new_height; - } - - - - - Image::crop($imageAlias, $width, $height,[$x,$y]) - ->save(Yii::getAlias($imageAliasSave), ['quality' => - 100]); - - - $imagine = new Imagine(); - $imagine->open($imageAliasSave) - ->resize(new Box($w, $h)) - ->save($imageAliasSave, array('flatten' => false)); - - - - } - private function deleteImages($old_img){ if(!empty($old_img) && file_exists($_SERVER['DOCUMENT_ROOT'].$old_img)){ @@ -162,7 +104,9 @@ class UploaderController extends Controller { $imageLink = '/storage/'.$this->getUserPath().'/'.$md5_file.'/'.$size['width'].'x'.$size['height'].'.'.$model->file->extension; - $this->resizeImg($size['width'], $size['height'], $imageOrigAlias,$imageAlias); + $resizer = new UploaderComponent(); + + $resizer->resizeImg($size['width'], $size['height'], $imageOrigAlias,$imageAlias); } } diff --git a/composer.lock b/composer.lock index d409974..b4453dc 100755 --- a/composer.lock +++ b/composer.lock @@ -429,8 +429,7 @@ }, "license": [ "MIT" - ], - "time": "2015-12-31 08:33:51" + ] }, { "name": "cebe/markdown", @@ -541,12 +540,12 @@ "source": { "type": "git", "url": "https://github.com/Codeception/Codeception.git", - "reference": "061cd3b6bece7cdd32456079b5217734342aeda9" + "reference": "099d101299e8ab43413b4787189d5da1927ec6a2" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/Codeception/Codeception/zipball/061cd3b6bece7cdd32456079b5217734342aeda9", - "reference": "061cd3b6bece7cdd32456079b5217734342aeda9", + "url": "https://api.github.com/repos/Codeception/Codeception/zipball/099d101299e8ab43413b4787189d5da1927ec6a2", + "reference": "099d101299e8ab43413b4787189d5da1927ec6a2", "shasum": "" }, "require": { @@ -618,7 +617,7 @@ "functional testing", "unit testing" ], - "time": "2016-04-20 22:27:36" + "time": "2016-04-26 21:38:53" }, { "name": "developeruz/yii2-db-rbac", @@ -1134,12 +1133,12 @@ "source": { "type": "git", "url": "https://github.com/kartik-v/yii2-krajee-base.git", - "reference": "a46d88d937f914b71bee85d181687b8f2098e6fa" + "reference": "314f3b31990870856dbdf5f9bf5192ee683a386a" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/kartik-v/yii2-krajee-base/zipball/a46d88d937f914b71bee85d181687b8f2098e6fa", - "reference": "a46d88d937f914b71bee85d181687b8f2098e6fa", + "url": "https://api.github.com/repos/kartik-v/yii2-krajee-base/zipball/314f3b31990870856dbdf5f9bf5192ee683a386a", + "reference": "314f3b31990870856dbdf5f9bf5192ee683a386a", "shasum": "" }, "require": { @@ -1177,7 +1176,7 @@ "widget", "yii2" ], - "time": "2016-04-21 17:23:21" + "time": "2016-04-28 03:01:32" }, { "name": "kartik-v/yii2-widget-colorinput", @@ -1239,12 +1238,12 @@ "source": { "type": "git", "url": "https://github.com/kartik-v/yii2-widget-rating.git", - "reference": "69b192bc2b26a435618e17eed7c56294ef805fab" + "reference": "e3110576b60ecb6098bc8389efb5ef3a402b6e61" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/kartik-v/yii2-widget-rating/zipball/69b192bc2b26a435618e17eed7c56294ef805fab", - "reference": "69b192bc2b26a435618e17eed7c56294ef805fab", + "url": "https://api.github.com/repos/kartik-v/yii2-widget-rating/zipball/e3110576b60ecb6098bc8389efb5ef3a402b6e61", + "reference": "e3110576b60ecb6098bc8389efb5ef3a402b6e61", "shasum": "" }, "require": { @@ -1287,7 +1286,7 @@ "widget", "yii2" ], - "time": "2016-02-17 19:13:26" + "time": "2016-04-27 19:26:00" }, { "name": "kartik-v/yii2-widget-select2", @@ -1506,48 +1505,6 @@ "time": "2015-07-03 07:08:52" }, { - "name": "myclabs/deep-copy", - "version": "1.5.0", - "source": { - "type": "git", - "url": "https://github.com/myclabs/DeepCopy.git", - "reference": "e3abefcd7f106677fd352cd7c187d6c969aa9ddc" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/myclabs/DeepCopy/zipball/e3abefcd7f106677fd352cd7c187d6c969aa9ddc", - "reference": "e3abefcd7f106677fd352cd7c187d6c969aa9ddc", - "shasum": "" - }, - "require": { - "php": ">=5.4.0" - }, - "require-dev": { - "doctrine/collections": "1.*", - "phpunit/phpunit": "~4.1" - }, - "type": "library", - "autoload": { - "psr-4": { - "DeepCopy\\": "src/DeepCopy/" - } - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "MIT" - ], - "description": "Create deep copies (clones) of your objects", - "homepage": "https://github.com/myclabs/DeepCopy", - "keywords": [ - "clone", - "copy", - "duplicate", - "object", - "object graph" - ], - "time": "2015-11-07 22:20:37" - }, - { "name": "nodge/lightopenid", "version": "1.1.2", "source": { @@ -1759,40 +1716,39 @@ }, { "name": "phpunit/php-code-coverage", - "version": "dev-master", + "version": "2.2.x-dev", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/php-code-coverage.git", - "reference": "6d3b505cb3a6143adcd58375870eb0b4b98636bc" + "reference": "eabf68b476ac7d0f73793aada060f1c1a9bf8979" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/php-code-coverage/zipball/6d3b505cb3a6143adcd58375870eb0b4b98636bc", - "reference": "6d3b505cb3a6143adcd58375870eb0b4b98636bc", + "url": "https://api.github.com/repos/sebastianbergmann/php-code-coverage/zipball/eabf68b476ac7d0f73793aada060f1c1a9bf8979", + "reference": "eabf68b476ac7d0f73793aada060f1c1a9bf8979", "shasum": "" }, "require": { - "php": "^5.6 || ^7.0", + "php": ">=5.3.3", "phpunit/php-file-iterator": "~1.3", "phpunit/php-text-template": "~1.2", - "phpunit/php-token-stream": "^1.4.2", - "sebastian/code-unit-reverse-lookup": "~1.0", + "phpunit/php-token-stream": "~1.3", "sebastian/environment": "^1.3.2", - "sebastian/version": "~1.0|~2.0" + "sebastian/version": "~1.0" }, "require-dev": { "ext-xdebug": ">=2.1.4", - "phpunit/phpunit": "^5.4" + "phpunit/phpunit": "~4" }, "suggest": { "ext-dom": "*", - "ext-xdebug": ">=2.4.0", + "ext-xdebug": ">=2.2.1", "ext-xmlwriter": "*" }, "type": "library", "extra": { "branch-alias": { - "dev-master": "4.0.x-dev" + "dev-master": "2.2.x-dev" } }, "autoload": { @@ -1818,7 +1774,7 @@ "testing", "xunit" ], - "time": "2016-04-21 05:23:34" + "time": "2015-10-06 15:47:00" }, { "name": "phpunit/php-file-iterator", @@ -2000,16 +1956,16 @@ }, { "name": "phpunit/phpunit", - "version": "dev-master", + "version": "4.8.x-dev", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/phpunit.git", - "reference": "9b5b99b3da70e3eea5294712bc5843910ad2bad6" + "reference": "3c4becbce99732549949904c47b76ffe602a7595" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/9b5b99b3da70e3eea5294712bc5843910ad2bad6", - "reference": "9b5b99b3da70e3eea5294712bc5843910ad2bad6", + "url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/3c4becbce99732549949904c47b76ffe602a7595", + "reference": "3c4becbce99732549949904c47b76ffe602a7595", "shasum": "" }, "require": { @@ -2018,22 +1974,19 @@ "ext-pcre": "*", "ext-reflection": "*", "ext-spl": "*", - "myclabs/deep-copy": "~1.3", - "php": "^5.6 || ^7.0", + "php": ">=5.3.3", "phpspec/prophecy": "^1.3.1", - "phpunit/php-code-coverage": "^4.0", + "phpunit/php-code-coverage": "~2.1", "phpunit/php-file-iterator": "~1.4", "phpunit/php-text-template": "~1.2", "phpunit/php-timer": "^1.0.6", - "phpunit/phpunit-mock-objects": "^3.2", + "phpunit/phpunit-mock-objects": "~2.3", "sebastian/comparator": "~1.1", "sebastian/diff": "~1.2", "sebastian/environment": "~1.3", "sebastian/exporter": "~1.2", "sebastian/global-state": "~1.0", - "sebastian/object-enumerator": "~1.0", - "sebastian/resource-operations": "~1.0", - "sebastian/version": "~1.0|~2.0", + "sebastian/version": "~1.0", "symfony/yaml": "~2.1|~3.0" }, "suggest": { @@ -2045,7 +1998,7 @@ "type": "library", "extra": { "branch-alias": { - "dev-master": "5.4.x-dev" + "dev-master": "4.8.x-dev" } }, "autoload": { @@ -2071,30 +2024,30 @@ "testing", "xunit" ], - "time": "2016-04-19 17:48:13" + "time": "2016-04-25 09:17:33" }, { "name": "phpunit/phpunit-mock-objects", - "version": "dev-master", + "version": "2.3.x-dev", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/phpunit-mock-objects.git", - "reference": "5d8c2a839d2c77757b7499eb135f34f9f5f07e6f" + "reference": "ac8e7a3db35738d56ee9a76e78a4e03d97628983" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/phpunit-mock-objects/zipball/5d8c2a839d2c77757b7499eb135f34f9f5f07e6f", - "reference": "5d8c2a839d2c77757b7499eb135f34f9f5f07e6f", + "url": "https://api.github.com/repos/sebastianbergmann/phpunit-mock-objects/zipball/ac8e7a3db35738d56ee9a76e78a4e03d97628983", + "reference": "ac8e7a3db35738d56ee9a76e78a4e03d97628983", "shasum": "" }, "require": { "doctrine/instantiator": "^1.0.2", - "php": ">=5.6", + "php": ">=5.3.3", "phpunit/php-text-template": "~1.2", "sebastian/exporter": "~1.2" }, "require-dev": { - "phpunit/phpunit": "^5.4" + "phpunit/phpunit": "~4.4" }, "suggest": { "ext-soap": "*" @@ -2102,7 +2055,7 @@ "type": "library", "extra": { "branch-alias": { - "dev-master": "3.2.x-dev" + "dev-master": "2.3.x-dev" } }, "autoload": { @@ -2127,7 +2080,7 @@ "mock", "xunit" ], - "time": "2016-04-20 14:39:30" + "time": "2015-10-02 06:51:40" }, { "name": "psr/http-message", @@ -2282,51 +2235,6 @@ "time": "2016-04-21 11:55:25" }, { - "name": "sebastian/code-unit-reverse-lookup", - "version": "dev-master", - "source": { - "type": "git", - "url": "https://github.com/sebastianbergmann/code-unit-reverse-lookup.git", - "reference": "c36f5e7cfce482fde5bf8d10d41a53591e0198fe" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/code-unit-reverse-lookup/zipball/c36f5e7cfce482fde5bf8d10d41a53591e0198fe", - "reference": "c36f5e7cfce482fde5bf8d10d41a53591e0198fe", - "shasum": "" - }, - "require": { - "php": ">=5.6" - }, - "require-dev": { - "phpunit/phpunit": "~5" - }, - "type": "library", - "extra": { - "branch-alias": { - "dev-master": "1.0.x-dev" - } - }, - "autoload": { - "classmap": [ - "src/" - ] - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "BSD-3-Clause" - ], - "authors": [ - { - "name": "Sebastian Bergmann", - "email": "sebastian@phpunit.de" - } - ], - "description": "Looks up which function or method a line of code belongs to", - "homepage": "https://github.com/sebastianbergmann/code-unit-reverse-lookup/", - "time": "2016-02-13 06:45:14" - }, - { "name": "sebastian/comparator", "version": "dev-master", "source": { @@ -2611,52 +2519,6 @@ "time": "2015-10-12 03:26:01" }, { - "name": "sebastian/object-enumerator", - "version": "dev-master", - "source": { - "type": "git", - "url": "https://github.com/sebastianbergmann/object-enumerator.git", - "reference": "d4ca2fb70344987502567bc50081c03e6192fb26" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/object-enumerator/zipball/d4ca2fb70344987502567bc50081c03e6192fb26", - "reference": "d4ca2fb70344987502567bc50081c03e6192fb26", - "shasum": "" - }, - "require": { - "php": ">=5.6", - "sebastian/recursion-context": "~1.0" - }, - "require-dev": { - "phpunit/phpunit": "~5" - }, - "type": "library", - "extra": { - "branch-alias": { - "dev-master": "1.0.x-dev" - } - }, - "autoload": { - "classmap": [ - "src/" - ] - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "BSD-3-Clause" - ], - "authors": [ - { - "name": "Sebastian Bergmann", - "email": "sebastian@phpunit.de" - } - ], - "description": "Traverses array structures and object graphs to enumerate all referenced objects", - "homepage": "https://github.com/sebastianbergmann/object-enumerator/", - "time": "2016-01-28 13:25:10" - }, - { "name": "sebastian/recursion-context", "version": "dev-master", "source": { @@ -2710,70 +2572,20 @@ "time": "2016-01-28 05:39:29" }, { - "name": "sebastian/resource-operations", - "version": "dev-master", - "source": { - "type": "git", - "url": "https://github.com/sebastianbergmann/resource-operations.git", - "reference": "ce990bb21759f94aeafd30209e8cfcdfa8bc3f52" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/resource-operations/zipball/ce990bb21759f94aeafd30209e8cfcdfa8bc3f52", - "reference": "ce990bb21759f94aeafd30209e8cfcdfa8bc3f52", - "shasum": "" - }, - "require": { - "php": ">=5.6.0" - }, - "type": "library", - "extra": { - "branch-alias": { - "dev-master": "1.0.x-dev" - } - }, - "autoload": { - "classmap": [ - "src/" - ] - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "BSD-3-Clause" - ], - "authors": [ - { - "name": "Sebastian Bergmann", - "email": "sebastian@phpunit.de" - } - ], - "description": "Provides a list of PHP built-in functions that operate on resources", - "homepage": "https://www.github.com/sebastianbergmann/resource-operations", - "time": "2015-07-28 20:34:47" - }, - { "name": "sebastian/version", - "version": "dev-master", + "version": "1.0.6", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/version.git", - "reference": "c829badbd8fdf16a0bad8aa7fa7971c029f1b9c5" + "reference": "58b3a85e7999757d6ad81c787a1fbf5ff6c628c6" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/version/zipball/c829badbd8fdf16a0bad8aa7fa7971c029f1b9c5", - "reference": "c829badbd8fdf16a0bad8aa7fa7971c029f1b9c5", + "url": "https://api.github.com/repos/sebastianbergmann/version/zipball/58b3a85e7999757d6ad81c787a1fbf5ff6c628c6", + "reference": "58b3a85e7999757d6ad81c787a1fbf5ff6c628c6", "shasum": "" }, - "require": { - "php": ">=5.6" - }, "type": "library", - "extra": { - "branch-alias": { - "dev-master": "2.0.x-dev" - } - }, "autoload": { "classmap": [ "src/" @@ -2792,7 +2604,7 @@ ], "description": "Library that helps with managing the version number of Git-hosted PHP projects", "homepage": "https://github.com/sebastianbergmann/version", - "time": "2016-02-04 12:56:52" + "time": "2015-06-21 13:59:46" }, { "name": "swiftmailer/swiftmailer", @@ -3296,12 +3108,12 @@ "source": { "type": "git", "url": "https://github.com/yiisoft/yii2-framework.git", - "reference": "10db2b4a2c361fc0e5824640f48781d24e304358" + "reference": "a1bd20682516fa5a6f2f0518f15197cf6437998a" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/yiisoft/yii2-framework/zipball/10db2b4a2c361fc0e5824640f48781d24e304358", - "reference": "10db2b4a2c361fc0e5824640f48781d24e304358", + "url": "https://api.github.com/repos/yiisoft/yii2-framework/zipball/a1bd20682516fa5a6f2f0518f15197cf6437998a", + "reference": "a1bd20682516fa5a6f2f0518f15197cf6437998a", "shasum": "" }, "require": { @@ -3382,7 +3194,7 @@ "framework", "yii2" ], - "time": "2016-04-21 20:58:36" + "time": "2016-04-28 01:02:17" }, { "name": "yiisoft/yii2-bootstrap", @@ -3709,12 +3521,12 @@ "source": { "type": "git", "url": "https://github.com/fzaninotto/Faker.git", - "reference": "1c33e894fbbad6cf65bd42871719cd33227ed6a7" + "reference": "6abfc0cec5648e6ccc6a8053533383ecaf4dbdb4" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/fzaninotto/Faker/zipball/1c33e894fbbad6cf65bd42871719cd33227ed6a7", - "reference": "1c33e894fbbad6cf65bd42871719cd33227ed6a7", + "url": "https://api.github.com/repos/fzaninotto/Faker/zipball/6abfc0cec5648e6ccc6a8053533383ecaf4dbdb4", + "reference": "6abfc0cec5648e6ccc6a8053533383ecaf4dbdb4", "shasum": "" }, "require": { @@ -3751,7 +3563,7 @@ "faker", "fixtures" ], - "time": "2016-04-13 06:45:05" + "time": "2016-04-28 06:53:57" }, { "name": "phpspec/php-diff", -- libgit2 0.21.4