From 1ffacf2d03740bfd2b2c2b8832a471611d832792 Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Wed, 14 Dec 2011 17:13:09 +0100 Subject: [PATCH] Fix: Use correct constant --- htdocs/core/lib/images.lib.php | 73 ++++++++++++++++++++++------------ 1 file changed, 47 insertions(+), 26 deletions(-) diff --git a/htdocs/core/lib/images.lib.php b/htdocs/core/lib/images.lib.php index 41c3f72fbc2..80443e7708c 100644 --- a/htdocs/core/lib/images.lib.php +++ b/htdocs/core/lib/images.lib.php @@ -286,14 +286,15 @@ function dol_imageResizeOrCrop($file, $mode, $newWidth, $newHeight, $src_x=0, $s /** * Create a thumbnail from an image file (Supported extensions are gif, jpg, png and bmp). * If file is myfile.jpg, new file may be myfile_small.jpg - * @param file Path of source file to resize - * @param maxWidth Largeur maximum que dois faire la miniature (-1=unchanged, 160 by default) - * @param maxHeight Hauteur maximum que dois faire l'image (-1=unchanged, 120 by default) - * @param extName Extension to differenciate thumb file name ('_small', '_mini') - * @param quality Quality of compression (0=worst, 100=best) - * @param outdir Directory where to store thumb - * @param targetformat New format of target (1,2,3,4 or 0 to keep old format) - * @return string Full path of thumb + * + * @param string $file Path of source file to resize + * @param int $maxWidth Largeur maximum que dois faire la miniature (-1=unchanged, 160 by default) + * @param int $maxHeight Hauteur maximum que dois faire l'image (-1=unchanged, 120 by default) + * @param string $extName Extension to differenciate thumb file name ('_small', '_mini') + * @param int $quality Quality of compression (0=worst, 100=best) + * @param string $outdir Directory where to store thumb + * @param int $targetformat New format of target (1,2,3,... or 0 to keep old format) + * @return string Full path of thumb or '' if it fails */ function vignette($file, $maxWidth = 160, $maxHeight = 120, $extName='_small', $quality=50, $outdir='thumbs', $targetformat=0) { @@ -355,16 +356,19 @@ function vignette($file, $maxWidth = 160, $maxHeight = 120, $extName='_small', $ $imgfonction=''; switch($infoImg[2]) { - case 1: // IMG_GIF + case IMAGETYPE_GIF: // 1 $imgfonction = 'imagecreatefromgif'; break; - case 2: // IMG_JPG + case IMAGETYPE_JPEG: // 2 $imgfonction = 'imagecreatefromjpeg'; break; - case 3: // IMG_PNG + case IMAGETYPE_PNG: // 3 $imgfonction = 'imagecreatefrompng'; break; - case 4: // IMG_WBMP + case IMAGETYPE_BMP: // 6 + // Not supported by PHP GD + break; + case IMAGETYPE_WBMP: // 15 $imgfonction = 'imagecreatefromwbmp'; break; } @@ -384,23 +388,32 @@ function vignette($file, $maxWidth = 160, $maxHeight = 120, $extName='_small', $ // Initialisation des variables selon l'extension de l'image switch($infoImg[2]) { - case 1: // Gif + case IMAGETYPE_GIF: // 1 $img = imagecreatefromgif($fichier); $extImg = '.gif'; // Extension de l'image break; - case 2: // Jpg + case IMAGETYPE_JPEG: // 2 $img = imagecreatefromjpeg($fichier); $extImg = '.jpg'; // Extension de l'image break; - case 3: // Png + case IMAGETYPE_PNG: // 3 $img = imagecreatefrompng($fichier); $extImg = '.png'; break; - case 4: // Bmp + case IMAGETYPE_BMP: // 6 + // Not supported by PHP GD + $extImg = '.bmp'; + break; + case IMAGETYPE_WBMP: // 15 $img = imagecreatefromwbmp($fichier); $extImg = '.bmp'; break; } + if (! is_resource($img)) + { + dol_syslog('Failed to detect type of image. We found infoImg[2]='.$infoImg[2], LOG_WARNING); + return 0; + } // Initialisation des dimensions de la vignette si elles sont superieures a l'original if($maxWidth > $imgWidth){ $maxWidth = $imgWidth; } @@ -426,7 +439,7 @@ function vignette($file, $maxWidth = 160, $maxHeight = 120, $extName='_small', $ if (empty($targetformat)) $targetformat=$infoImg[2]; // Create empty image - if ($targetformat == 1) + if ($targetformat == IMAGETYPE_GIF) { // Compatibilite image GIF $imgThumb = imagecreate($thumbWidth, $thumbHeight); @@ -451,25 +464,30 @@ function vignette($file, $maxWidth = 160, $maxHeight = 120, $extName='_small', $ // Initialisation des variables selon l'extension de l'image switch($targetformat) { - case 1: // Gif + case IMAGETYPE_GIF: // 1 $trans_colour = imagecolorallocate($imgThumb, 255, 255, 255); // On procede autrement pour le format GIF imagecolortransparent($imgThumb,$trans_colour); $extImgTarget = '.gif'; $newquality='NU'; break; - case 2: // Jpg - $trans_colour = imagecolorallocatealpha($imgThumb, 255, 255, 255, 0); + case IMAGETYPE_JPEG: // 2 + $trans_colour = imagecolorallocatealpha($imgThumb, 255, 255, 255, 0); $extImgTarget = '.jpg'; $newquality=$quality; break; - case 3: // Png + case IMAGETYPE_PNG: // 3 imagealphablending($imgThumb,false); // Pour compatibilite sur certain systeme $trans_colour = imagecolorallocatealpha($imgThumb, 255, 255, 255, 127); // Keep transparent channel $extImgTarget = '.png'; $newquality=$quality-100; $newquality=round(abs($quality-100)*9/100); break; - case 4: // Bmp + case IMAGETYPE_BMP: // 6 + // Not supported by PHP GD + $extImgTarget = '.bmp'; + $newquality='NU'; + break; + case IMAGETYPE_WBMP: // 15 $trans_colour = imagecolorallocatealpha($imgThumb, 255, 255, 255, 0); $extImgTarget = '.bmp'; $newquality='NU'; @@ -492,16 +510,19 @@ function vignette($file, $maxWidth = 160, $maxHeight = 120, $extName='_small', $ // Create image on disk switch($targetformat) { - case 1: // Gif + case IMAGETYPE_GIF: // 1 imagegif($imgThumb, $imgThumbName); break; - case 2: // Jpg + case IMAGETYPE_JPEG: // 2 imagejpeg($imgThumb, $imgThumbName, $newquality); break; - case 3: // Png + case IMAGETYPE_PNG: // 3 imagepng($imgThumb, $imgThumbName, $newquality); break; - case 4: // Bmp + case IMAGETYPE_BMP: // 6 + // Not supported by PHP GD + break; + case IMAGETYPE_WBMP: // 15 image2wmp($imgThumb, $imgThumbName); break; }