improve image resizing (thumbnail) and add crop option
This commit is contained in:
@@ -190,7 +190,7 @@ class Procedure extends PhpObject
|
||||
}
|
||||
|
||||
//Thumbnail picture
|
||||
$asResult = ToolBox::createThumbnail($sFilePath, self::THUMB_MAX_SIZE, self::THUMB_MAX_SIZE, self::IMAGE_FOLDER_THUMB, false, Databap::$UPLOAD_IMG_EXTS);
|
||||
$asResult = ToolBox::createThumbnail($sFilePath, self::THUMB_MAX_SIZE, self::THUMB_MAX_SIZE, self::IMAGE_FOLDER_THUMB, false, Databap::$UPLOAD_IMG_EXTS, true);
|
||||
if($asResult['error']!='') $this->addError('Unable to create thumbnail : '.$asResult['out']. '('.$asResult['error'].')');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -130,7 +130,7 @@ class ToolBox
|
||||
return json_encode($asData);
|
||||
}
|
||||
|
||||
public static function createThumbnail($sInPath, $iMaxWidth, $iMaxHeight, $sOutPath='', $bDeleteIn=false, $asImageExts=array('jpg', 'jpeg', 'gif', 'png'))
|
||||
public static function createThumbnail($sInPath, $iMaxWidth, $iMaxHeight, $sOutPath='', $bDeleteIn=false, $asImageExts=array('jpg', 'jpeg', 'gif', 'png'), $bCrop=false)
|
||||
{
|
||||
$asResult = array('error'=>'');
|
||||
|
||||
@@ -153,23 +153,48 @@ class ToolBox
|
||||
{
|
||||
$dResizeDeltaWidth = $iWidth - $iMaxWidth;
|
||||
$dResizeDeltaHeight = $iHeight - $iMaxHeight;
|
||||
if($dResizeDeltaWidth > $dResizeDeltaHeight)
|
||||
$iPosLeft = $iPosTop = 0;
|
||||
$iThumbWidth = $iMaxWidth;
|
||||
$iThumbHeight = $iMaxHeight;
|
||||
|
||||
//Max up the lowest value between height and width and crop the other
|
||||
if($bCrop)
|
||||
{
|
||||
$iResizedWidth = $iMaxWidth;
|
||||
$iResizedHeight = ($iResizedWidth / $iWidth) * $iHeight;
|
||||
if($dResizeDeltaWidth > $dResizeDeltaHeight) //Crop width
|
||||
{
|
||||
$iResizedHeight = $iMaxHeight;
|
||||
$iResizedWidth = ($iResizedHeight / $iHeight) * $iWidth;
|
||||
$iPosLeft = ($iResizedWidth - $iMaxWidth)/2*(-1);
|
||||
}
|
||||
else
|
||||
{
|
||||
$iResizedWidth = $iMaxWidth;
|
||||
$iResizedHeight = ($iResizedWidth / $iWidth) * $iHeight;
|
||||
$iPosTop = ($iResizedHeight - $iMaxHeight)/2*(-1);
|
||||
}
|
||||
}
|
||||
else
|
||||
else //Just resize
|
||||
{
|
||||
$iResizedHeight = $iMaxHeight;
|
||||
$iResizedWidth = ($iResizedHeight / $iHeight) * $iWidth;
|
||||
if($dResizeDeltaWidth > $dResizeDeltaHeight)
|
||||
{
|
||||
$iResizedWidth = $iMaxWidth;
|
||||
$iResizedHeight = ($iResizedWidth / $iWidth) * $iHeight;
|
||||
}
|
||||
else
|
||||
{
|
||||
$iResizedHeight = $iMaxHeight;
|
||||
$iResizedWidth = ($iResizedHeight / $iHeight) * $iWidth;
|
||||
}
|
||||
$iThumbWidth = $iResizedWidth;
|
||||
$iThumbHeight = $iResizedHeight;
|
||||
}
|
||||
|
||||
//create image from source
|
||||
$oSource = call_user_func('imagecreatefrom'.$sImageExt, $sInPath);
|
||||
|
||||
//Resize
|
||||
$oThumb = imagecreatetruecolor($iResizedWidth, $iResizedHeight);
|
||||
imagecopyresized($oThumb, $oSource, 0, 0, 0, 0, $iResizedWidth, $iResizedHeight, $iWidth, $iHeight);
|
||||
$oThumb = imagecreatetruecolor($iThumbWidth, $iThumbHeight);
|
||||
imagecopyresampled($oThumb, $oSource, $iPosLeft, $iPosTop, 0, 0, $iResizedWidth, $iResizedHeight, $iWidth, $iHeight);
|
||||
|
||||
//Save
|
||||
if(file_exists($sOutPath)) unlink($sOutPath);
|
||||
|
||||
Reference in New Issue
Block a user