Skip to content

Commit

Permalink
2017-02-27 1120
Browse files Browse the repository at this point in the history
  • Loading branch information
allanmcarvalho committed Feb 27, 2017
1 parent bf643c3 commit de84284
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 28 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`;
Expand Down
50 changes: 30 additions & 20 deletions src/File/Writer/ImageWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -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;
}
}
}
}
Expand Down
25 changes: 17 additions & 8 deletions src/File/Writer/Traits/ImageTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -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)
{
Expand All @@ -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()));
}
Expand Down

0 comments on commit de84284

Please sign in to comment.