I just wrote a tiny class for that... you'll have fun reading it, it's coded in French ! 😛
<?php
class NZAU_Redimensionner_Image {
function __construct($intL, $intH, $strFormat_Destination = 'png', $bolContour_Vide = true, $arrFond_Destination = array(0, 0, 0), $bolTransparente = false) {
$this->arrDimensions = array($intL, $intH);
$this->bolContour_Vide = $bolContour_Vide;
$this->strFormat_Destination = $strFormat_Destination;
$this->arrFond_Destination = $arrFond_Destination;
$this->bolTransparente = $bolTransparente;
}
function retournerErreur($strFonction, $strErreur){
echo __CLASS__ . '->' . $strFonction . '() : ' . $strErreur . '.<br />' . "\n";
return false;
}
function retournerRedimensionner($strFichier_Origine, $strFichier_Destination) {
if(!is_readable($strFichier_Origine)) {
return $this->retournerErreur(__FUNCTION__, 'impossible de lire le fichier d\'origine');
} elseif(!is_writable(dirname($strFichier_Destination))) {
return $this->retournerErreur(__FUNCTION__, 'impossible d\'écrire dans le répertoire de destination');
}
switch(exif_imagetype($strFichier_Origine)) {
case IMAGETYPE_GIF:
$resImage_Origine = imagecreatefromgif($strFichier_Origine);
break;
case IMAGETYPE_JPEG:
$resImage_Origine = imagecreatefromjpeg($strFichier_Origine);
break;
case IMAGETYPE_PNG:
$resImage_Origine = imagecreatefrompng($strFichier_Origine);
break;
case IMAGETYPE_WBMP:
$resImage_Origine = imagecreatefromwbmp($strFichier_Origine);
break;
case IMAGETYPE_XBM:
$resImage_Origine = imagecreatefromxbm($strFichier_Origine);
break;
default:
return $this->retournerErreur(__FUNCTION__, 'le type de l\'image d\'origine n\'est pas valide.');
break;
}
$intRatio = max($this->arrDimensions[0], $this->arrDimensions[1]) / max(imagesx($resImage_Origine), imagesy($resImage_Origine));
$arrDimensions_Destination = array(round(imagesx($resImage_Origine) * $intRatio), round(imagesy($resImage_Origine) * $intRatio));
$resImage_Destination = imagecreate((($this->bolContour_Vide) ? $arrDimensions_Destination[0] : 100), (($this->bolContour_Vide) ? $arrDimensions_Destination[1] : 100));
$resCouleur = imagecolorallocate($resImage_Destination, $this->arrFond_Destination[0], $this->arrFond_Destination[1], $this->arrFond_Destination[2]);
if($this->bolTransparente) {
imagecolortransparent($resImage_Destination, $resCouleur);
}
if(!@imagecopyresampled($resImage_Destination, $resImage_Origine, (($this->bolContour_Vide) ? 0 : (round(($this->arrDimensions[0] - $arrDimensions_Destination[0]) / 2))), (($this->bolContour_Vide) ? 0 : (round(($this->arrDimensions[1] - $arrDimensions_Destination[1]) / 2))), 0, 0, $arrDimensions_Destination[0], $arrDimensions_Destination[1], imagesx($resImage_Origine), imagesy($resImage_Origine))) {
return $this->retournerErreur(__FUNCTION__, 'impossible de redimensionner l\'image');
} else {
switch($this->strFormat_Destination) {
case 'gd':
return imagegd($resImage_Destination, $strFichier_Destination);
break;
case 'gd2':
return imagegd2($resImage_Destination, $strFichier_Destination);
break;
case 'jpeg':
return imagejpeg($resImage_Destination, $strFichier_Destination, 100);
break;
case 'png':
return imagepng($resImage_Destination, $strFichier_Destination);
break;
case 'wbmp':
return imagewbmp($resImage_Destination, $strFichier_Destination);
break;
case 'xbm':
return imagexbm($resImage_Destination, $strFichier_Destination);
break;
default:
return $this->retournerErreur(__FUNCTION__, 'format de destination inconnu');
break;
}
}
}
}
$objNZAU_RI = new NZAU_Redimensionner_Image(100, 100);
$objNZAU_RI->retournerRedimensionner('C:/Temp/moi4.jpg', 'C:/Temp/moi4_redim.png');
?>
When you initiate the class, you have to/can define these parameters :
$intL : the (maximum) width of the thumbnail
$intH : the (maximum) height of the thumbnail
$strFormat_Destination = 'png' : the format of the thumbnail
$bolContour_Vide = true : if true, the thumbnail won't be exactly W x H, either the width or the height will be smaller, if false, the thumbnail will have a width and a height you specified with $intL and $intH
$arrFond_Destination = array(0, 0, 0) : the background color
$bolTransparente = false : if true, the image will be transparent (if you use the png format)
Then, call the retournerRedimensionner() function with two parameters : the first, the "origin image", the second, "the destination image".
That's it.