Skip to content

Commit

Permalink
2017-03-16 1750
Browse files Browse the repository at this point in the history
  • Loading branch information
allanmcarvalho committed Mar 16, 2017
1 parent 9a8406a commit 26c8f94
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 12 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ class ExamplesTable extends Table
- **y:** (optional) The crop image y position based from top. Default: **Center**;
- **format:** Image format. It can be (`jpg`, `png`, `gif`, `same` (same as the original)). Default: `jpg`;
- **quality:** Image quality from 1 to 100. Default: `100`;
- **preserve_animation:** When the image is gif, its animation is not frozen. No changes (thubnails, crop ...) will be made in this image.. Default: `false`;
- **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 Down
43 changes: 34 additions & 9 deletions src/File/Writer/ImageWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
use Cake\ORM\Entity;
use Cake\Utility\Hash;
use Upload\File\Writer\Traits\ImageTrait;
use Cake\Filesystem\File;


/**
* Description of DefaultWriter
Expand Down Expand Up @@ -65,6 +67,12 @@ class ImageWriter extends DefaultWriter
*/
private $crop_y = false;

/**
* Preserves the animation when extension is gif
* @var bool
*/
private $preserveGifAnimation = false;

/**
* value of watermark file path
* @var string
Expand Down Expand Up @@ -114,6 +122,7 @@ public function __construct(Table $table, Entity $entity, $field, $settings)
$this->crop_width = Hash::get($this->settings, 'image.crop.width', false);
$this->crop_x = Hash::get($this->settings, 'image.crop.x', null);
$this->crop_y = Hash::get($this->settings, 'image.crop.y', null);
$this->preserveGifAnimation = Hash::get($this->settings, 'image.preserve_animation', false);
$this->watermark = Hash::get($this->settings, 'image.watermark.path', false);
$this->watermark_position = Hash::get($this->settings, 'image.watermark.position', 'bottom-right');
$this->watermark_opacity = Hash::get($this->settings, 'image.watermark.opacity', 100);
Expand All @@ -133,17 +142,33 @@ public function write()
$this->createFilename(true);
}

$image = $this->getImage($this->fileInfo['tmp_name']);

$this->modifyImage($image);

if ($image->save("{$this->getPath()}{$this->getFilename()}", $this->getConfigImageQuality()))
if ($this->preserveGifAnimation === true and $this->getConfigFileFormat() == '.gif')
{
return $this->entity->set($this->field, "{$this->getFileName()}");
$file = new File($this->fileInfo['tmp_name'], true);

if ($file->copy("{$this->getPath()}{$this->getFilename()}"))
{
return $this->entity->set($this->field, "{$this->getFileName()}");
} else
{
\Cake\Log\Log::error(__d('upload', 'Unable to salve image "{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()));
return false;
}
} else
{
\Cake\Log\Log::error(__d('upload', 'Unable to salve image "{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()));
return false;
$image = $this->getImage($this->fileInfo['tmp_name']);

$this->modifyImage($image);


if ($image->save("{$this->getPath()}{$this->getFilename()}", $this->getConfigImageQuality()))
{
return $this->entity->set($this->field, "{$this->getFileName()}");
} else
{
\Cake\Log\Log::error(__d('upload', 'Unable to salve image "{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()));
return false;
}
}
}

Expand Down Expand Up @@ -191,7 +216,7 @@ public function deleteThubnails($path, $filename)
$result = true;
foreach ($this->thumbnails as $thumbnail)
{
$label = Hash::get($thumbnail, 'label', false);
$label = Hash::get($thumbnail, 'label', false);

if ($label === false)
{
Expand Down
6 changes: 3 additions & 3 deletions src/Validation/Traits/ImageTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,8 @@ public static function isBelowMaxHeight($check, $height)
return $image->height() > $height ? false : true;
}

/**

/**
* Check that the file has exact width requirement
*
* @param mixed $check Value to check
Expand Down Expand Up @@ -158,8 +159,7 @@ public static function isThisHeight($check, $height)
return false;
}
$image = self::getImage($check['tmp_name']);

return $image->height() == $height ? false : true;
return $image->height() == $height ? true : false;
}

/**
Expand Down

0 comments on commit 26c8f94

Please sign in to comment.