From de842841b72fe286eb1dfbb23bf04b691e29c96f Mon Sep 17 00:00:00 2001 From: Allan Mariucci Carvalho Date: Mon, 27 Feb 2017 11:20:48 -0300 Subject: [PATCH] 2017-02-27 1120 --- README.md | 1 + src/File/Writer/ImageWriter.php | 50 ++++++++++++++++----------- src/File/Writer/Traits/ImageTrait.php | 25 +++++++++----- 3 files changed, 48 insertions(+), 28 deletions(-) diff --git a/README.md b/README.md index 7eecea0..317982e 100644 --- a/README.md +++ b/README.md @@ -129,6 +129,7 @@ class ExamplesTable extends Table - **width:** (at least one) New image width. Default: **If height is set is automatic**; - **height:** (at least one) New image height. Default: **If width is set is automatic**; - **thumbnails:** (optional) Setting to set thumbnails to be created. Default: **Does not have**; + - **label:** (optional) Label for the folder where the thumbnail will be saved. When not informed, the dimensions of the image will be the name of the folder.. Default: **Dimensions of the image**; - **width:** (at least one) Thumbnail width. Default: **If height is set is automatic**; - **height:** (at least one) Thumbnail height. Default: **If width is set is automatic**; - **watermark:** (optional) If `true` follows the default image settings (if exists). If `false` does not insert the watermark. If any setting is passed in an **array**, overwrites the default image settings. Default: `true`; diff --git a/src/File/Writer/ImageWriter.php b/src/File/Writer/ImageWriter.php index 7c3be11..bbe5dec 100644 --- a/src/File/Writer/ImageWriter.php +++ b/src/File/Writer/ImageWriter.php @@ -188,6 +188,7 @@ public function deleteThubnails($path, $filename) $height = Hash::get($thumbnail, 'height', false); $cropWidth = Hash::get($thumbnail, 'crop.width', false); $cropHeight = Hash::get($thumbnail, 'crop.height', false); + $label = Hash::get($thumbnail, 'label', false); if ($width === false and $height === false) { @@ -212,33 +213,42 @@ public function deleteThubnails($path, $filename) $height = $height <= $image->height() ? $height : $image->height(); } - if ($cropWidth !== false or $cropHeight !== false) + if ($label == !false) { - if ($cropWidth === false) - { - $cropWidth = $cropHeight <= $width ? $cropHeight : $width; - } else - { - $cropWidth = $cropWidth <= $width ? $cropWidth : $width; - } - - if ($cropHeight === false) - { - $cropHeight = $cropWidth <= $height ? $cropWidth : $height; - } else - { - $cropHeight = $cropHeight <= $height ? $cropHeight : $height; - } - - if (!$this->_delete($this->getPath("{$cropWidth}x{$cropHeight}"), $filename)) + if (!$this->_delete($this->getPath($label), $filename)) { $result = false; } } else { - if (!$this->_delete($this->getPath("{$width}x{$height}"), $filename)) + if ($cropWidth !== false or $cropHeight !== false) { - $result = false; + if ($cropWidth === false) + { + $cropWidth = $cropHeight <= $width ? $cropHeight : $width; + } else + { + $cropWidth = $cropWidth <= $width ? $cropWidth : $width; + } + + if ($cropHeight === false) + { + $cropHeight = $cropWidth <= $height ? $cropWidth : $height; + } else + { + $cropHeight = $cropHeight <= $height ? $cropHeight : $height; + } + + if (!$this->_delete($this->getPath("{$cropWidth}x{$cropHeight}"), $filename)) + { + $result = false; + } + } else + { + if (!$this->_delete($this->getPath("{$width}x{$height}"), $filename)) + { + $result = false; + } } } } diff --git a/src/File/Writer/Traits/ImageTrait.php b/src/File/Writer/Traits/ImageTrait.php index 7bf7bcb..423dfdd 100644 --- a/src/File/Writer/Traits/ImageTrait.php +++ b/src/File/Writer/Traits/ImageTrait.php @@ -121,16 +121,16 @@ protected function insertWatermark($image, $path, $position, $opacity) $targetHeight = intval($image->height() * 0.07); $targetWarthermarkHeight = $targetHeight <= $watermark->height() ? $targetHeight : $watermark->height(); $targetWarthermarkWidth = $this->getEquivalentResizeWidth($watermark, $targetWarthermarkHeight); - - if($targetWarthermarkWidth > $image->width()) + + if ($targetWarthermarkWidth > $image->width()) { - $targetWidth = intval($image->width() * 0.8); - $targetWarthermarkWidth = $targetWidth <= $watermark->width() ? $targetWidth : $watermark->width(); + $targetWidth = intval($image->width() * 0.8); + $targetWarthermarkWidth = $targetWidth <= $watermark->width() ? $targetWidth : $watermark->width(); $targetWarthermarkHeight = $this->getEquivalentResizeHeight($image, $targetWarthermarkWidth); } $this->resize($watermark, $targetWarthermarkWidth, $targetWarthermarkHeight); - + $watermark->opacity($opacity); $image->insert($watermark, $position, round($image->width() * 0.05), round($image->height() * 0.05)); @@ -156,6 +156,7 @@ protected function createThumbnails() $cropHeight = Hash::get($thumbnail, 'crop.height', false); $cropX = Hash::get($thumbnail, 'crop.x', null); $cropY = Hash::get($thumbnail, 'crop.y', null); + $label = Hash::get($thumbnail, 'label', false); if ($width === false) { @@ -175,18 +176,26 @@ protected function createThumbnails() } $watermarkPath = Hash::get($thumbnail, 'watermark.path', $this->watermark); - if(empty($watermarkPath)) + if (empty($watermarkPath)) { $watermarkPath = $this->watermark; } if ($watermarkPath !== false and Hash::get($thumbnail, 'watermark', true)) { $watermarkPosition = Hash::get($thumbnail, 'watermark.position', $this->watermark_position); - $watermarkOpacity = Hash::get($thumbnail, 'watermark.opacity', $this->watermark_opacity); + $watermarkOpacity = Hash::get($thumbnail, 'watermark.opacity', $this->watermark_opacity); $this->insertWatermark($newThumbnail, $watermarkPath, $watermarkPosition, $watermarkOpacity); } + + if($label ==! false) + { + $subPath = $label; + }else + { + $subPath = "{$newThumbnail->getWidth()}x{$newThumbnail->getHeight()}"; + } - if (!$newThumbnail->save($this->getPath("{$newThumbnail->getWidth()}x{$newThumbnail->getHeight()}") . $this->getFilename(), $this->getConfigImageQuality())) + if (!$newThumbnail->save($this->getPath($subPath) . $this->getFilename(), $this->getConfigImageQuality())) { \Cake\Log\Log::error(__d('upload', 'Unable to salve thumbnail "{0}" in entity id "{1}" from table "{2}" and path "{3}" because it does not exist', $this->getFileName(), $this->entity->get($this->table->getPrimaryKey()), $this->table->getTable(), $this->getPath())); }