Bonjour,
comment dire à GD qu'il me mette un fond transparent et nonblanc lorsqu'il me crée ma miniature ?
Car je dois afficher mon image sur des fonds aux couleurs différents, et c'est moche si ce n'est pas transparent...
imagecolorallocate($dst_img, 255, 255, 255);
--> 255,255,25 c'est pour le fond blanc, mais pour mettre de la transparence ?
Voici la fonction complète de la création de ma miniature :
function resize($source, $destination, $width, $height, $stretch, $quality) {
// 1 - Gestion des erreurs :
if (!file_exists($source)){
echo "Erreur : Fichier inexistant !";
return false;
}
if (!function_exists("ImageCreateFromJpeg")){
echo "Erreur : Librairie GD non installée !";
return false;
}
// 2 - Lecture de l'image et de son extension :
$ext = strtolower(strrchr(basename($source),'.'));
if($ext==".jpg" || $ext==".jpe" || $ext==".jpeg"){
$src_img=imagecreatefromjpeg($source);
}else{
if($ext==".gif"){
$src_img=imagecreatefromgif($source);
}else{
if($ext==".png"){
$src_img=imagecreatefrompng($source);
}else{
echo "Erreur : Extension incorrecte.";
}
}
}
if (!$src_img){
echo "Erreur : Lecture impossible de l'image !";
return false;
}
$w = imagesx($src_img);
$h = imagesy($src_img);
// 3 - Redimensionnement en largeur :
if (!$stretch) {
if ((($h * $width) / $w) > $height) {
$im_w = ($w * $height) / $h;
$im_h = $height;
} else {
$im_w = $width;
$im_h = ($h * $width) / $w;
}
} else {
$im_w = $width;
$im_h = $height;
}
$x = ($width-$im_w)/2;
$y = ($height-$im_h)/2;
// 4 - Création d'une image buffer :
$dst_img = imagecreatetruecolor($width, $height);
if (!$dst_img){
echo "Erreur : Buffer '".$dst_img."' non créé !";
return false;
}
$bgc = imagecolorallocate($dst_img, 248, 230, 244);
imagefilledrectangle($dst_img, 0, 0, $width, $height, $bgc);
imagecopyresized($dst_img,$src_img,$x,$y,0,0,$im_w,$im_h,$w,$h);
// 5 - Enregistrement de la miniature en .jpg :
touch($destination);
imagejpeg($dst_img,$destination,$quality);
return true;
}
comment dire à GD qu'il me mette un fond transparent et nonblanc lorsqu'il me crée ma miniature ?
Car je dois afficher mon image sur des fonds aux couleurs différents, et c'est moche si ce n'est pas transparent...
imagecolorallocate($dst_img, 255, 255, 255);
--> 255,255,25 c'est pour le fond blanc, mais pour mettre de la transparence ?
Voici la fonction complète de la création de ma miniature :
function resize($source, $destination, $width, $height, $stretch, $quality) {
// 1 - Gestion des erreurs :
if (!file_exists($source)){
echo "Erreur : Fichier inexistant !";
return false;
}
if (!function_exists("ImageCreateFromJpeg")){
echo "Erreur : Librairie GD non installée !";
return false;
}
// 2 - Lecture de l'image et de son extension :
$ext = strtolower(strrchr(basename($source),'.'));
if($ext==".jpg" || $ext==".jpe" || $ext==".jpeg"){
$src_img=imagecreatefromjpeg($source);
}else{
if($ext==".gif"){
$src_img=imagecreatefromgif($source);
}else{
if($ext==".png"){
$src_img=imagecreatefrompng($source);
}else{
echo "Erreur : Extension incorrecte.";
}
}
}
if (!$src_img){
echo "Erreur : Lecture impossible de l'image !";
return false;
}
$w = imagesx($src_img);
$h = imagesy($src_img);
// 3 - Redimensionnement en largeur :
if (!$stretch) {
if ((($h * $width) / $w) > $height) {
$im_w = ($w * $height) / $h;
$im_h = $height;
} else {
$im_w = $width;
$im_h = ($h * $width) / $w;
}
} else {
$im_w = $width;
$im_h = $height;
}
$x = ($width-$im_w)/2;
$y = ($height-$im_h)/2;
// 4 - Création d'une image buffer :
$dst_img = imagecreatetruecolor($width, $height);
if (!$dst_img){
echo "Erreur : Buffer '".$dst_img."' non créé !";
return false;
}
$bgc = imagecolorallocate($dst_img, 248, 230, 244);
imagefilledrectangle($dst_img, 0, 0, $width, $height, $bgc);
imagecopyresized($dst_img,$src_img,$x,$y,0,0,$im_w,$im_h,$w,$h);
// 5 - Enregistrement de la miniature en .jpg :
touch($destination);
imagejpeg($dst_img,$destination,$quality);
return true;
}