Skip to content

Commit

Permalink
2017-03-01 1542
Browse files Browse the repository at this point in the history
  • Loading branch information
allanmcarvalho committed Mar 1, 2017
1 parent 6cdcc83 commit e8d398d
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 79 deletions.
19 changes: 12 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,17 @@ class ExamplesTable extends Table
],
'thumbnails' => [
[
'height' => 750, // width will be automatically calculated
'label' => 'thumb1',
'resize' => [
'height' => 750,
],
'watermark' => false // Disables watermark for this item
],
[
'height' => 750,
'width' => 750,
'label' => 'thumb2',
'resize' => [
'min_size' => 750,
],
'watermark' => [
'opacity' => 60, // 60% of opacity
'position' => 'top', // center top position
Expand All @@ -120,7 +125,7 @@ class ExamplesTable extends Table
>
> - **crop:** (optional) Crop the image. **Obs.:** If resize is also configured, it will be done before crop. Default: **Does not have**;
- **width:** (at least one) The crop image width. Default: **If height is set is the same**;
- **height:** (at least one) The crop image height. Default: **If widthis set is the same**;
- **height:** (at least one) The crop image height. Default: **If width is set is the same**;
- **x:** (optional) The crop image x position based from left. Default: **Center**;
- **y:** (optional) The crop image y position based from top. Default: **Center**;
- **format:** Image format. It can be (jpg, png, gif). Default: `jpg`;
Expand All @@ -130,7 +135,7 @@ class ExamplesTable extends Table
- **height:** (at least one) New image height. Default: **If width is set is automatic**;
- **min_size:** (at least one) Resize an image from the smaller side. Default: `false`;
- **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**;
- **label:** (required) Label for the folder where the thumbnail will be saved. . Default: **none**;
- **resize:** (optional) Changes the image size. Default: **Does not have**;
- **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**;
Expand All @@ -140,8 +145,8 @@ class ExamplesTable extends Table
- **path:** (optional) Path to watermark image for this thumbnail. Default: **Same as original**;
- **position:** (optional) Watermak orientation. Default: `bottom-right`. It can be the same positions quotes below;
- **crop:** (optional) Crop the new thumbnail image. **Obs.:** If resize is also configured, it will be done before crop. Default: **Does not have**;
- **width:** (required) New image crop width. Default:**Does not have**;
- **height:** (required) New image crop height. Default: **Does not have**;
- **width:** (required) New image crop width. Default:**If height is set is the same**;
- **height:** (required) New image crop height. Default: **If width is set is the same**;
- **x:** (required) The crop image x position. Default: **Center**;
- **y:** (required) The crop image y position. Default: **Center**;
- **watermark:** Insert watermark on image. Default: **Does not have**;
Expand Down
66 changes: 5 additions & 61 deletions src/File/Writer/ImageWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class ImageWriter extends DefaultWriter
* @var int
*/
private $resize_width = false;

/**
* Make resize with min image size
* @var int
Expand Down Expand Up @@ -191,72 +191,16 @@ public function deleteThubnails($path, $filename)
$result = true;
foreach ($this->thumbnails as $thumbnail)
{
$width = Hash::get($thumbnail, 'width', false);
$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)
{
return false;
}

if ($width === false)
{
$newWidth = $this->getEquivalentResizeWidth($image, $height);
$width = $newWidth <= $image->width() ? $newWidth : $image->width();
} else
if ($label === false)
{
$width = $width <= $image->width() ? $width : $image->width();
continue;
}

if ($height === false)
{
$newHeight = $this->getEquivalentResizeHeight($image, $width);
$height = $newHeight <= $image->height() ? $newHeight : $image->height();
} else
if (!$this->_delete($this->getPath($label), $filename))
{
$height = $height <= $image->height() ? $height : $image->height();
}

if ($label == !false)
{
if (!$this->_delete($this->getPath($label), $filename))
{
$result = false;
}
} else
{
if ($cropWidth !== false or $cropHeight !== 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;
}
}
$result = false;
}
}
return $result;
Expand Down
19 changes: 8 additions & 11 deletions src/File/Writer/Traits/ImageTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ protected function insertWatermark($image, $path, $position, $opacity)
$targetWarthermarkHeight = $this->getEquivalentResizeHeight($image, $targetWarthermarkWidth);
}

$this->resize($watermark, $targetWarthermarkWidth, $targetWarthermarkHeight);
$this->resize($watermark, $targetWarthermarkWidth, $targetWarthermarkHeight, false);

$watermark->opacity($opacity);

Expand Down Expand Up @@ -177,6 +177,11 @@ protected function createThumbnails()
continue;
}

if($label === false)
{
continue;
}

if ($width === false and $height === false and $minSize !== false)
{
if ($newThumbnail->width() < $newThumbnail->height())
Expand All @@ -201,7 +206,7 @@ protected function createThumbnails()
$height = $this->getEquivalentResizeHeight($newThumbnail, $width);
}

$this->resize($newThumbnail, $width, $height);
$this->resize($newThumbnail, $width, $height, $minSize);

if ($cropWidth !== false or $cropHeight !== false)
{
Expand All @@ -220,15 +225,7 @@ protected function createThumbnails()
$this->insertWatermark($newThumbnail, $watermarkPath, $watermarkPosition, $watermarkOpacity);
}

if ($label == !false)
{
$subPath = $label;
} else
{
$subPath = "{$newThumbnail->getWidth()}x{$newThumbnail->getHeight()}";
}

if (!$newThumbnail->save($this->getPath($subPath) . $this->getFilename(), $this->getConfigImageQuality()))
if (!$newThumbnail->save($this->getPath($label) . $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 e8d398d

Please sign in to comment.