Ejemplo n.º 1
0
 /**
  *    	Return HTML code to output a photo
  *
  *    	@param	string		$modulepart			Key to define module concerned ('societe', 'userphoto', 'memberphoto')
  *     	@param  object		$object				Object containing data to retrieve file name
  * 		@param	int			$width				Width of photo
  * 		@param	int			$height				Height of photo (auto if 0)
  * 		@param	int			$caneditfield		Add edit fields
  * 		@param	string		$cssclass			CSS name to use on img for photo
  * 		@param	string		$imagesize		    'mini', 'small' or '' (original)
  *      @param  int         $addlinktofullsize  Add link to fullsize image
  *      @param  int         $cache              1=Accept to use image in cache
  * 	  	@return string    						HTML code to output photo
  */
 static function showphoto($modulepart, $object, $width = 100, $height = 0, $caneditfield = 0, $cssclass = 'photowithmargin', $imagesize = '', $addlinktofullsize = 1, $cache = 0)
 {
     global $conf, $langs;
     $entity = !empty($object->entity) ? $object->entity : $conf->entity;
     $id = !empty($object->id) ? $object->id : $object->rowid;
     $ret = '';
     $dir = '';
     $file = '';
     $originalfile = '';
     $altfile = '';
     $email = '';
     if ($modulepart == 'societe') {
         $dir = $conf->societe->multidir_output[$entity];
         if (!empty($object->logo)) {
             if ((string) $imagesize == 'mini') {
                 $file = get_exdir(0, 0, 0, 0, $object, 'thirdparty') . '/logos/' . getImageFileNameForSize($object->logo, '_mini');
             } else {
                 if ((string) $imagesize == 'small') {
                     $file = get_exdir(0, 0, 0, 0, $object, 'thirdparty') . '/logos/' . getImageFileNameForSize($object->logo, '_small');
                 } else {
                     $file = get_exdir(0, 0, 0, 0, $object, 'thirdparty') . '/logos/' . $object->logo;
                 }
             }
             $originalfile = get_exdir(0, 0, 0, 0, $object, 'thirdparty') . '/logos/' . $object->logo;
         }
     } else {
         if ($modulepart == 'contact') {
             $dir = $conf->societe->multidir_output[$entity] . '/contact';
             if (!empty($object->photo)) {
                 if ((string) $imagesize == 'mini') {
                     $file = get_exdir(0, 0, 0, 0, $object, 'contact') . '/photos/' . getImageFileNameForSize($object->photo, '_mini');
                 } else {
                     if ((string) $imagesize == 'small') {
                         $file = get_exdir(0, 0, 0, 0, $object, 'contact') . '/photos/' . getImageFileNameForSize($object->photo, '_small');
                     } else {
                         $file = get_exdir(0, 0, 0, 0, $object, 'contact') . '/photos/' . $object->photo;
                     }
                 }
                 $originalfile = get_exdir(0, 0, 0, 0, $object, 'contact') . '/photos/' . $object->photo;
             }
         } else {
             if ($modulepart == 'userphoto') {
                 $dir = $conf->user->dir_output;
                 if (!empty($object->photo)) {
                     if ((string) $imagesize == 'mini') {
                         $file = get_exdir($id, 2, 0, 0, $object, 'user') . getImageFileNameForSize($object->photo, '_mini');
                     } else {
                         if ((string) $imagesize == 'small') {
                             $file = get_exdir($id, 2, 0, 0, $object, 'user') . getImageFileNameForSize($object->photo, '_small');
                         } else {
                             $file = get_exdir($id, 2, 0, 0, $object, 'user') . $object->photo;
                         }
                     }
                     $originalfile = get_exdir($id, 2, 0, 0, $object, 'user') . $object->photo;
                 }
                 if (!empty($conf->global->MAIN_OLD_IMAGE_LINKS)) {
                     $altfile = $object->id . ".jpg";
                 }
                 // For backward compatibility
                 $email = $object->email;
             } else {
                 if ($modulepart == 'memberphoto') {
                     $dir = $conf->adherent->dir_output;
                     if (!empty($object->photo)) {
                         if ((string) $imagesize == 'mini') {
                             $file = get_exdir($id, 2, 0, 0, $object, 'member') . 'photos/' . getImageFileNameForSize($object->photo, '_mini');
                         } else {
                             if ((string) $imagesize == 'small') {
                                 $file = get_exdir($id, 2, 0, 0, $object, 'member') . 'photos/' . getImageFileNameForSize($object->photo, '_small');
                             } else {
                                 $file = get_exdir($id, 2, 0, 0, $object, 'member') . 'photos/' . $object->photo;
                             }
                         }
                         $originalfile = get_exdir($id, 2, 0, 0, $object, 'member') . 'photos/' . $object->photo;
                     }
                     if (!empty($conf->global->MAIN_OLD_IMAGE_LINKS)) {
                         $altfile = $object->id . ".jpg";
                     }
                     // For backward compatibility
                     $email = $object->email;
                 } else {
                     $dir = $conf->{$modulepart}->dir_output;
                     if (!empty($object->photo)) {
                         if ((string) $imagesize == 'mini') {
                             $file = get_exdir($id, 2, 0, 0, $object, $modulepart) . 'photos/' . getImageFileNameForSize($object->photo, '_mini');
                         } else {
                             if ((string) $imagesize == 'small') {
                                 $file = get_exdir($id, 2, 0, 0, $object, $modulepart) . 'photos/' . getImageFileNameForSize($object->photo, '_small');
                             } else {
                                 $file = get_exdir($id, 2, 0, 0, $object, $modulepart) . 'photos/' . $object->photo;
                             }
                         }
                         $originalfile = get_exdir($id, 2, 0, 0, $object, $modulepart) . 'photos/' . $object->photo;
                     }
                     if (!empty($conf->global->MAIN_OLD_IMAGE_LINKS)) {
                         $altfile = $object->id . ".jpg";
                     }
                     // For backward compatibility
                     $email = $object->email;
                 }
             }
         }
     }
     if ($dir) {
         if ($file && file_exists($dir . "/" . $file)) {
             if ($addlinktofullsize) {
                 $ret .= '<a href="' . DOL_URL_ROOT . '/viewimage.php?modulepart=' . $modulepart . '&entity=' . $entity . '&file=' . urlencode($originalfile) . '&cache=' . $cache . '">';
             }
             $ret .= '<img alt="Photo" id="photologo' . preg_replace('/[^a-z]/i', '_', $file) . '" class="' . $cssclass . '" ' . ($width ? ' width="' . $width . '"' : '') . ($height ? ' height="' . $height . '"' : '') . ' src="' . DOL_URL_ROOT . '/viewimage.php?modulepart=' . $modulepart . '&entity=' . $entity . '&file=' . urlencode($file) . '&cache=' . $cache . '">';
             if ($addlinktofullsize) {
                 $ret .= '</a>';
             }
         } else {
             if ($altfile && file_exists($dir . "/" . $altfile)) {
                 if ($addlinktofullsize) {
                     $ret .= '<a href="' . DOL_URL_ROOT . '/viewimage.php?modulepart=' . $modulepart . '&entity=' . $entity . '&file=' . urlencode($originalfile) . '&cache=' . $cache . '">';
                 }
                 $ret .= '<img alt="Photo alt" id="photologo' . preg_replace('/[^a-z]/i', '_', $file) . '" class="' . $cssclass . '" ' . ($width ? ' width="' . $width . '"' : '') . ($height ? ' height="' . $height . '"' : '') . ' src="' . DOL_URL_ROOT . '/viewimage.php?modulepart=' . $modulepart . '&entity=' . $entity . '&file=' . urlencode($altfile) . '&cache=' . $cache . '">';
                 if ($addlinktofullsize) {
                     $ret .= '</a>';
                 }
             } else {
                 $nophoto = '/public/theme/common/nophoto.png';
                 if (in_array($modulepart, array('userphoto', 'contact'))) {
                     $nophoto = '/public/theme/common/user_anonymous.png';
                     if ($object->gender == 'man') {
                         $nophoto = '/public/theme/common/user_man.png';
                     }
                     if ($object->gender == 'woman') {
                         $nophoto = '/public/theme/common/user_woman.png';
                     }
                 }
                 if (!empty($conf->gravatar->enabled) && $email) {
                     /**
                      * @see https://gravatar.com/site/implement/images/php/
                      */
                     global $dolibarr_main_url_root;
                     $ret .= '<!-- Put link to gravatar -->';
                     $ret .= '<img class="photo' . $modulepart . ($cssclass ? ' ' . $cssclass : '') . '" alt="Gravatar avatar" title="' . $email . ' Gravatar avatar" border="0"' . ($width ? ' width="' . $width . '"' : '') . ($height ? ' height="' . $height . '"' : '') . ' src="https://www.gravatar.com/avatar/' . dol_hash(strtolower(trim($email)), 3) . '?s=' . $width . '&d=' . urlencode(dol_buildpath($nophoto, 2)) . '">';
                     // gravatar need md5 hash
                 } else {
                     $ret .= '<img class="photo' . $modulepart . ($cssclass ? ' ' . $cssclass : '') . '" alt="No photo" border="0"' . ($width ? ' width="' . $width . '"' : '') . ($height ? ' height="' . $height . '"' : '') . ' src="' . DOL_URL_ROOT . $nophoto . '">';
                 }
             }
         }
         if ($caneditfield) {
             if ($object->photo) {
                 $ret .= "<br>\n";
             }
             $ret .= '<table class="nobordernopadding hideonsmartphone">';
             if ($object->photo) {
                 $ret .= '<tr><td align="center"><input type="checkbox" class="flat photodelete" name="deletephoto" id="photodelete"> ' . $langs->trans("Delete") . '<br><br></td></tr>';
             }
             $ret .= '<tr><td>' . $langs->trans("PhotoFile") . '</td></tr>';
             $ret .= '<tr><td><input type="file" class="flat" name="photo" id="photoinput"></td></tr>';
             $ret .= '</table>';
         }
     } else {
         dol_print_error('', 'Call of showphoto with wrong parameters');
     }
     return $ret;
 }
Ejemplo n.º 2
0
	/**
	 *  Show photos of a product (nbmax maximum), into several columns
	 *	TODO Move this into html.formproduct.class.php
	 *
	 *  @param      string	$sdir        	Directory to scan
	 *  @param      int		$size        	0=original size, 1='small' use thumbnail if possible
	 *  @param      int		$nbmax       	Nombre maximum de photos (0=pas de max)
	 *  @param      int		$nbbyrow     	Number of image per line or -1 to use div. Used only if size=1.
	 * 	@param		int		$showfilename	1=Show filename
	 * 	@param		int		$showaction		1=Show icon with action links (resize, delete)
	 * 	@param		int		$maxHeight		Max height of original image when size='small' (so we can use original even if small requested). If 0, always use 'small' thumb image.
	 * 	@param		int		$maxWidth		Max width of original image when size='small'
	 *  @param      int     $nolink         Do not add a href link to view enlarged imaged into a new tab
	 *  @return     string					Html code to show photo. Number of photos shown is saved in this->nbphoto
	 */
	function show_photos($sdir,$size=0,$nbmax=0,$nbbyrow=5,$showfilename=0,$showaction=0,$maxHeight=120,$maxWidth=160,$nolink=0)
	{
		global $conf,$user,$langs;

		include_once DOL_DOCUMENT_ROOT .'/core/lib/files.lib.php';
		include_once DOL_DOCUMENT_ROOT .'/core/lib/images.lib.php';

		$dir = $sdir . '/';
		$pdir = '/';
		if (! empty($conf->global->PRODUCT_USE_OLD_PATH_FOR_PHOTO))
		{
			$dir .= get_exdir($this->id,2,0,0,$this,'product') . $this->id ."/photos/";
			$pdir .= get_exdir($this->id,2,0,0,$this,'product') . $this->id ."/photos/";
		}
		else
		{
			$dir .= get_exdir(0,0,0,0,$this,'product').$this->ref.'/';
			$pdir .= get_exdir(0,0,0,0,$this,'product').$this->ref.'/';
		}

		$dirthumb = $dir.'thumbs/';
		$pdirthumb = $pdir.'thumbs/';

		$return ='<!-- Photo -->'."\n";
		$nbphoto=0;

		$dir_osencoded=dol_osencode($dir);
		if (file_exists($dir_osencoded))
		{
			$handle=opendir($dir_osencoded);
            if (is_resource($handle))
            {
    			while (($file = readdir($handle)) != false)
    			{
    				$photo='';

    				if (! utf8_check($file)) $file=utf8_encode($file);	// To be sure file is stored in UTF8 in memory

    				if (dol_is_file($dir.$file) && preg_match('/('.$this->regeximgext.')$/i', $dir.$file))
    				{
    					$nbphoto++;
    					$photo = $file;
    					$viewfilename = $file;

    					if ($size == 1 || $size == 'small') {   // Format vignette

    						// Find name of thumb file
    						$photo_vignette=basename(getImageFileNameForSize($dir.$file, '_small', '.png'));
    						if (! dol_is_file($dirthumb.$photo_vignette)) $photo_vignette='';
    						
    						// Get filesize of original file
    						$imgarray=dol_getImageSize($dir.$photo);

    						if ($nbbyrow > 0)
    						{
    							if ($nbphoto == 1) $return.= '<table width="100%" valign="top" align="center" border="0" cellpadding="2" cellspacing="2">';

    							if ($nbphoto % $nbbyrow == 1) $return.= '<tr align=center valign=middle border=1>';
    							$return.= '<td width="'.ceil(100/$nbbyrow).'%" class="photo">';
    						}
    						else if ($nbbyrow < 0) $return .= '<div class="inline-block">';

    						$return.= "\n";
    						if (empty($nolink)) $return.= '<a href="'.DOL_URL_ROOT.'/viewimage.php?modulepart=product&entity='.$this->entity.'&file='.urlencode($pdir.$photo).'" class="aphoto" target="_blank">';

    						// Show image (width height=$maxHeight)
    						// Si fichier vignette disponible et image source trop grande, on utilise la vignette, sinon on utilise photo origine
    						$alt=$langs->transnoentitiesnoconv('File').': '.$pdir.$photo;
    						$alt.=' - '.$langs->transnoentitiesnoconv('Size').': '.$imgarray['width'].'x'.$imgarray['height'];
    						
    						if (empty($maxHeight) || $photo_vignette && $imgarray['height'] > $maxHeight)
    						{
    							$return.= '<!-- Show thumb -->';
    							$return.= '<img class="photo photowithmargin" border="0" '.($conf->dol_use_jmobile?'max-height':'height').'="'.$maxHeight.'" src="'.DOL_URL_ROOT.'/viewimage.php?modulepart=product&entity='.$this->entity.'&file='.urlencode($pdirthumb.$photo_vignette).'" title="'.dol_escape_htmltag($alt).'">';
    						}
    						else {
    							$return.= '<!-- Show original file -->';
    							$return.= '<img class="photo photowithmargin" border="0" '.($conf->dol_use_jmobile?'max-height':'height').'="'.$maxHeight.'" src="'.DOL_URL_ROOT.'/viewimage.php?modulepart=product&entity='.$this->entity.'&file='.urlencode($pdir.$photo).'" title="'.dol_escape_htmltag($alt).'">';
    						}

    						if (empty($nolink)) $return.= '</a>';
    						$return.="\n";

    						if ($showfilename) $return.= '<br>'.$viewfilename;
    						if ($showaction)
    						{
    							$return.= '<br>';
    							// On propose la generation de la vignette si elle n'existe pas et si la taille est superieure aux limites
    							if ($photo_vignette && preg_match('/('.$this->regeximgext.')$/i', $photo) && ($this->imgWidth > $maxWidth || $this->imgHeight > $maxHeight))
    							{
    								$return.= '<a href="'.$_SERVER["PHP_SELF"].'?id='.$this->id.'&amp;action=addthumb&amp;file='.urlencode($pdir.$viewfilename).'">'.img_picto($langs->trans('GenerateThumb'),'refresh').'&nbsp;&nbsp;</a>';
    							}
    							if ($user->rights->produit->creer || $user->rights->service->creer)
    							{
    								// Link to resize
    			               		$return.= '<a href="'.DOL_URL_ROOT.'/core/photos_resize.php?modulepart='.urlencode('produit|service').'&id='.$this->id.'&amp;file='.urlencode($pdir.$viewfilename).'" title="'.dol_escape_htmltag($langs->trans("Resize")).'">'.img_picto($langs->trans("Resize"),DOL_URL_ROOT.'/theme/common/transform-crop-and-resize','',1).'</a> &nbsp; ';

    			               		// Link to delete
    								$return.= '<a href="'.$_SERVER["PHP_SELF"].'?id='.$this->id.'&amp;action=delete&amp;file='.urlencode($pdir.$viewfilename).'">';
    								$return.= img_delete().'</a>';
    							}
    						}
    						$return.= "\n";

    						if ($nbbyrow > 0)
    						{
    							$return.= '</td>';
    							if (($nbphoto % $nbbyrow) == 0) $return.= '</tr>';
    						}
    						else if ($nbbyrow < 0) $return.='</div>';
    					}

    					if (empty($size)) {     // Format origine
    						$return.= '<img class="photo photowithmargin" border="0" src="'.DOL_URL_ROOT.'/viewimage.php?modulepart=product&entity='.$this->entity.'&file='.urlencode($pdir.$photo).'">';

    						if ($showfilename) $return.= '<br>'.$viewfilename;
    						if ($showaction)
    						{
    							if ($user->rights->produit->creer || $user->rights->service->creer)
    							{
    								// Link to resize
    			               		$return.= '<a href="'.DOL_URL_ROOT.'/core/photos_resize.php?modulepart='.urlencode('produit|service').'&id='.$this->id.'&amp;file='.urlencode($pdir.$viewfilename).'" title="'.dol_escape_htmltag($langs->trans("Resize")).'">'.img_picto($langs->trans("Resize"),DOL_URL_ROOT.'/theme/common/transform-crop-and-resize','',1).'</a> &nbsp; ';

    			               		// Link to delete
    			               		$return.= '<a href="'.$_SERVER["PHP_SELF"].'?id='.$this->id.'&amp;action=delete&amp;file='.urlencode($pdir.$viewfilename).'">';
    								$return.= img_delete().'</a>';
    							}
    						}
    					}

    					// On continue ou on arrete de boucler ?
    					if ($nbmax && $nbphoto >= $nbmax) break;
    				}
    			}
            }

			if ($size==1 || $size='small')
			{
				if ($nbbyrow > 0)
				{
					// Ferme tableau
					while ($nbphoto % $nbbyrow)
					{
						$return.= '<td width="'.ceil(100/$nbbyrow).'%">&nbsp;</td>';
						$nbphoto++;
					}

					if ($nbphoto) $return.= '</table>';
				}
			}

			closedir($handle);
		}

		$this->nbphoto = $nbphoto;

		return $return;
	}
Ejemplo n.º 3
0
/**
 *    	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     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)
{
    require_once DOL_DOCUMENT_ROOT . '/core/lib/functions2.lib.php';
    global $conf, $langs;
    dol_syslog("vignette file=" . $file . " extName=" . $extName . " maxWidth=" . $maxWidth . " maxHeight=" . $maxHeight . " quality=" . $quality . " outdir=" . $outdir . " targetformat=" . $targetformat);
    // Clean parameters
    $file = trim($file);
    // Check parameters
    if (!$file) {
        // Si le fichier n'a pas ete indique
        return 'ErrorBadParameters';
    } elseif (!file_exists($file)) {
        // Si le fichier passe en parametre n'existe pas
        dol_syslog($langs->trans("ErrorFileNotFound", $file), LOG_ERR);
        return $langs->trans("ErrorFileNotFound", $file);
    } elseif (image_format_supported($file) < 0) {
        dol_syslog('This file ' . $file . ' does not seem to be an image format file name.', LOG_WARNING);
        return 'ErrorBadImageFormat';
    } elseif (!is_numeric($maxWidth) || empty($maxWidth) || $maxWidth < -1) {
        // Si la largeur max est incorrecte (n'est pas numerique, est vide, ou est inferieure a 0)
        dol_syslog('Wrong value for parameter maxWidth', LOG_ERR);
        return 'Error: Wrong value for parameter maxWidth';
    } elseif (!is_numeric($maxHeight) || empty($maxHeight) || $maxHeight < -1) {
        // Si la hauteur max est incorrecte (n'est pas numerique, est vide, ou est inferieure a 0)
        dol_syslog('Wrong value for parameter maxHeight', LOG_ERR);
        return 'Error: Wrong value for parameter maxHeight';
    }
    $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
    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
    // Si l'image est plus petite que la largeur et la hauteur max, on ne cree pas de vignette
    if ($infoImg[0] < $maxWidth && $infoImg[1] < $maxHeight) {
        // On cree toujours les vignettes
        dol_syslog("File size is smaller than thumb size", LOG_DEBUG);
        //return 'Le fichier '.$file.' ne necessite pas de creation de vignette';
    }
    $imgfonction = '';
    switch ($infoImg[2]) {
        case IMAGETYPE_GIF:
            // 1
            $imgfonction = 'imagecreatefromgif';
            break;
        case IMAGETYPE_JPEG:
            // 2
            $imgfonction = 'imagecreatefromjpeg';
            break;
        case IMAGETYPE_PNG:
            // 3
            $imgfonction = 'imagecreatefrompng';
            break;
        case IMAGETYPE_BMP:
            // 6
            // Not supported by PHP GD
            break;
        case IMAGETYPE_WBMP:
            // 15
            $imgfonction = 'imagecreatefromwbmp';
            break;
    }
    if ($imgfonction) {
        if (!function_exists($imgfonction)) {
            // Fonctions de conversion non presente dans ce PHP
            return 'Error: Creation of thumbs not possible. This PHP does not support GD function ' . $imgfonction;
        }
    }
    // On cree le repertoire contenant les vignettes
    $dirthumb = $dir . ($outdir ? '/' . $outdir : '');
    // Chemin du dossier contenant les vignettes
    dol_mkdir($dirthumb);
    // Initialisation des variables selon l'extension de l'image
    switch ($infoImg[2]) {
        case IMAGETYPE_GIF:
            // 1
            $img = imagecreatefromgif($fichier);
            $extImg = '.gif';
            // Extension de l'image
            break;
        case IMAGETYPE_JPEG:
            // 2
            $img = imagecreatefromjpeg($fichier);
            $extImg = preg_match('/\\.jpeg$/', $file) ? '.jpeg' : '.jpg';
            // Extension de l'image
            break;
        case IMAGETYPE_PNG:
            // 3
            $img = imagecreatefrompng($fichier);
            $extImg = '.png';
            break;
        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;
    }
    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
    if ($whFact < $imgWhFact) {
        // Si largeur determinante
        $thumbWidth = $maxWidth;
        $thumbHeight = $thumbWidth / $imgWhFact;
    } else {
        // Si hauteur determinante
        $thumbHeight = $maxHeight;
        $thumbWidth = $thumbHeight * $imgWhFact;
    }
    $thumbHeight = round($thumbHeight);
    $thumbWidth = round($thumbWidth);
    // Define target format
    if (empty($targetformat)) {
        $targetformat = $infoImg[2];
    }
    // Create empty image
    if ($targetformat == IMAGETYPE_GIF) {
        // Compatibilite image GIF
        $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);
    }
    // Initialisation des variables selon l'extension de l'image
    switch ($targetformat) {
        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 IMAGETYPE_JPEG:
            // 2
            $trans_colour = imagecolorallocatealpha($imgThumb, 255, 255, 255, 0);
            $extImgTarget = preg_match('/\\.jpeg$/i', $file) ? '.jpeg' : '.jpg';
            $newquality = $quality;
            break;
        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 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';
            break;
    }
    if (function_exists("imagefill")) {
        imagefill($imgThumb, 0, 0, $trans_colour);
    }
    dol_syslog("vignette: convert image from ({$imgWidth} x {$imgHeight}) to ({$thumbWidth} x {$thumbHeight}) as {$extImg}, newquality={$newquality}");
    //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
    $fileName = preg_replace('/(\\.gif|\\.jpeg|\\.jpg|\\.png|\\.bmp)$/i', '', $file);
    // On enleve extension quelquesoit la casse
    $fileName = basename($fileName);
    //$imgThumbName = $dirthumb.'/'.getImageFileNameForSize(basename($file), $extName, $extImgTarget);   // Full path of thumb file
    $imgThumbName = getImageFileNameForSize($file, $extName, $extImgTarget);
    // Full path of thumb file
    // Check if permission are ok
    //$fp = fopen($imgThumbName, "w");
    //fclose($fp);
    // Create image on disk
    switch ($targetformat) {
        case IMAGETYPE_GIF:
            // 1
            imagegif($imgThumb, $imgThumbName);
            break;
        case IMAGETYPE_JPEG:
            // 2
            imagejpeg($imgThumb, $imgThumbName, $newquality);
            break;
        case IMAGETYPE_PNG:
            // 3
            imagepng($imgThumb, $imgThumbName, $newquality);
            break;
        case IMAGETYPE_BMP:
            // 6
            // Not supported by PHP GD
            break;
        case IMAGETYPE_WBMP:
            // 15
            image2wbmp($imgThumb, $imgThumbName);
            break;
    }
    // Set permissions on file
    if (!empty($conf->global->MAIN_UMASK)) {
        @chmod($imgThumbName, octdec($conf->global->MAIN_UMASK));
    }
    // Free memory. This does not delete image.
    imagedestroy($img);
    imagedestroy($imgThumb);
    return $imgThumbName;
}