1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
|
<?php
//Version 1.0
//Creation d'une miniature d'une photo
//$nom -> Nom du fichier sur le disque dur
//$nom_t -> nom du fichier thumb a creer
//$imagethumbsize -> taille du thumb
//$c -> Compression
//ATTENTION, seul les formats JPeg et PNG sont supportés.
function genthumb($path,$nom,$nom_t,$imagethumbsize,$c)
{
list($width, $height) = getimagesize($path.$nom);
//Le thumb ne peut pas dépasser les $imagethumbsize * $imagethumbsize
if ($width>$height)
{
$th_h=(int)($height*$imagethumbsize)/$width;
$th_w=$imagethumbsize;
}
else
{
$th_w=(int)($width*$imagethumbsize)/$height;
$th_h=$imagethumbsize;
}
// résérvation d'espace mémoire de la taille du thumb
$image_t = imagecreatetruecolor($th_w , $th_h); // true color for best quality
if(exif_imagetype($path.$nom)==IMAGETYPE_JPEG) // C'est une Jpeg
{
$image = imagecreatefromjpeg($path.$nom);
}
else // C'est une PNG
{
$image = imagecreatefrompng($path.$nom);
$nom=basename($nom,".png").'.jpg'; // la miniature sera au format jpg
}
// on retaille l'image
imagecopyresampled($image_t, $image,0,0,0,0,$th_w,$th_h,$width,$height);
//Sauvegarde de l'image dans un fichier, avec la compression $c
imagejpeg($image_t, $path.$nom_t, $c);
//Libére la ram
imagedestroy($image);
imagedestroy($image_t);
}
// Transforme un gif en PNG
function gif2png($nomgif,$nompng)
{
if (imagetypes() & IMG_GIF)
{
$p=imagecreatefromgif($nomgif);
if ($p)
{
imagepng($p,$nompng);
imagedestroy($p);
return 1;
}
else return 0;
}
else return 0;
}
?> |
Partager