Skip to content

Commit

Permalink
Compatibility with cake 3.6.x and improviments
Browse files Browse the repository at this point in the history
  • Loading branch information
allanmcarvalho committed Mar 17, 2018
1 parent 92f06d8 commit 8d6aeaa
Show file tree
Hide file tree
Showing 9 changed files with 95 additions and 69 deletions.
25 changes: 14 additions & 11 deletions src/File/Writer/DefaultWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

namespace Upload\File\Writer;

use Cake\Log\Log;
use Cake\ORM\Table;
use Cake\ORM\Entity;
use Cake\Filesystem\File;
Expand Down Expand Up @@ -74,8 +75,8 @@ abstract class DefaultWriter implements WriterInterface
* Construct Method
* @param Table $table
* @param Entity $entity
* @param type $field
* @param type $settings
* @param string $field
* @param array $settings
*/
public function __construct(Table $table, Entity $entity, $field, $settings)
{
Expand All @@ -89,7 +90,8 @@ public function __construct(Table $table, Entity $entity, $field, $settings)

/**
* Delete a file from path
* @param string $PathAndFilename
* @param $path
* @param $filename
* @return boolean
*/
protected function _delete($path, $filename)
Expand All @@ -99,19 +101,20 @@ protected function _delete($path, $filename)
{
if (!$file->delete())
{
\Cake\Log\Log::error(__d('upload', 'Unable to delete file "{0}" in entity id "{1}" from table "{2}" and path "{3}"', $filename, $this->entity->get($this->table->getPrimaryKey()), $this->table->getTable(), $path));
Log::error(__d('upload', 'Unable to delete file "{0}" in entity id "{1}" from table "{2}" and path "{3}"', $filename, $this->entity->get($this->table->getPrimaryKey()), $this->table->getTable(), $path));
return false;
}
} else
{
\Cake\Log\Log::error(__d('upload', 'Unable to delete file "{0}" in entity id "{1}" from table "{2}" and path "{3}" because it does not exist', $filename, $this->entity->get($this->table->getPrimaryKey()), $this->table->getTable(), $path));
Log::error(__d('upload', 'Unable to delete file "{0}" in entity id "{1}" from table "{2}" and path "{3}" because it does not exist', $filename, $this->entity->get($this->table->getPrimaryKey()), $this->table->getTable(), $path));
return false;
}
return true;
}

/**
* Get a path to save file
* @param null|string $subDirectory
* @return string
*/
protected function getPath($subDirectory = null)
Expand Down Expand Up @@ -146,13 +149,13 @@ protected function createFolderIfItNotExists($path)
{
if (!new Folder($path, true))
{
\Cake\Log\Log::error(__d('upload', 'Unable to create directory: {0}', $path));
Log::error(__d('upload', 'Unable to create directory: {0}', $path));
}
}

/**
* get a image save format from behavior config
* @return type
* @return string
*/
protected function getConfigFileFormat()
{
Expand Down Expand Up @@ -188,13 +191,13 @@ protected function createFilename($ifExistCreateNew = false)
if ($this->filename === null)
{
$filePrefix = Hash::get($this->settings, 'prefix', '');
$fileUniqidMoreEntropy = Hash::get($this->settings, 'more_entropy', true);
$this->filename = Hash::get($this->settings, 'filename', uniqid($filePrefix, $fileUniqidMoreEntropy)) . $this->getConfigFileFormat();
$fileUniqueMoreEntropy = Hash::get($this->settings, 'more_entropy', true);
$this->filename = Hash::get($this->settings, 'filename', uniqid($filePrefix, $fileUniqueMoreEntropy)) . $this->getConfigFileFormat();
} elseif ($ifExistCreateNew === true)
{
$filePrefix = Hash::get($this->settings, 'prefix', '');
$fileUniqidMoreEntropy = Hash::get($this->settings, 'more_entropy', true);
$this->filename = Hash::get($this->settings, 'filename', uniqid($filePrefix, $fileUniqidMoreEntropy)) . $this->getConfigFileFormat();
$fileUniqueMoreEntropy = Hash::get($this->settings, 'more_entropy', true);
$this->filename = Hash::get($this->settings, 'filename', uniqid($filePrefix, $fileUniqueMoreEntropy)) . $this->getConfigFileFormat();
}
return $this->filename;
}
Expand Down
13 changes: 7 additions & 6 deletions src/File/Writer/FileWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

namespace Upload\File\Writer;

use Cake\Log\Log;
use Cake\ORM\Table;
use Cake\ORM\Entity;
use Cake\Filesystem\File;
Expand All @@ -30,8 +31,8 @@ public function __construct(Table $table, Entity $entity, $field, $settings)

/**
* write a file
* @return boolean
*/
* @return boolean|string
*/
public function write()
{
if (!$this->entity->isNew())
Expand All @@ -47,19 +48,19 @@ public function write()
return $this->entity->set($this->field, "{$this->getFileName()}");
} else
{
\Cake\Log\Log::error(__d('upload', 'Unable to salve file "{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()));
Log::error(__d('upload', 'Unable to salve file "{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;
}
}

/**
* Delete method that delete primary and thumbnails images
* @param bool $isUptade
* @param bool $isUpdate
* @return bool
*/
public function delete($isUptade = false)
public function delete($isUpdate = false)
{
if($isUptade === false)
if($isUpdate === false)
{
$entity = &$this->entity;
}else
Expand Down
38 changes: 21 additions & 17 deletions src/File/Writer/ImageWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

namespace Upload\File\Writer;

use Cake\Log\Log;
use Cake\ORM\Table;
use Cake\ORM\Entity;
use Cake\Utility\Hash;
Expand All @@ -29,7 +30,7 @@ class ImageWriter extends DefaultWriter
* value of resize height
* @var int
*/
private $resize_heigth = false;
private $resize_height = false;

/**
* value of resize width
Expand All @@ -47,7 +48,7 @@ class ImageWriter extends DefaultWriter
* value of crop height
* @var int
*/
private $crop_heigth = false;
private $crop_height = false;

/**
* value of crop width
Expand Down Expand Up @@ -107,18 +108,18 @@ class ImageWriter extends DefaultWriter
* Constructor method
* @param Table $table
* @param Entity $entity
* @param type $field
* @param type $settings
* @param string $field
* @param array $settings
*/
public function __construct(Table $table, Entity $entity, $field, $settings)
{
parent::__construct($table, $entity, $field, $settings);
$this->defaultPath = WWW_ROOT . 'img' . DS . $this->table->getAlias() . DS;

$this->resize_heigth = Hash::get($this->settings, 'image.resize.height', false);
$this->resize_height = 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_height = 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);
$this->crop_y = Hash::get($this->settings, 'image.crop.y', null);
Expand All @@ -132,7 +133,7 @@ public function __construct(Table $table, Entity $entity, $field, $settings)

/**
* write a image
* @return boolean
* @return boolean|string
*/
public function write()
{
Expand All @@ -151,7 +152,7 @@ public function write()
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()));
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
Expand All @@ -166,19 +167,20 @@ public function write()
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()));
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;
}
}
}

/**
* delete images
* @param bool $isUptade
* @param bool $isUpdate
* @return bool
*/
public function delete($isUptade = false)
public function delete($isUpdate = false)
{
if ($isUptade === false)
if ($isUpdate === false)
{
$entity = &$this->entity;
} else
Expand All @@ -204,7 +206,9 @@ public function delete($isUptade = false)

/**
* Delete image thumbnails
* @param \Intervention\Image\Image $image
* @param $path
* @param $filename
* @return bool
*/
public function deleteThubnails($path, $filename)
{
Expand Down Expand Up @@ -234,13 +238,13 @@ public function deleteThubnails($path, $filename)
/**
* Modifier function calls
* @param \Intervention\Image\Image $image
* @return \Intervention\Image\Image
* @return void
*/
private function modifyImage($image)
{
$this->resize($image, $this->resize_width, $this->resize_heigth, $this->resize_min_size);
$this->resize($image, $this->resize_width, $this->resize_height, $this->resize_min_size);

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

$this->createThumbnails();

Expand All @@ -252,7 +256,7 @@ private function modifyImage($image)

/**
* get a image quality from behavior config
* @return type
* @return int
*/
private function getConfigImageQuality()
{
Expand Down
55 changes: 32 additions & 23 deletions src/File/Writer/Traits/ImageTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

namespace Upload\File\Writer\Traits;

use Cake\Log\Log;
use Intervention\Image\ImageManager;
use Intervention\Image\Image;
use Cake\Utility\Hash;
Expand Down Expand Up @@ -40,11 +41,12 @@ protected function getImage($path)
}

/**
* Resize a imagem based in
* Resize a imagem based in
* @param Image $image
* @param mixed $width must by smaller than the original
* @param mixed $height must by smaller than the original
* @return Image
* @param $minSize
* @return void
*/
protected function resize($image, $width, $height, $minSize)
{
Expand Down Expand Up @@ -87,7 +89,9 @@ protected function resize($image, $width, $height, $minSize)
* @param Image $image
* @param mixed $width
* @param mixed $height
* @return Image
* @param $x
* @param $y
* @return void
*/
protected function crop($image, $width, $height, $x, $y)
{
Expand Down Expand Up @@ -126,32 +130,32 @@ protected function crop($image, $width, $height, $x, $y)
* @param \Intervention\Image\Image $image
* @param string $path
* @param string $position
* @return \Intervention\Image\Image
* @param $opacity
* @return void
*/
protected function insertWatermark($image, $path, $position, $opacity)
{
$watermark = $this->getImage($path);
$targetHeight = intval($image->height() * 0.07);
$targetWarthermarkHeight = $targetHeight <= $watermark->height() ? $targetHeight : $watermark->height();
$targetWarthermarkWidth = $this->getEquivalentResizeWidth($watermark, $targetWarthermarkHeight);
$targetWatermarkHeight = $targetHeight <= $watermark->height() ? $targetHeight : $watermark->height();
$targetWatermarkWidth = $this->getEquivalentResizeWidth($watermark, $targetWatermarkHeight);

if ($targetWarthermarkWidth > $image->width())
if ($targetWatermarkWidth > $image->width())
{
$targetWidth = intval($image->width() * 0.8);
$targetWarthermarkWidth = $targetWidth <= $watermark->width() ? $targetWidth : $watermark->width();
$targetWarthermarkHeight = $this->getEquivalentResizeHeight($image, $targetWarthermarkWidth);
$targetWatermarkWidth = $targetWidth <= $watermark->width() ? $targetWidth : $watermark->width();
$targetWatermarkHeight = $this->getEquivalentResizeHeight($image, $targetWatermarkWidth);
}

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

$watermark->opacity($opacity);

$image->insert($watermark, $position, round($image->width() * 0.05), round($image->height() * 0.05));
}

/**
*
* @param \Intervention\Image\Image $thumbnailImage
*
* @return boolean
*/
protected function createThumbnails()
Expand Down Expand Up @@ -227,7 +231,7 @@ protected function createThumbnails()
$newThumbnail->interlace(true);
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()));
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()));
}
unset($newThumbnail);
}
Expand All @@ -252,22 +256,27 @@ private function greatestCommonDivisor($dividend_a, $dividend_b)
*/
private function getEquivalentResizeWidth($image, $newImageHeight)
{
$imageWidht = $image->width();
$imageWidth = $image->width();
$imageHeight = $image->height();
$gcd = round($this->greatestCommonDivisor($imageWidht, $imageHeight));
$widthRadio = round($imageWidht / $gcd);
$heigthRadio = round($imageHeight / $gcd);
return round(($newImageHeight / $heigthRadio) * $widthRadio);
$gcd = round($this->greatestCommonDivisor($imageWidth, $imageHeight));
$widthRadio = round($imageWidth / $gcd);
$heightRadio = round($imageHeight / $gcd);
return round(($newImageHeight / $heightRadio) * $widthRadio);
}

/**
* @param Image $image
* @param $newImageWidth
* @return float
*/
private function getEquivalentResizeHeight($image, $newImageWidth)
{
$imageWidht = $image->width();
$imageWidth = $image->width();
$imageHeight = $image->height();
$gcd = round($this->greatestCommonDivisor($imageWidht, $imageHeight));
$widthRadio = round($imageWidht / $gcd);
$heigthRadio = round($imageHeight / $gcd);
return round(($newImageWidth / $widthRadio) * $heigthRadio);
$gcd = round($this->greatestCommonDivisor($imageWidth, $imageHeight));
$widthRadio = round($imageWidth / $gcd);
$heightRadio = round($imageHeight / $gcd);
return round(($newImageWidth / $widthRadio) * $heightRadio);
}

}
Loading

0 comments on commit 8d6aeaa

Please sign in to comment.