Adding to SP3 : fixing images and others

This commit is contained in:
2014-11-30 17:24:49 +01:00
parent b6ebf33a02
commit c1f4308089
9 changed files with 159 additions and 115 deletions

View File

@@ -130,20 +130,48 @@ class ToolBox
return json_encode($asData);
}
public static function getMimeType($sPath, $bSubTypeOnly=false)
{
//Retrieving file Content Type
if(file_exists($sPath)) //Local
{
$oFileInfo = new finfo(FILEINFO_MIME);
$sMimetype = $oFileInfo->file($sPath);
}
else //Remote
{
$oCurl = curl_init($sPath);
curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($oCurl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($oCurl, CURLOPT_HEADER, true);
curl_setopt($oCurl, CURLOPT_NOBODY, true);
curl_exec($oCurl);
$sMimetype = curl_getinfo($oCurl, CURLINFO_CONTENT_TYPE);
}
//Only sub type (after /)
if($bSubTypeOnly)
{
preg_match('`\/(?P<type>\w+)(\;|$)`ui', mb_strtolower($sMimetype), $asMatch);
$sMimetype = $asMatch['type'];
}
return $sMimetype;
}
public static function createThumbnail($sInPath, $iMaxWidth, $iMaxHeight, $sOutPath='', $bDeleteIn=false, $asImageExts=array('jpg', 'jpeg', 'gif', 'png'), $bCrop=false)
{
$asResult = array('error'=>'');
//Look up the extension to choose the image creator
//TODO use MIME types
$sInInfo = pathinfo($sInPath);
$sInName = mb_strtolower($sInInfo['basename']);
$sImageExt = mb_strtolower($sInInfo['extension']);
//Look up the file type to choose the image creator
$sImageExt = self::getMimeType($sInPath, true);
$sImageExt = ($sImageExt=='jpg')?'jpeg':$sImageExt;
//New Destination folder
$asInInfo = pathinfo($sInPath);
$asOutInfo = pathinfo($sOutPath);
if($sOutPath=='') $sOutPath = $sInPath;
elseif(mb_substr($sOutPath, -1)=='/') $sOutPath .= $sInName;
elseif(mb_substr($sOutPath, -1)=='/') $sOutPath .= mb_strtolower($asInInfo['basename']); //folder only, keep original name
elseif(!array_key_exists('extension', $asOutInfo)) $sOutPath .= '.'.$sImageExt; //folder + filename, but getting ext from file info
//New sizes
if(in_array($sImageExt, $asImageExts))