New: Function to resize image can also change format

This commit is contained in:
Laurent Destailleur 2010-08-20 19:19:24 +00:00
parent 116243edbb
commit 1e8eb81ec4

View File

@ -256,16 +256,17 @@ function dol_imageResizeOrCrop($file, $mode, $newWidth, $newHeight, $src_x=0, $s
* \param maxHeight Hauteur maximum que dois faire l'image (-1=unchanged, 120 par defaut)
* \param extName Extension pour differencier le nom de la vignette
* \param quality Quality of compression (0=worst, 100=best)
* \param targetformat New format of target (1,2,3,4 or 0 to keep old format)
* \return string Full path of thumb
* \remarks With file=myfile.jpg -> myfile_small.jpg
*/
function vignette($file, $maxWidth = 160, $maxHeight = 120, $extName='_small', $quality=50, $outdir='thumbs')
function vignette($file, $maxWidth = 160, $maxHeight = 120, $extName='_small', $quality=50, $outdir='thumbs', $targetformat=0)
{
require_once(DOL_DOCUMENT_ROOT."/lib/functions2.lib.php");
global $conf,$langs;
dol_syslog("vignette file=".$file." extName=".$extName." maxWidth=".$maxWidth." maxHeight=".$maxHeight." quality=".$quality);
dol_syslog("vignette file=".$file." extName=".$extName." maxWidth=".$maxWidth." maxHeight=".$maxHeight." quality=".$quality." targetformat=".$targetformat);
// Clean parameters
$file=trim($file);
@ -347,23 +348,18 @@ function vignette($file, $maxWidth = 160, $maxHeight = 120, $extName='_small', $
case 1: // Gif
$img = imagecreatefromgif($fichier);
$extImg = '.gif'; // Extension de l'image
$newquality='NU';
break;
case 2: // Jpg
$img = imagecreatefromjpeg($fichier);
$extImg = '.jpg'; // Extension de l'image
$newquality=$quality;
break;
case 3: // Png
$img = imagecreatefrompng($fichier);
$extImg = '.png';
$newquality=$quality-100;
$newquality=round(abs($quality-100)*9/100);
break;
case 4: // Bmp
$img = imagecreatefromwbmp($fichier);
$extImg = '.bmp';
$newquality='NU';
break;
}
@ -387,8 +383,11 @@ function vignette($file, $maxWidth = 160, $maxHeight = 120, $extName='_small', $
$thumbHeight=round($thumbHeight);
$thumbWidth=round($thumbWidth);
// Define target format
if (empty($targetformat)) $targetformat=$infoImg[2];
// Create empty image
if ($infoImg[2] == 1)
if ($targetformat == 1)
{
// Compatibilite image GIF
$imgThumb = imagecreate($thumbWidth, $thumbHeight);
@ -411,22 +410,31 @@ function vignette($file, $maxWidth = 160, $maxHeight = 120, $extName='_small', $
}
// Initialisation des variables selon l'extension de l'image
switch($infoImg[2])
switch($targetformat)
{
case 1: // Gif
$trans_colour = imagecolorallocate($imgThumb, 255, 255, 255); // On procede autrement pour le format GIF
imagecolortransparent($imgThumb,$trans_colour);
break;
$extImgTarget = '.gif';
$newquality='NU';
break;
case 2: // Jpg
$trans_colour = imagecolorallocatealpha($imgThumb, 255, 255, 255, 0);
break;
$extImgTarget = '.jpg';
$newquality=$quality;
break;
case 3: // Png
imagealphablending($imgThumb,false); // Pour compatibilite sur certain systeme
$trans_colour = imagecolorallocatealpha($imgThumb, 255, 255, 255, 127); // Keep transparent channel
break;
$extImgTarget = '.png';
$newquality=$quality-100;
$newquality=round(abs($quality-100)*9/100);
break;
case 4: // Bmp
$trans_colour = imagecolorallocatealpha($imgThumb, 255, 255, 255, 0);
break;
$extImgTarget = '.bmp';
$newquality='NU';
break;
}
if (function_exists("imagefill")) imagefill($imgThumb, 0, 0, $trans_colour);
@ -436,14 +444,14 @@ function vignette($file, $maxWidth = 160, $maxHeight = 120, $extName='_small', $
$fileName = preg_replace('/(\.gif|\.jpeg|\.jpg|\.png|\.bmp)$/i','',$file); // On enleve extension quelquesoit la casse
$fileName = basename($fileName);
$imgThumbName = $dirthumb.'/'.$fileName.$extName.$extImg; // Chemin complet du fichier de la vignette
$imgThumbName = $dirthumb.'/'.$fileName.$extName.$extImgTarget; // Chemin complet du fichier de la vignette
// Check if permission are ok
//$fp = fopen($imgThumbName, "w");
//fclose($fp);
// Create image on disk
switch($infoImg[2])
switch($targetformat)
{
case 1: // Gif
imagegif($imgThumb, $imgThumbName);