improve image resizing (thumbnail) and add crop option

This commit is contained in:
2014-09-06 13:32:33 +02:00
parent 5660522c3e
commit f3bd550c28
3 changed files with 44 additions and 20 deletions

View File

@@ -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);