fix animated gif resizing

This commit is contained in:
2015-05-25 16:29:31 +02:00
parent b557ded1a6
commit 13fc4d4ef9
2 changed files with 44 additions and 13 deletions

View File

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

View File

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