Skip to content

Commit

Permalink
fix code and new arguments in method generateThumb
Browse files Browse the repository at this point in the history
  • Loading branch information
dmitriitux committed Jan 15, 2021
1 parent 8f7f8d0 commit 83e5959
Showing 1 changed file with 55 additions and 45 deletions.
100 changes: 55 additions & 45 deletions jinterventionimage.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,30 +32,36 @@ public static function getInstance($options = ['driver' => 'gd'])


/**
* @param $source
* @param string $algorithm
* @param null $thumb_path
* @param $maxWidth
* @param $maxHeight
* @param string $source
* @param int $max_width
* @param int $max_height
* @param string $algorithm values: fit|bestfit|resize
* @param string $thumb_path example: cache/images
* @param string $how_save values: folder|file
*
* @return mixed|string|string[]
* @return mixed|string
*
* @since version
*/
public static function generateThumb($source, $maxWidth, $maxHeight, $algorithm = 'resize', $thumb_path = null)
public static function generateThumb($source, $max_width, $max_height, $algorithm = 'resize', $thumb_path = null, $how_save = 'file')
{
$source = str_replace(JPATH_ROOT . DIRECTORY_SEPARATOR, '', $source);
$paths = explode(DIRECTORY_SEPARATOR, $source);
$file = array_pop($paths);
$fileSplit = explode('.', $file);
$fileExt = mb_strtolower(array_pop($fileSplit));
$file_split = explode('.', $file);
$file_ext = mb_strtolower(array_pop($file_split));
$extAccept = ['jpg', 'jpeg', 'png', 'gif', 'webp'];

if(!in_array($fileExt, $extAccept))
if(!in_array($file_ext, $extAccept))
{
return $file;
}

if($how_save === 'file')
{
$file = implode('.', $file_split) . '_' . $max_width . '_' . $max_height . '.' . $file_ext;
}

if($thumb_path === null)
{
$pathThumb = implode(DIRECTORY_SEPARATOR, array_merge($paths, ['_thumb']));
Expand All @@ -67,6 +73,12 @@ public static function generateThumb($source, $maxWidth, $maxHeight, $algorithm
$pathFileThumb = Path::clean($thumb_path . DIRECTORY_SEPARATOR . '_thumb' . DIRECTORY_SEPARATOR . $file);
}

if($how_save === 'folder')
{
$pathThumb .= DIRECTORY_SEPARATOR . $max_width . 'x' . $max_height;
$pathFileThumb = Path::clean($pathThumb . DIRECTORY_SEPARATOR . $file);
}

$params = [];

$fullPathThumb = Path::clean(JPATH_ROOT . DIRECTORY_SEPARATOR . $pathThumb . DIRECTORY_SEPARATOR . $file);
Expand Down Expand Up @@ -100,17 +112,17 @@ public static function generateThumb($source, $maxWidth, $maxHeight, $algorithm

if ($algorithm === 'fit')
{
self::fit($fullPathThumb, $maxWidth, $maxHeight);
self::fit($fullPathThumb, $max_width, $max_height);
}

if ($algorithm === 'bestfit')
{
self::bestFit($fullPathThumb, $maxWidth, $maxHeight);
self::bestFit($fullPathThumb, $max_width, $max_height);
}

if ($algorithm === 'resize')
{
self::resize($fullPathThumb, $maxWidth, $maxHeight);
self::resize($fullPathThumb, $max_width, $max_height);
}

}
Expand All @@ -122,68 +134,66 @@ public static function generateThumb($source, $maxWidth, $maxHeight, $algorithm


/**
* @param $file
* @param null $widthFit
* @param null $heightFit
* @param string $file
* @param int $width_fit
* @param int $height_fit
*
*
* @since version
*/
public static function resize($file, $widthFit = null, $heightFit = null)
public static function resize($file, $width_fit, $height_fit)
{
list($width, $height, $type, $attr) = getimagesize($file);
$newWidth = $width;
$newHeight = $height;
$maxWidth = (int)$widthFit;
$maxHeight = (int)$heightFit;
$max_width = (int)$width_fit;
$max_height = (int)$height_fit;

$manager = self::getInstance(['driver' => self::getNameDriver()]);
$manager
->make($file)
->resize($maxWidth, $maxHeight, function ($constraint) {
->resize($max_width, $max_height, function ($constraint) {
$constraint->aspectRatio();
})
->resizeCanvas($maxWidth, $maxHeight)
->resizeCanvas($max_width, $max_height)
->save($file);

}


/**
* @param $file
* @param $widthFit
* @param $heightFit
* @param string $file
* @param int $width_fit
* @param int $height_fit
*
*
* @since version
*/
public static function bestFit($file, $widthFit, $heightFit)
public static function bestFit($file, $width_fit, $height_fit)
{
list($width, $height, $type, $attr) = getimagesize($file);
$newWidth = $width;
$newHeight = $height;
$maxWidth = (int)$widthFit;
$maxHeight = (int)$heightFit;
$new_width = $width;
$new_height = $height;
$max_width = (int)$width_fit;
$max_height = (int)$height_fit;

$ratio = $width / $height;

if($width > $maxWidth)
if($width > $max_width)
{
$newWidth = $maxWidth;
$newHeight = round($newWidth / $ratio);
$new_width = $max_width;
$new_height = round($new_width/$ratio);
}

if($newHeight > $maxHeight)
if($new_height > $max_height)
{
$newHeight = $maxHeight;
$newWidth = round($newHeight * $ratio);
$new_height = $max_height;
$new_width = round($new_height * $ratio);
}


$manager = self::getInstance(['driver' => self::getNameDriver()]);
$manager
->make($file)
->resize($newWidth, $newHeight, function ($constraint) {
->resize($new_width, $new_height, function ($constraint) {
$constraint->aspectRatio();
$constraint->upsize();
})
Expand All @@ -193,25 +203,25 @@ public static function bestFit($file, $widthFit, $heightFit)


/**
* @param $file
* @param $widthFit
* @param $heightFit
* @param string $file
* @param int $width_fit
* @param int $height_fit
*
*
* @since version
*/
public static function fit($file, $widthFit, $heightFit )
public static function fit($file, $width_fit, $height_fit)
{
list($width, $height, $type, $attr) = getimagesize($file);
$newWidth = $width;
$newHeight = $height;
$maxWidth = (int)$widthFit;
$maxHeight = (int)$heightFit;
$max_width = (int)$width_fit;
$max_height = (int)$height_fit;

$manager = self::getInstance(['driver' => self::getNameDriver()]);
$manager
->make($file)
->fit($maxWidth, $maxHeight, function ($constraint) {
->fit($max_width, $max_height, function ($constraint) {
$constraint->aspectRatio();
})
->save($file);
Expand Down

0 comments on commit 83e5959

Please sign in to comment.