/*------------------------------*/
|
/*
|
Titre : Créer une vignette avec GD en se basant sur le type d'image
|
|
Date édition : 13 Jan 2010
|
*/
|
/*------------------------------*/
|
|
|
// image source
|
$source_image = 'image1.jpg';
|
|
// maximum hauteur
|
$thumb_height = 40;
|
|
// prefix de l'image
|
$thumb_prefix = 'thumb_';
|
|
// la qualité de l'image
|
$quality = 100;
|
|
// regarde l'existence du fichier
|
if(!file_exists($source_image)) {
|
echo 'No file found';
|
}
|
else
|
{
|
// types supportés
|
$supported_types = array(1, 2, 3, 7);
|
|
// prend des infos de l'image
|
list($width_orig, $height_orig, $image_type) = getimagesize(
|
$source_image);
|
|
// regarde si le type est supporté
|
if(!in_array($image_type, $supported_types))
|
{
|
echo 'Unsupported Image Type: ' . $image_type;
|
}
|
else
|
{
|
|
// prend le nom du fichier
|
$path_parts = pathinfo($source_image);
|
$filename = $path_parts['filename'];
|
|
// calcul le ration
|
$aspect_ratio = (float) $width_orig / $height_orig;
|
|
// calcul l'image sur la base hauteur
|
$thumb_width = round($thumb_height * $aspect_ratio);
|
|
|
// imagecreatefromstring va automatiquement detecter le type de l'image
|
$source = imagecreatefromstring(file_get_contents($source_image));
|
|
// creer le canvas
|
$thumb = imagecreatetruecolor($thumb_width, $thumb_height);
|
|
// map l'image
|
imagecopyresampled($thumb, $source, 0, 0, 0, 0, $thumb_width,
|
$thumb_height, $width_orig, $height_orig);
|
// detruit la source
|
imagedestroy($source);
|
|
// ecrit l'image
|
switch ( $image_type )
|
{
|
case 1:
|
imagegif($im, $fileName);
|
$thumbnail = $thumb_prefix.$filename . '.gif';
|
break;
|
|
case 2:
|
$thumbnail = $thumb_prefix.$filename . '.jpg';
|
imagejpeg($thumb, $thumbnail, $quality);
|
break;
|
|
case 3:
|
imagepng($im, $fileName);
|
$thumbnail = $thumb_prefix.$filename . '.png';
|
break;
|
|
case 7:
|
imagewbmp($im, $fileName);
|
$thumbnail = $thumb_prefix.$filename . '.bmp';
|
break;
|
}
|
}
|
}
|
| ?> |
Invité
05 Déc 2017 à 10:23Bon début mais le code dans les case est bon uniquement pour le jpeg, les variable $im et $filename dans les autre case ne sont pas bonne, il font ce baser sur le cas du jpg pour corriger cela