From b1e47ea8554265d482ecb32d2550b9a3e37ec6bf Mon Sep 17 00:00:00 2001 From: franzz Date: Mon, 26 Aug 2013 22:03:32 +0200 Subject: [PATCH] do not resize image if not necessary --- config.php | 55 +++++++++++++++++++++++++++++++++--------------------- 1 file changed, 34 insertions(+), 21 deletions(-) mode change 100644 => 100755 config.php diff --git a/config.php b/config.php old mode 100644 new mode 100755 index ab08237..9089f6e --- a/config.php +++ b/config.php @@ -569,8 +569,19 @@ class Databap extends PhpObject //Image $oImages = $oPost->getElementsByTagName('img'); - foreach($oImages as $oImage) {if($oImage->getAttribute('class') == 'badge-item-img') break;} - $asResult = $this->downloadToTmp($oImage->getAttribute('src')); + foreach($oImages as $oImage) + { + switch($oImage->getAttribute('class')) + { + case 'badge-item-animated-img': + $o9gagImage = $oImage; + break 2; + case 'badge-item-img': + $o9gagImage = $oImage; + break; + } + } + $asResult = $this->downloadToTmp($o9gagImage->getAttribute('src')); if($asResult['error']=='') { @@ -4355,8 +4366,9 @@ class ToolBox public static function createThumbnail($sInPath, $iMaxWidth, $iMaxHeight, $sOutPath='', $bDeleteIn=false) { $asResult = array('error'=>''); - + //Look up the extension to choose the image creator + //TODO use MIME types $sInInfo = pathinfo($sInPath); $sInName = strtolower($sInInfo['basename']); $sImageExt = strtolower($sInInfo['extension']); @@ -4365,7 +4377,7 @@ class ToolBox //New Destination folder if($sOutPath=='') $sOutPath = $sInPath; elseif(substr($sOutPath, -1)=='/') $sOutPath .= $sInName; - + //New sizes if(in_array($sImageExt, Databap::$UPLOAD_IMG_EXTS)) { @@ -4382,33 +4394,34 @@ class ToolBox $iResizedHeight = $iMaxHeight; $iResizedWidth = ($iResizedHeight / $iHeight) * $iWidth; } + + //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); + + //Save + if(file_exists($sOutPath)) unlink($sOutPath); + if(!call_user_func_array('image'.$sImageExt, array($oThumb, $sOutPath))) + { + $asResult['error'] = 'Unable to create thumbnail : '.$sOutPath; + } } - else + elseif($sInPath != $sOutPath) { $iResizedWidth = $iWidth; $iResizedHeight = $iHeight; + if(!copy($sInPath, $sOutPath)) $asResult['error'] = 'Copy failed from '.$sInPath.' to '.$sOutPath; } $asResult['width'] = $iResizedWidth; $asResult['height'] = $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); - - //Save - if(file_exists($sOutPath)) unlink($sOutPath); - if(!call_user_func_array('image'.$sImageExt, array($oThumb, $sOutPath))) - { - $asResult['error'] = 'Unable to create thumbnail : '.$sOutPath; - } $asResult['out'] = $sOutPath; } else $asResult['error'] = 'Wrong file type'; - if($bDeleteIn && $asResult['error']=='') unlink($sInPath); + if($bDeleteIn && $asResult['error']=='' && $sInPath != $sOutPath) unlink($sInPath); return $asResult; } @@ -4447,7 +4460,7 @@ function pre($sText, $sMode='return', $bDie=false, $sTitle='Test') function dlog($sText, $sTitle='Test') { - pre($sText, date('d/m/Y H:m:i').' - '.$sTitle, false, 'log'); + pre($sText, 'log', false, date('d/m/Y H:m:i').' - '.$sTitle, false); } ?> \ No newline at end of file