diff --git a/inc/databap.php b/inc/databap.php index 7a153fd..3f0fbda 100755 --- a/inc/databap.php +++ b/inc/databap.php @@ -122,7 +122,7 @@ class Databap extends PhpObject const REBOOT_DELAY = 15; //seconds const PM_SEP = '___'; const CHAT_IMG_MAX_WIDTH = 700; - const CHAT_IMG_MAX_HEIGHT = 1080; + const CHAT_IMG_MAX_HEIGHT = 800; const JSON_PREFIX = '[\-JSON-/]'; const MAX_NB_NEWS = 3; diff --git a/inc/toolbox.php b/inc/toolbox.php index 1bdffce..7c63fa2 100644 --- a/inc/toolbox.php +++ b/inc/toolbox.php @@ -162,6 +162,22 @@ class ToolBox return $sMimetype; } + public static function isAnimatedGif($sFilePath) + { + if(!($oFile = @fopen($sFilePath, 'rb'))) return false; + + $iCount = 0; + while (!feof($oFile) && $iCount < 2) + { + + $sChunk = fread($oFile, 1024 * 100); //read 100kb at a time + $iCount += preg_match_all('#\x00\x21\xF9\x04.{4}\x00(\x2C|\x21)#s', $sChunk, $asMatches); + } + fclose($oFile); + + return $iCount > 1; + } + public static function createThumbnail($sInPath, $iMaxWidth, $iMaxHeight, $sOutPath='', $bDeleteIn=false, $asImageExts=array('jpg', 'jpeg', 'gif', 'png'), $bCrop=false) { $asResult = array('error'=>''); @@ -221,19 +237,34 @@ class ToolBox $iThumbWidth = $iResizedWidth; $iThumbHeight = $iResizedHeight; } - - //create image from source - $oSource = call_user_func('imagecreatefrom'.$sImageExt, $sInPath); - - //Resize - $oThumb = imagecreatetruecolor($iThumbWidth, $iThumbHeight); - imagecopyresampled($oThumb, $oSource, $iPosLeft, $iPosTop, 0, 0, $iResizedWidth, $iResizedHeight, $iWidth, $iHeight); - - //Save - if(file_exists($sOutPath)) unlink($sOutPath); - if(!call_user_func_array('image'.$sImageExt, array($oThumb, $sOutPath))) + + if($sImageExt=='gif' && self::isAnimatedGif($sInPath)) { - $asResult['error'] = 'Unable to create thumbnail : '.$sOutPath; + $sContent = file_get_contents($sInPath); + $sBigGifPath = Databap::DOC_TMP_FOLDER.uniqid(); + $sCoalescePath = Databap::DOC_TMP_FOLDER.uniqid(); + + file_put_contents($sBigGifPath, $sContent); + system('convert '.$sBigGifPath.' -coalesce '.$sCoalescePath); + system('convert -size '.$iWidth.'x'.$iHeight.' '.$sCoalescePath.' -resize '.$iResizedWidth.'x'.$iResizedHeight.' '.$sOutPath); + unlink($sBigGifPath); + unlink($sCoalescePath); + } + else + { + //create image from source + $oSource = call_user_func('imagecreatefrom'.$sImageExt, $sInPath); + + //Resize + $oThumb = imagecreatetruecolor($iThumbWidth, $iThumbHeight); + imagecopyresampled($oThumb, $oSource, $iPosLeft, $iPosTop, 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; + } } } elseif($sInPath != $sOutPath)