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
$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'].')');
}
}

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

View File

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