fix animated gif resizing
This commit is contained in:
@@ -122,7 +122,7 @@ class Databap extends PhpObject
|
|||||||
const REBOOT_DELAY = 15; //seconds
|
const REBOOT_DELAY = 15; //seconds
|
||||||
const PM_SEP = '___';
|
const PM_SEP = '___';
|
||||||
const CHAT_IMG_MAX_WIDTH = 700;
|
const CHAT_IMG_MAX_WIDTH = 700;
|
||||||
const CHAT_IMG_MAX_HEIGHT = 1080;
|
const CHAT_IMG_MAX_HEIGHT = 800;
|
||||||
const JSON_PREFIX = '[\-JSON-/]';
|
const JSON_PREFIX = '[\-JSON-/]';
|
||||||
const MAX_NB_NEWS = 3;
|
const MAX_NB_NEWS = 3;
|
||||||
|
|
||||||
|
|||||||
@@ -162,6 +162,22 @@ class ToolBox
|
|||||||
return $sMimetype;
|
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)
|
public static function createThumbnail($sInPath, $iMaxWidth, $iMaxHeight, $sOutPath='', $bDeleteIn=false, $asImageExts=array('jpg', 'jpeg', 'gif', 'png'), $bCrop=false)
|
||||||
{
|
{
|
||||||
$asResult = array('error'=>'');
|
$asResult = array('error'=>'');
|
||||||
@@ -221,19 +237,34 @@ class ToolBox
|
|||||||
$iThumbWidth = $iResizedWidth;
|
$iThumbWidth = $iResizedWidth;
|
||||||
$iThumbHeight = $iResizedHeight;
|
$iThumbHeight = $iResizedHeight;
|
||||||
}
|
}
|
||||||
|
|
||||||
//create image from source
|
if($sImageExt=='gif' && self::isAnimatedGif($sInPath))
|
||||||
$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;
|
$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)
|
elseif($sInPath != $sOutPath)
|
||||||
|
|||||||
Reference in New Issue
Block a user