Skip to content

Commit

Permalink
2017-03-01 1501
Browse files Browse the repository at this point in the history
  • Loading branch information
allanmcarvalho committed Mar 1, 2017
1 parent de84284 commit 6cdcc83
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 13 deletions.
15 changes: 9 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,17 +128,20 @@ class ExamplesTable extends Table
- **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**;
- **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**;
- **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`;
- **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**;
- **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**;
- **min_size:** (at least one) Resize an image from the smaller side. Default: `false`;
- **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`;
- **opacity:** (optional) Watermak opacity from 1 to 100 where the smaller is more transparent. Default: **Same as original**.
- **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**;
- **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 height. Default: **Does not have**;
- **height:** (required) New image crop height. Default: **Does not have**;
- **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
9 changes: 8 additions & 1 deletion src/File/Writer/ImageWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ class ImageWriter extends DefaultWriter
* @var int
*/
private $resize_width = false;

/**
* Make resize with min image size
* @var int
*/
private $resize_min_size = false;

/**
* value of crop height
Expand Down Expand Up @@ -103,6 +109,7 @@ public function __construct(Table $table, Entity $entity, $field, $settings)

$this->resize_heigth = Hash::get($this->settings, 'image.resize.height', false);
$this->resize_width = Hash::get($this->settings, 'image.resize.width', false);
$this->resize_min_size = Hash::get($this->settings, 'image.resize.min_size', false);
$this->crop_heigth = Hash::get($this->settings, 'image.crop.height', false);
$this->crop_width = Hash::get($this->settings, 'image.crop.width', false);
$this->crop_x = Hash::get($this->settings, 'image.crop.x', null);
Expand Down Expand Up @@ -262,7 +269,7 @@ public function deleteThubnails($path, $filename)
*/
private function modifyImage($image)
{
$this->resize($image, $this->resize_width, $this->resize_heigth);
$this->resize($image, $this->resize_width, $this->resize_heigth, $this->resize_min_size);

$this->crop($image, $this->crop_width, $this->crop_heigth, $this->crop_x, $this->crop_y);

Expand Down
45 changes: 39 additions & 6 deletions src/File/Writer/Traits/ImageTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,21 @@ protected function getImage($path)
* @param mixed $height must by smaller than the original
* @return Image
*/
protected function resize($image, $width, $height)
protected function resize($image, $width, $height, $minSize)
{
if ($width === false and $height === false and $minSize !== false)
{
if ($image->width() < $image->height())
{
$width = $minSize;
} elseif ($image->height() < $image->width())
{
$height = $minSize;
} elseif ($image->height() == $image->width())
{
$width = $minSize;
}
}
if ($width !== false or $height !== false)
{
if ($width === false)
Expand Down Expand Up @@ -150,14 +163,34 @@ protected function createThumbnails()
foreach ($this->thumbnails as $thumbnail)
{
$newThumbnail = $this->getImage($this->fileInfo['tmp_name']);
$width = Hash::get($thumbnail, 'width', false);
$height = Hash::get($thumbnail, 'height', false);
$width = Hash::get($thumbnail, 'resize.width', false);
$height = Hash::get($thumbnail, 'resize.height', false);
$minSize = Hash::get($thumbnail, 'resize.min_size', false);
$cropWidth = Hash::get($thumbnail, 'crop.width', false);
$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 and $height === false and $minSize === false and $cropWidth === false and $cropHeight === false)
{
continue;
}

if ($width === false and $height === false and $minSize !== false)
{
if ($newThumbnail->width() < $newThumbnail->height())
{
$width = $minSize;
} elseif ($newThumbnail->height() < $newThumbnail->width())
{
$height = $minSize;
} elseif ($newThumbnail->height() == $newThumbnail->width())
{
$width = $minSize;
}
}

if ($width === false)
{
$width = $this->getEquivalentResizeWidth($newThumbnail, $height);
Expand Down Expand Up @@ -186,11 +219,11 @@ protected function createThumbnails()
$watermarkOpacity = Hash::get($thumbnail, 'watermark.opacity', $this->watermark_opacity);
$this->insertWatermark($newThumbnail, $watermarkPath, $watermarkPosition, $watermarkOpacity);
}
if($label ==! false)

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

0 comments on commit 6cdcc83

Please sign in to comment.