From f3bd550c28b4766ff742a8cf9280ea5551c0bffd Mon Sep 17 00:00:00 2001 From: franzz Date: Sat, 6 Sep 2014 13:32:33 +0200 Subject: [PATCH] improve image resizing (thumbnail) and add crop option --- inc/procedure.php | 2 +- inc/toolbox.php | 43 ++++++++++++++++++++++++++++++++++--------- masks/procedure.html | 19 +++++++++---------- 3 files changed, 44 insertions(+), 20 deletions(-) diff --git a/inc/procedure.php b/inc/procedure.php index 217ca3a..9558d6f 100644 --- a/inc/procedure.php +++ b/inc/procedure.php @@ -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'].')'); } } diff --git a/inc/toolbox.php b/inc/toolbox.php index c1c27da..50c1968 100644 --- a/inc/toolbox.php +++ b/inc/toolbox.php @@ -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); diff --git a/masks/procedure.html b/masks/procedure.html index 07eb0cb..14945fe 100755 --- a/masks/procedure.html +++ b/masks/procedure.html @@ -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