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

@@ -190,7 +190,7 @@ class Procedure extends PhpObject
} }
//Thumbnail picture //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'].')'); if($asResult['error']!='') $this->addError('Unable to create thumbnail : '.$asResult['out']. '('.$asResult['error'].')');
} }
} }

View File

@@ -130,7 +130,7 @@ class ToolBox
return json_encode($asData); 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'=>''); $asResult = array('error'=>'');
@@ -153,6 +153,28 @@ class ToolBox
{ {
$dResizeDeltaWidth = $iWidth - $iMaxWidth; $dResizeDeltaWidth = $iWidth - $iMaxWidth;
$dResizeDeltaHeight = $iHeight - $iMaxHeight; $dResizeDeltaHeight = $iHeight - $iMaxHeight;
$iPosLeft = $iPosTop = 0;
$iThumbWidth = $iMaxWidth;
$iThumbHeight = $iMaxHeight;
//Max up the lowest value between height and width and crop the other
if($bCrop)
{
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 //Just resize
{
if($dResizeDeltaWidth > $dResizeDeltaHeight) if($dResizeDeltaWidth > $dResizeDeltaHeight)
{ {
$iResizedWidth = $iMaxWidth; $iResizedWidth = $iMaxWidth;
@@ -163,13 +185,16 @@ class ToolBox
$iResizedHeight = $iMaxHeight; $iResizedHeight = $iMaxHeight;
$iResizedWidth = ($iResizedHeight / $iHeight) * $iWidth; $iResizedWidth = ($iResizedHeight / $iHeight) * $iWidth;
} }
$iThumbWidth = $iResizedWidth;
$iThumbHeight = $iResizedHeight;
}
//create image from source //create image from source
$oSource = call_user_func('imagecreatefrom'.$sImageExt, $sInPath); $oSource = call_user_func('imagecreatefrom'.$sImageExt, $sInPath);
//Resize //Resize
$oThumb = imagecreatetruecolor($iResizedWidth, $iResizedHeight); $oThumb = imagecreatetruecolor($iThumbWidth, $iThumbHeight);
imagecopyresized($oThumb, $oSource, 0, 0, 0, 0, $iResizedWidth, $iResizedHeight, $iWidth, $iHeight); imagecopyresampled($oThumb, $oSource, $iPosLeft, $iPosTop, 0, 0, $iResizedWidth, $iResizedHeight, $iWidth, $iHeight);
//Save //Save
if(file_exists($sOutPath)) unlink($sOutPath); if(file_exists($sOutPath)) unlink($sOutPath);

View File

@@ -275,17 +275,17 @@ function setThumbnails(bShow)
function getImageUrl(sImageName, bThumbnail) function getImageUrl(sImageName, bThumbnail)
{ {
bThumbnail = bThumbnail || false;
//Stored image
var sImageUrl = (bThumbnail?databap.consts.image_folder_thumb:databap.consts.image_folder)+sImageName; var sImageUrl = (bThumbnail?databap.consts.image_folder_thumb:databap.consts.image_folder)+sImageName;
if(file_exists(sImageUrl)) if(!file_exists(sImageUrl))
{ {
return sImageUrl; //Temp image
}
sImageUrl = databap.consts.image_folder_tmp+sImageName; sImageUrl = databap.consts.image_folder_tmp+sImageName;
if(file_exists(sImageUrl)) if(!file_exists(sImageUrl)) sImageUrl = '';
{
return sImageUrl;
} }
return ''; return sImageUrl;
} }
function addStep(stepId, sPosition, sDesc) function addStep(stepId, sPosition, sDesc)
@@ -451,9 +451,8 @@ function saveProcedure()
databap.addSuccessIcon(); databap.addSuccessIcon();
//setDisplay('read'); //setDisplay('read');
//Unblock exit //Unblock exit & go
databap.tmp('started', false); databap.tmp('started', false);
databap.goToInternalLink('proc', proc_info.proc_id); databap.goToInternalLink('proc', proc_info.proc_id);
} }
else else