2008-04-05 16:18:13 +02:00
< ? php
2010-02-21 04:09:45 +01:00
/* Copyright ( C ) 2004 - 2010 Laurent Destailleur < eldy @ users . sourceforge . net >
2012-12-30 15:13:49 +01:00
* Copyright ( C ) 2005 - 2007 Regis Houssin < regis . houssin @ capnetworks . com >
2008-04-05 16:18:13 +02:00
*
* This program is free software ; you can redistribute it and / or modify
* it under the terms of the GNU General Public License as published by
2013-01-16 15:36:08 +01:00
* the Free Software Foundation ; either version 3 of the License , or
2008-04-05 16:18:13 +02:00
* ( at your option ) any later version .
*
* This program is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
* GNU General Public License for more details .
*
* You should have received a copy of the GNU General Public License
2011-08-01 01:24:38 +02:00
* along with this program . If not , see < http :// www . gnu . org / licenses />.
2008-04-05 16:18:13 +02:00
* or see http :// www . gnu . org /
*/
/**
2011-10-24 10:45:06 +02:00
* \file htdocs / core / lib / images . lib . php
2011-02-07 15:23:57 +01:00
* \brief Set of function for manipulating images
2008-11-09 01:49:57 +01:00
*/
2008-04-05 16:18:13 +02:00
2011-02-07 15:23:57 +01:00
// Define size of logo small and mini
$maxwidthsmall = 270 ; $maxheightsmall = 150 ;
$maxwidthmini = 128 ; $maxheightmini = 72 ;
$quality = 80 ;
2008-04-05 16:18:13 +02:00
2010-02-21 02:45:58 +01:00
/**
2011-02-07 15:23:57 +01:00
* Return if a filename is file name of a supported image format
2012-02-04 14:39:47 +01:00
*
* @ param string $file Filename
* @ return int - 1 = Not image filename , 0 = Image filename but format not supported by PHP , 1 = Image filename with format supported
2010-02-21 02:45:58 +01:00
*/
2011-02-07 15:23:57 +01:00
function image_format_supported ( $file )
2010-02-21 02:45:58 +01:00
{
2011-02-07 15:23:57 +01:00
// Case filename is not a format image
if ( ! preg_match ( '/(\.gif|\.jpg|\.jpeg|\.png|\.bmp)$/i' , $file , $reg )) return - 1 ;
// Case filename is a format image but not supported by this PHP
$imgfonction = '' ;
if ( strtolower ( $reg [ 1 ]) == '.gif' ) $imgfonction = 'imagecreatefromgif' ;
if ( strtolower ( $reg [ 1 ]) == '.png' ) $imgfonction = 'imagecreatefrompng' ;
if ( strtolower ( $reg [ 1 ]) == '.jpg' ) $imgfonction = 'imagecreatefromjpeg' ;
if ( strtolower ( $reg [ 1 ]) == '.jpeg' ) $imgfonction = 'imagecreatefromjpeg' ;
if ( strtolower ( $reg [ 1 ]) == '.bmp' ) $imgfonction = 'imagecreatefromwbmp' ;
if ( $imgfonction )
{
if ( ! function_exists ( $imgfonction ))
{
// Fonctions de conversion non presente dans ce PHP
return 0 ;
}
}
// Filename is a format image and supported by this PHP
return 1 ;
}
2010-02-21 02:45:58 +01:00
2011-02-07 15:23:57 +01:00
/**
* Return size of image file on disk ( Supported extensions are gif , jpg , png and bmp )
2012-02-04 14:39:47 +01:00
*
* @ param string $file Full path name of file
2013-07-03 16:06:42 +02:00
* @ param bool $url Image with url ( true or false )
2012-02-04 14:39:47 +01:00
* @ return array array ( 'width' => width , 'height' => height )
2011-02-07 15:23:57 +01:00
*/
2013-07-03 16:06:42 +02:00
function dol_getImageSize ( $file , $url = false )
2011-02-07 15:23:57 +01:00
{
2010-02-21 02:45:58 +01:00
$ret = array ();
if ( image_format_supported ( $file ) < 0 ) return $ret ;
2013-07-03 16:06:42 +02:00
$fichier = $file ;
if ( ! $url )
{
$fichier = realpath ( $file ); // Chemin canonique absolu de l'image
$dir = dirname ( $file ); // Chemin du dossier contenant l'image
}
2010-02-21 02:45:58 +01:00
$infoImg = getimagesize ( $fichier ); // Recuperation des infos de l'image
$ret [ 'width' ] = $infoImg [ 0 ]; // Largeur de l'image
$ret [ 'height' ] = $infoImg [ 1 ]; // Hauteur de l'image
return $ret ;
}
2008-04-05 16:18:13 +02:00
2010-02-21 04:09:45 +01:00
/**
2011-02-07 15:23:57 +01:00
* Resize or crop an image file ( Supported extensions are gif , jpg , png and bmp )
2012-02-04 14:39:47 +01:00
*
* @ param string $file Path of file to resize / crop
* @ param int $mode 0 = Resize , 1 = Crop
* @ param int $newWidth Largeur maximum que dois faire l ' image destination ( 0 = keep ratio )
* @ param int $newHeight Hauteur maximum que dois faire l ' image destination ( 0 = keep ratio )
* @ param int $src_x Position of croping image in source image ( not use if mode = 0 )
* @ param int $src_y Position of croping image in source image ( not use if mode = 0 )
* @ return int File name if OK , error message if KO
2010-02-21 04:09:45 +01:00
*/
2010-02-21 12:50:21 +01:00
function dol_imageResizeOrCrop ( $file , $mode , $newWidth , $newHeight , $src_x = 0 , $src_y = 0 )
2010-02-21 04:09:45 +01:00
{
2012-08-22 23:11:24 +02:00
require_once DOL_DOCUMENT_ROOT . '/core/lib/functions2.lib.php' ;
2010-02-21 04:09:45 +01:00
global $conf , $langs ;
2010-02-21 12:50:21 +01:00
dol_syslog ( " dol_imageResizeOrCrop file= " . $file . " mode= " . $mode . " newWidth= " . $newWidth . " newHeight= " . $newHeight . " src_x= " . $src_x . " src_y= " . $src_y );
2010-02-21 04:09:45 +01:00
// Clean parameters
$file = trim ( $file );
// Check parameters
if ( ! $file )
{
// Si le fichier n'a pas ete indique
return 'Bad parameter file' ;
}
elseif ( ! file_exists ( $file ))
{
// Si le fichier passe en parametre n'existe pas
return $langs -> trans ( " ErrorFileNotFound " , $file );
}
elseif ( image_format_supported ( $file ) < 0 )
{
2010-02-21 12:50:21 +01:00
return 'This filename ' . $file . ' does not seem to be an image filename.' ;
2010-02-21 04:09:45 +01:00
}
2010-02-21 12:50:21 +01:00
elseif ( ! is_numeric ( $newWidth ) && ! is_numeric ( $newHeight ))
{
2010-02-21 04:09:45 +01:00
return 'Wrong value for parameter newWidth or newHeight' ;
}
2010-02-21 12:50:21 +01:00
elseif ( $mode == 0 && $newWidth <= 0 && $newHeight <= 0 )
{
return 'At least newHeight or newWidth must be defined for resizing' ;
}
elseif ( $mode == 1 && ( $newWidth <= 0 || $newHeight <= 0 ))
2010-02-21 04:09:45 +01:00
{
2010-02-21 12:50:21 +01:00
return 'Both newHeight or newWidth must be defined for croping' ;
2010-02-21 04:09:45 +01:00
}
$fichier = realpath ( $file ); // Chemin canonique absolu de l'image
$dir = dirname ( $file ); // Chemin du dossier contenant l'image
$infoImg = getimagesize ( $fichier ); // Recuperation des infos de l'image
$imgWidth = $infoImg [ 0 ]; // Largeur de l'image
$imgHeight = $infoImg [ 1 ]; // Hauteur de l'image
2010-02-21 12:50:21 +01:00
if ( $mode == 0 ) // If resize, we check parameters
2010-02-21 04:09:45 +01:00
{
2010-02-21 12:50:21 +01:00
if ( $newWidth <= 0 )
{
$newWidth = intval (( $newHeight / $imgHeight ) * $imgWidth ); // Keep ratio
}
if ( $newHeight <= 0 )
{
$newHeight = intval (( $newWidth / $imgWidth ) * $imgHeight ); // Keep ratio
}
2010-02-21 04:09:45 +01:00
}
$imgfonction = '' ;
switch ( $infoImg [ 2 ])
{
case 1 : // IMG_GIF
$imgfonction = 'imagecreatefromgif' ;
break ;
case 2 : // IMG_JPG
$imgfonction = 'imagecreatefromjpeg' ;
break ;
case 3 : // IMG_PNG
$imgfonction = 'imagecreatefrompng' ;
break ;
case 4 : // IMG_WBMP
$imgfonction = 'imagecreatefromwbmp' ;
break ;
}
if ( $imgfonction )
{
if ( ! function_exists ( $imgfonction ))
{
// Fonctions de conversion non presente dans ce PHP
return 'Resize not possible. This PHP does not support GD functions ' . $imgfonction ;
}
}
// Initialisation des variables selon l'extension de l'image
switch ( $infoImg [ 2 ])
{
case 1 : // Gif
$img = imagecreatefromgif ( $fichier );
2010-02-21 12:50:21 +01:00
$extImg = '.gif' ; // File name extension of image
$newquality = 'NU' ; // Quality is not used for this format
2010-02-21 04:09:45 +01:00
break ;
case 2 : // Jpg
$img = imagecreatefromjpeg ( $fichier );
2010-02-21 12:50:21 +01:00
$extImg = '.jpg' ;
$newquality = 100 ; // % quality maximum
2010-02-21 04:09:45 +01:00
break ;
case 3 : // Png
$img = imagecreatefrompng ( $fichier );
$extImg = '.png' ;
2010-02-21 12:50:21 +01:00
$newquality = 0 ; // No compression (0-9)
2010-02-21 04:09:45 +01:00
break ;
case 4 : // Bmp
$img = imagecreatefromwbmp ( $fichier );
$extImg = '.bmp' ;
2010-02-21 12:50:21 +01:00
$newquality = 'NU' ; // Quality is not used for this format
2010-02-21 04:09:45 +01:00
break ;
}
// Create empty image
if ( $infoImg [ 2 ] == 1 )
{
// Compatibilite image GIF
$imgThumb = imagecreate ( $newWidth , $newHeight );
}
else
{
$imgThumb = imagecreatetruecolor ( $newWidth , $newHeight );
}
// Activate antialiasing for better quality
if ( function_exists ( 'imageantialias' ))
{
imageantialias ( $imgThumb , true );
}
// This is to keep transparent alpha channel if exists (PHP >= 4.2)
if ( function_exists ( 'imagesavealpha' ))
{
imagesavealpha ( $imgThumb , true );
}
// Initialisation des variables selon l'extension de l'image
switch ( $infoImg [ 2 ])
{
case 1 : // Gif
$trans_colour = imagecolorallocate ( $imgThumb , 255 , 255 , 255 ); // On procede autrement pour le format GIF
imagecolortransparent ( $imgThumb , $trans_colour );
break ;
case 2 : // Jpg
$trans_colour = imagecolorallocatealpha ( $imgThumb , 255 , 255 , 255 , 0 );
break ;
case 3 : // Png
imagealphablending ( $imgThumb , false ); // Pour compatibilite sur certain systeme
$trans_colour = imagecolorallocatealpha ( $imgThumb , 255 , 255 , 255 , 127 ); // Keep transparent channel
break ;
case 4 : // Bmp
$trans_colour = imagecolorallocatealpha ( $imgThumb , 255 , 255 , 255 , 0 );
break ;
}
if ( function_exists ( " imagefill " )) imagefill ( $imgThumb , 0 , 0 , $trans_colour );
2010-02-21 12:50:21 +01:00
dol_syslog ( " dol_imageResizeOrCrop: convert image from ( $imgWidth x $imgHeight ) at position ( $src_x x $src_y ) to ( $newWidth x $newHeight ) as $extImg , newquality= $newquality " );
2010-02-21 04:09:45 +01:00
//imagecopyresized($imgThumb, $img, 0, 0, 0, 0, $thumbWidth, $thumbHeight, $imgWidth, $imgHeight); // Insere l'image de base redimensionnee
2010-02-21 12:50:21 +01:00
imagecopyresampled ( $imgThumb , $img , 0 , 0 , $src_x , $src_y , $newWidth , $newHeight , ( $mode == 0 ? $imgWidth : $newWidth ), ( $mode == 0 ? $imgHeight : $newHeight )); // Insere l'image de base redimensionnee
2010-02-21 04:09:45 +01:00
$imgThumbName = $file ;
// Check if permission are ok
//$fp = fopen($imgThumbName, "w");
//fclose($fp);
// Create image on disk
switch ( $infoImg [ 2 ])
{
case 1 : // Gif
imagegif ( $imgThumb , $imgThumbName );
break ;
case 2 : // Jpg
imagejpeg ( $imgThumb , $imgThumbName , $newquality );
break ;
case 3 : // Png
imagepng ( $imgThumb , $imgThumbName , $newquality );
break ;
case 4 : // Bmp
2013-09-05 17:56:28 +02:00
image2wbmp ( $imgThumb , $imgThumbName );
2010-02-21 04:09:45 +01:00
break ;
}
// Set permissions on file
if ( ! empty ( $conf -> global -> MAIN_UMASK )) @ chmod ( $imgThumbName , octdec ( $conf -> global -> MAIN_UMASK ));
2011-07-06 00:51:24 +02:00
// Free memory. This does not delete image.
imagedestroy ( $img );
2010-02-21 04:09:45 +01:00
imagedestroy ( $imgThumb );
2010-06-02 10:34:44 +02:00
clearstatcache (); // File was replaced by a modified one, so we clear file caches.
2010-02-21 12:50:21 +01:00
2010-02-21 04:09:45 +01:00
return $imgThumbName ;
}
2008-04-08 01:46:01 +02:00
/**
2011-02-07 15:23:57 +01:00
* 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
2011-12-14 17:13:09 +01:00
*
* @ 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
2008-04-05 16:18:13 +02:00
*/
2010-08-20 21:19:24 +02:00
function vignette ( $file , $maxWidth = 160 , $maxHeight = 120 , $extName = '_small' , $quality = 50 , $outdir = 'thumbs' , $targetformat = 0 )
2008-04-05 16:18:13 +02:00
{
2012-08-22 23:11:24 +02:00
require_once DOL_DOCUMENT_ROOT . '/core/lib/functions2.lib.php' ;
2008-11-16 02:09:04 +01:00
2008-11-09 01:49:57 +01:00
global $conf , $langs ;
2008-04-05 16:18:13 +02:00
2011-06-22 11:11:00 +02:00
dol_syslog ( " vignette file= " . $file . " extName= " . $extName . " maxWidth= " . $maxWidth . " maxHeight= " . $maxHeight . " quality= " . $quality . " outdir= " . $outdir . " targetformat= " . $targetformat );
2008-11-28 10:46:47 +01:00
2008-11-09 01:49:57 +01:00
// Clean parameters
2008-04-05 16:18:13 +02:00
$file = trim ( $file );
2008-11-09 01:49:57 +01:00
// Check parameters
2008-04-05 16:18:13 +02:00
if ( ! $file )
{
2008-11-09 01:49:57 +01:00
// Si le fichier n'a pas ete indique
2011-06-22 11:11:00 +02:00
return 'ErrorBadParameters' ;
2008-04-05 16:18:13 +02:00
}
elseif ( ! file_exists ( $file ))
{
2008-11-09 01:49:57 +01:00
// Si le fichier passe en parametre n'existe pas
2011-06-22 11:11:00 +02:00
dol_syslog ( $langs -> trans ( " ErrorFileNotFound " , $file ), LOG_ERR );
return $langs -> trans ( " ErrorFileNotFound " , $file );
2008-04-05 16:18:13 +02:00
}
elseif ( image_format_supported ( $file ) < 0 )
{
2011-06-22 11:11:00 +02:00
dol_syslog ( 'This file ' . $file . ' does not seem to be an image format file name.' , LOG_WARNING );
return 'ErrorBadImageFormat' ;
2008-04-05 16:18:13 +02:00
}
2010-02-13 23:20:32 +01:00
elseif ( ! is_numeric ( $maxWidth ) || empty ( $maxWidth ) || $maxWidth < - 1 ){
2008-11-09 01:49:57 +01:00
// Si la largeur max est incorrecte (n'est pas numerique, est vide, ou est inferieure a 0)
2011-06-22 11:11:00 +02:00
dol_syslog ( 'Wrong value for parameter maxWidth' , LOG_ERR );
2011-07-06 00:40:35 +02:00
return 'Error: Wrong value for parameter maxWidth' ;
2008-04-05 16:18:13 +02:00
}
2010-02-13 23:20:32 +01:00
elseif ( ! is_numeric ( $maxHeight ) || empty ( $maxHeight ) || $maxHeight < - 1 ){
2008-11-09 01:49:57 +01:00
// Si la hauteur max est incorrecte (n'est pas numerique, est vide, ou est inferieure a 0)
2011-06-22 11:11:00 +02:00
dol_syslog ( 'Wrong value for parameter maxHeight' , LOG_ERR );
2011-07-06 00:40:35 +02:00
return 'Error: Wrong value for parameter maxHeight' ;
2008-04-05 16:18:13 +02:00
}
2010-02-13 23:20:32 +01:00
$fichier = realpath ( $file ); // Chemin canonique absolu de l'image
$dir = dirname ( $file ); // Chemin du dossier contenant l'image
2008-04-05 16:18:13 +02:00
2008-11-09 01:49:57 +01:00
$infoImg = getimagesize ( $fichier ); // Recuperation des infos de l'image
2008-04-05 16:18:13 +02:00
$imgWidth = $infoImg [ 0 ]; // Largeur de l'image
$imgHeight = $infoImg [ 1 ]; // Hauteur de l'image
2010-02-13 23:20:32 +01:00
if ( $maxWidth == - 1 ) $maxWidth = $infoImg [ 0 ]; // If size is -1, we keep unchanged
if ( $maxHeight == - 1 ) $maxHeight = $infoImg [ 1 ]; // If size is -1, we keep unchanged
2008-11-09 01:49:57 +01:00
// Si l'image est plus petite que la largeur et la hauteur max, on ne cree pas de vignette
2008-04-05 16:18:13 +02:00
if ( $infoImg [ 0 ] < $maxWidth && $infoImg [ 1 ] < $maxHeight )
{
// On cree toujours les vignettes
2009-02-20 23:53:15 +01:00
dol_syslog ( " File size is smaller than thumb size " , LOG_DEBUG );
2008-11-09 01:49:57 +01:00
//return 'Le fichier '.$file.' ne necessite pas de creation de vignette';
2008-04-05 16:18:13 +02:00
}
$imgfonction = '' ;
switch ( $infoImg [ 2 ])
{
2011-12-14 17:13:09 +01:00
case IMAGETYPE_GIF : // 1
2008-04-05 16:18:13 +02:00
$imgfonction = 'imagecreatefromgif' ;
break ;
2011-12-14 17:13:09 +01:00
case IMAGETYPE_JPEG : // 2
2008-04-05 16:18:13 +02:00
$imgfonction = 'imagecreatefromjpeg' ;
break ;
2011-12-14 17:13:09 +01:00
case IMAGETYPE_PNG : // 3
2008-04-05 16:18:13 +02:00
$imgfonction = 'imagecreatefrompng' ;
break ;
2011-12-14 17:13:09 +01:00
case IMAGETYPE_BMP : // 6
// Not supported by PHP GD
break ;
case IMAGETYPE_WBMP : // 15
2008-04-05 16:18:13 +02:00
$imgfonction = 'imagecreatefromwbmp' ;
break ;
}
if ( $imgfonction )
{
if ( ! function_exists ( $imgfonction ))
{
// Fonctions de conversion non presente dans ce PHP
2011-07-06 00:40:35 +02:00
return 'Error: Creation of thumbs not possible. This PHP does not support GD function ' . $imgfonction ;
2008-04-05 16:18:13 +02:00
}
}
2008-11-09 01:49:57 +01:00
// On cree le repertoire contenant les vignettes
2010-02-21 02:53:01 +01:00
$dirthumb = $dir . ( $outdir ? '/' . $outdir : '' ); // Chemin du dossier contenant les vignettes
2012-02-19 18:34:22 +01:00
dol_mkdir ( $dirthumb );
2008-04-05 16:18:13 +02:00
// Initialisation des variables selon l'extension de l'image
switch ( $infoImg [ 2 ])
{
2011-12-14 17:13:09 +01:00
case IMAGETYPE_GIF : // 1
2008-04-05 16:18:13 +02:00
$img = imagecreatefromgif ( $fichier );
$extImg = '.gif' ; // Extension de l'image
break ;
2011-12-14 17:13:09 +01:00
case IMAGETYPE_JPEG : // 2
2008-04-05 16:18:13 +02:00
$img = imagecreatefromjpeg ( $fichier );
2012-03-18 01:21:18 +01:00
$extImg = ( preg_match ( '/\.jpeg$/' , $file ) ? '.jpeg' : '.jpg' ); // Extension de l'image
2008-04-05 16:18:13 +02:00
break ;
2011-12-14 17:13:09 +01:00
case IMAGETYPE_PNG : // 3
2008-04-05 16:18:13 +02:00
$img = imagecreatefrompng ( $fichier );
$extImg = '.png' ;
break ;
2011-12-14 17:13:09 +01:00
case IMAGETYPE_BMP : // 6
// Not supported by PHP GD
$extImg = '.bmp' ;
break ;
case IMAGETYPE_WBMP : // 15
2008-04-05 16:18:13 +02:00
$img = imagecreatefromwbmp ( $fichier );
$extImg = '.bmp' ;
break ;
}
2011-12-14 17:13:09 +01:00
if ( ! is_resource ( $img ))
{
dol_syslog ( 'Failed to detect type of image. We found infoImg[2]=' . $infoImg [ 2 ], LOG_WARNING );
return 0 ;
}
2008-04-05 16:18:13 +02:00
2008-11-09 01:49:57 +01:00
// Initialisation des dimensions de la vignette si elles sont superieures a l'original
2008-04-05 16:18:13 +02:00
if ( $maxWidth > $imgWidth ){ $maxWidth = $imgWidth ; }
if ( $maxHeight > $imgHeight ){ $maxHeight = $imgHeight ; }
$whFact = $maxWidth / $maxHeight ; // Facteur largeur/hauteur des dimensions max de la vignette
$imgWhFact = $imgWidth / $imgHeight ; // Facteur largeur/hauteur de l'original
// Fixe les dimensions de la vignette
2012-03-18 01:21:18 +01:00
if ( $whFact < $imgWhFact )
{
2008-11-09 01:49:57 +01:00
// Si largeur determinante
2008-04-05 16:18:13 +02:00
$thumbWidth = $maxWidth ;
$thumbHeight = $thumbWidth / $imgWhFact ;
2012-03-18 01:21:18 +01:00
}
else
{
2008-11-09 01:49:57 +01:00
// Si hauteur determinante
2008-04-05 16:18:13 +02:00
$thumbHeight = $maxHeight ;
$thumbWidth = $thumbHeight * $imgWhFact ;
}
$thumbHeight = round ( $thumbHeight );
$thumbWidth = round ( $thumbWidth );
2008-11-28 10:46:47 +01:00
2010-08-20 21:19:24 +02:00
// Define target format
if ( empty ( $targetformat )) $targetformat = $infoImg [ 2 ];
2008-04-05 16:18:13 +02:00
// Create empty image
2011-12-14 17:13:09 +01:00
if ( $targetformat == IMAGETYPE_GIF )
2008-04-05 16:18:13 +02:00
{
2008-11-09 01:49:57 +01:00
// Compatibilite image GIF
2008-04-05 16:18:13 +02:00
$imgThumb = imagecreate ( $thumbWidth , $thumbHeight );
}
else
{
$imgThumb = imagecreatetruecolor ( $thumbWidth , $thumbHeight );
}
// Activate antialiasing for better quality
if ( function_exists ( 'imageantialias' ))
{
imageantialias ( $imgThumb , true );
}
// This is to keep transparent alpha channel if exists (PHP >= 4.2)
if ( function_exists ( 'imagesavealpha' ))
{
imagesavealpha ( $imgThumb , true );
}
2008-11-28 10:46:47 +01:00
2008-04-05 16:18:13 +02:00
// Initialisation des variables selon l'extension de l'image
2010-08-20 21:19:24 +02:00
switch ( $targetformat )
2008-04-05 16:18:13 +02:00
{
2011-12-14 17:13:09 +01:00
case IMAGETYPE_GIF : // 1
2008-11-09 01:49:57 +01:00
$trans_colour = imagecolorallocate ( $imgThumb , 255 , 255 , 255 ); // On procede autrement pour le format GIF
2008-04-05 16:18:13 +02:00
imagecolortransparent ( $imgThumb , $trans_colour );
2010-08-20 21:19:24 +02:00
$extImgTarget = '.gif' ;
$newquality = 'NU' ;
break ;
2011-12-14 17:13:09 +01:00
case IMAGETYPE_JPEG : // 2
$trans_colour = imagecolorallocatealpha ( $imgThumb , 255 , 255 , 255 , 0 );
2012-03-18 01:21:18 +01:00
$extImgTarget = ( preg_match ( '/\.jpeg$/' , $file ) ? '.jpeg' : '.jpg' );
2010-08-20 21:19:24 +02:00
$newquality = $quality ;
break ;
2011-12-14 17:13:09 +01:00
case IMAGETYPE_PNG : // 3
2008-11-09 01:49:57 +01:00
imagealphablending ( $imgThumb , false ); // Pour compatibilite sur certain systeme
2008-04-05 16:18:13 +02:00
$trans_colour = imagecolorallocatealpha ( $imgThumb , 255 , 255 , 255 , 127 ); // Keep transparent channel
2010-08-20 21:19:24 +02:00
$extImgTarget = '.png' ;
$newquality = $quality - 100 ;
$newquality = round ( abs ( $quality - 100 ) * 9 / 100 );
break ;
2011-12-14 17:13:09 +01:00
case IMAGETYPE_BMP : // 6
// Not supported by PHP GD
$extImgTarget = '.bmp' ;
$newquality = 'NU' ;
break ;
case IMAGETYPE_WBMP : // 15
2008-04-05 16:18:13 +02:00
$trans_colour = imagecolorallocatealpha ( $imgThumb , 255 , 255 , 255 , 0 );
2010-08-20 21:19:24 +02:00
$extImgTarget = '.bmp' ;
$newquality = 'NU' ;
break ;
2008-04-05 16:18:13 +02:00
}
if ( function_exists ( " imagefill " )) imagefill ( $imgThumb , 0 , 0 , $trans_colour );
2009-02-20 23:53:15 +01:00
dol_syslog ( " vignette: convert image from ( $imgWidth x $imgHeight ) to ( $thumbWidth x $thumbHeight ) as $extImg , newquality= $newquality " );
2008-11-09 01:49:57 +01:00
//imagecopyresized($imgThumb, $img, 0, 0, 0, 0, $thumbWidth, $thumbHeight, $imgWidth, $imgHeight); // Insere l'image de base redimensionnee
imagecopyresampled ( $imgThumb , $img , 0 , 0 , 0 , 0 , $thumbWidth , $thumbHeight , $imgWidth , $imgHeight ); // Insere l'image de base redimensionnee
2008-04-05 16:18:13 +02:00
2009-10-21 19:42:31 +02:00
$fileName = preg_replace ( '/(\.gif|\.jpeg|\.jpg|\.png|\.bmp)$/i' , '' , $file ); // On enleve extension quelquesoit la casse
2008-04-05 16:18:13 +02:00
$fileName = basename ( $fileName );
2010-08-20 21:19:24 +02:00
$imgThumbName = $dirthumb . '/' . $fileName . $extName . $extImgTarget ; // Chemin complet du fichier de la vignette
2008-04-05 16:18:13 +02:00
// Check if permission are ok
//$fp = fopen($imgThumbName, "w");
//fclose($fp);
// Create image on disk
2010-08-20 21:19:24 +02:00
switch ( $targetformat )
2008-04-05 16:18:13 +02:00
{
2011-12-14 17:13:09 +01:00
case IMAGETYPE_GIF : // 1
2008-04-05 16:18:13 +02:00
imagegif ( $imgThumb , $imgThumbName );
break ;
2011-12-14 17:13:09 +01:00
case IMAGETYPE_JPEG : // 2
2008-04-05 16:18:13 +02:00
imagejpeg ( $imgThumb , $imgThumbName , $newquality );
break ;
2011-12-14 17:13:09 +01:00
case IMAGETYPE_PNG : // 3
2008-04-05 16:18:13 +02:00
imagepng ( $imgThumb , $imgThumbName , $newquality );
break ;
2011-12-14 17:13:09 +01:00
case IMAGETYPE_BMP : // 6
// Not supported by PHP GD
break ;
case IMAGETYPE_WBMP : // 15
2013-09-05 17:56:28 +02:00
image2wbmp ( $imgThumb , $imgThumbName );
2008-04-05 16:18:13 +02:00
break ;
}
2008-11-09 01:49:57 +01:00
// Set permissions on file
if ( ! empty ( $conf -> global -> MAIN_UMASK )) @ chmod ( $imgThumbName , octdec ( $conf -> global -> MAIN_UMASK ));
2011-07-06 00:51:24 +02:00
// Free memory. This does not delete image.
imagedestroy ( $img );
imagedestroy ( $imgThumb );
2008-04-05 16:18:13 +02:00
return $imgThumbName ;
}
2008-04-20 06:31:31 +02:00
/**
2012-02-04 14:39:47 +01:00
* This function returns the html for the moneymeter .
*
* @ param int $actualValue amount of actual money
* @ param int $pendingValue amount of money of pending memberships
* @ param int $intentValue amount of intended money ( that ' s without the amount of actual money )
* @ return string thermometer htmlLegenda
2008-11-28 10:46:47 +01:00
*/
2008-04-20 06:31:31 +02:00
function moneyMeter ( $actualValue = 0 , $pendingValue = 0 , $intentValue = 0 )
{
2008-11-28 10:46:47 +01:00
global $langs ;
// variables
$height = " 200 " ;
$maximumValue = 125000 ;
2008-11-28 10:46:47 +01:00
$imageDir = " http://eucd.info/images/therm/ " ;
2008-11-28 10:46:47 +01:00
$imageTop = $imageDir . " therm_top.png " ;
$imageMiddleActual = $imageDir . " therm_actual.png " ;
$imageMiddlePending = $imageDir . " therm_pending.png " ;
$imageMiddleIntent = $imageDir . " therm_intent.png " ;
$imageMiddleGoal = $imageDir . " therm_goal.png " ;
$imageIndex = $imageDir . " therm_index.png " ;
$imageBottom = $imageDir . " therm_bottom.png " ;
$imageColorActual = $imageDir . " therm_color_actual.png " ;
$imageColorPending = $imageDir . " therm_color_pending.png " ;
$imageColorIntent = $imageDir . " therm_color_intent.png " ;
2010-02-13 23:20:32 +01:00
2011-11-08 10:18:45 +01:00
$formThermTop = '
2008-04-20 06:31:31 +02:00
<!-- Thermometer Begin -->
< table cellpadding = " 0 " cellspacing = " 4 " border = " 0 " >
< tr >< td >
< table cellpadding = " 0 " cellspacing = " 0 " border = " 0 " >
< tr >
< td colspan = " 2 " >< img src = " ' . $imageTop . ' " width = " 58 " height = " 6 " border = " 0 " ></ td >
</ tr >
< tr >
< td >
< table cellpadding = " 0 " cellspacing = " 0 " border = " 0 " > ' ;
2008-11-28 10:46:47 +01:00
2011-11-08 10:18:45 +01:00
$formSection = '
2008-04-20 06:31:31 +02:00
< tr >< td >< img src = " { image} " width = " 26 " height = " { height} " border = " 0 " ></ td ></ tr > ' ;
2010-02-13 23:20:32 +01:00
2011-11-08 10:18:45 +01:00
$formThermbottom = '
2008-04-20 06:31:31 +02:00
</ table >
</ td >
< td >< img src = " ' . $imageIndex . ' " width = " 32 " height = " 200 " border = " 0 " ></ td >
</ tr >
< tr >
< td colspan = " 2 " >< img src = " ' . $imageBottom . ' " width = " 58 " height = " 32 " border = " 0 " ></ td >
</ tr >
</ table >
</ td >
2010-02-13 23:20:32 +01:00
</ tr ></ table > ' ;
2008-04-20 06:31:31 +02:00
2008-11-28 10:46:47 +01:00
// legenda
$legendaActual = " € " . round ( $actualValue );
$legendaPending = " € " . round ( $pendingValue );
$legendaIntent = " € " . round ( $intentValue );
$legendaTotal = " € " . round ( $actualValue + $pendingValue + $intentValue );
2011-11-08 10:18:45 +01:00
$formLegenda = '
2008-04-20 06:31:31 +02:00
< table cellpadding = " 0 " cellspacing = " 0 " border = " 0 " >
2010-03-16 00:54:37 +01:00
< tr >< td >< img src = " ' . $imageColorActual . ' " width = " 9 " height = " 9 " >& nbsp ; </ td >< td >< font size = " 1 " face = " Verdana, Arial, Helvetica, sans-serif " >< b > '.$langs->trans("Paid").' :< br > ' . $legendaActual . ' </ b ></ font ></ td ></ tr >
< tr >< td >< img src = " ' . $imageColorPending . ' " width = " 9 " height = " 9 " >& nbsp ; </ td >< td >< font size = " 1 " face = " Verdana, Arial, Helvetica, sans-serif " > '.$langs->trans("Waiting").' :< br > ' . $legendaPending . ' </ font ></ td ></ tr >
< tr >< td >< img src = " ' . $imageColorIntent . ' " width = " 9 " height = " 9 " >& nbsp ; </ td >< td >< font size = " 1 " face = " Verdana, Arial, Helvetica, sans-serif " > '.$langs->trans("Promesses").' :< br > ' . $legendaIntent . ' </ font ></ td ></ tr >
< tr >< td >& nbsp ; </ td >< td >< font size = " 1 " face = " Verdana, Arial, Helvetica, sans-serif " > Total :< br > ' . $legendaTotal . ' </ font ></ td ></ tr >
2008-04-20 06:31:31 +02:00
</ table >
<!-- Thermometer End --> ' ;
2008-11-28 10:46:47 +01:00
// check and edit some values
$error = 0 ;
if ( $maximumValue <= 0 || $height <= 0 || $actualValue < 0 || $pendingValue < 0 || $intentValue < 0 )
2008-04-20 06:31:31 +02:00
{
2008-11-28 10:46:47 +01:00
return " The money meter could not be processed<br> \n " ;
}
if ( $actualValue > $maximumValue )
{
$actualValue = $maximumValue ;
$pendingValue = 0 ;
$intentValue = 0 ;
}
else
{
if ( ( $actualValue + $pendingValue ) > $maximumValue )
{
2008-04-20 06:31:31 +02:00
$pendingValue = $maximumValue - $actualValue ;
$intentValue = 0 ;
2008-11-28 10:46:47 +01:00
}
else
{
if ( ( $actualValue + $pendingValue + $intentValue ) > $maximumValue )
{
$intentValue = $maximumValue - $actualValue - $pendingValue ;
}
}
2008-04-20 06:31:31 +02:00
}
2008-11-28 10:46:47 +01:00
// start writing the html (from bottom to top)
// bottom
2011-11-08 10:18:45 +01:00
$thermometer = $formThermbottom ;
2008-11-28 10:46:47 +01:00
// actual
$sectionHeight = round (( $actualValue / $maximumValue ) * $height );
$totalHeight = $totalHeight + $sectionHeight ;
if ( $sectionHeight > 0 )
2008-04-20 06:31:31 +02:00
{
2011-11-08 10:18:45 +01:00
$section = $formSection ;
2008-11-28 10:46:47 +01:00
$section = str_replace ( " { image} " , $imageMiddleActual , $section );
$section = str_replace ( " { height} " , $sectionHeight , $section );
$thermometer = $section . $thermometer ;
}
// pending
$sectionHeight = round (( $pendingValue / $maximumValue ) * $height );
$totalHeight = $totalHeight + $sectionHeight ;
if ( $sectionHeight > 0 )
{
2011-11-08 10:18:45 +01:00
$section = $formSection ;
2008-11-28 10:46:47 +01:00
$section = str_replace ( " { image} " , $imageMiddlePending , $section );
$section = str_replace ( " { height} " , $sectionHeight , $section );
$thermometer = $section . $thermometer ;
2008-04-20 06:31:31 +02:00
}
2008-11-28 10:46:47 +01:00
// intent
$sectionHeight = round (( $intentValue / $maximumValue ) * $height );
$totalHeight = $totalHeight + $sectionHeight ;
if ( $sectionHeight > 0 )
{
2011-11-08 10:18:45 +01:00
$section = $formSection ;
2008-11-28 10:46:47 +01:00
$section = str_replace ( " { image} " , $imageMiddleIntent , $section );
$section = str_replace ( " { height} " , $sectionHeight , $section );
$thermometer = $section . $thermometer ;
}
// goal
$sectionHeight = $height - $totalHeight ;
if ( $sectionHeight > 0 )
{
2011-11-08 10:18:45 +01:00
$section = $formSection ;
2008-11-28 10:46:47 +01:00
$section = str_replace ( " { image} " , $imageMiddleGoal , $section );
$section = str_replace ( " { height} " , $sectionHeight , $section );
$thermometer = $section . $thermometer ;
}
// top
2011-11-08 10:18:45 +01:00
$thermometer = $formThermTop . $thermometer ;
2008-11-28 10:46:47 +01:00
2011-11-08 10:18:45 +01:00
return $thermometer . $formLegenda ;
2008-04-20 06:31:31 +02:00
}
2008-04-05 16:18:13 +02:00
?>