Salve
vi faccio vedere una mia funzione (mia si fa per dire... l'ho scopiazzata e riadattata).
Codice PHP:
function make_thumb($img_name, $filename, $new_w, $new_h)
{
// flag per decidere se l'immagine è stata ridimensionata o no
$resized = true;
// ricavo l'estensione
$ext = getExtension($img_name);
// creo la nuova immagine tramite la GCD Library - solo GIF, JPG e PNG
if(!strcmp("jpg",$ext) || !strcmp("jpeg",$ext))
$src_img = imagecreatefromjpeg($img_name);
if(!strcmp("png",$ext))
$src_img = imagecreatefrompng($img_name);
if(!strcmp("gif",$ext))
$src_img = imagecreatefromgif($img_name);
// ricavo le dimensioni dell'immagine
$old_x = imagesx($src_img);
$old_y = imagesy($src_img);
if($old_x<=$new_w && $old_y<=$new_h)
{
$resized=false;
}
else
{
$ratio1 = $old_x/$new_w;
$ratio2 = $old_y/$new_h;
if($ratio1>$ratio2)
{
$thumb_w = $new_w;
$thumb_h = $old_y/$ratio1;
}
else
{
$thumb_w = $old_x/$ratio2;
$thumb_h = $new_h;
}
$dst_img = ImageCreateTrueColor($thumb_w, $thumb_h);
imagecopyresampled($dst_img,$src_img,0,0,0,0,$thumb_w, $thumb_h,$old_x, $old_y);
}
if($resized)
{
if(!strcmp("png","$ext"))
imagepng($dst_img,$filename);
if(!strcmp("gif","$ext"))
imagegif($dst_img,$filename);
if(!strcmp("jpg","$ext") || !strcmp("jpeg","$ext"))
imagejpeg($dst_img,$filename);
imagedestroy($src_img);
imagedestroy($dst_img);
}
else
{
if(!strcmp("png","$ext"))
imagepng($src_img,$filename);
if(!strcmp("gif","$ext"))
imagegif($src_img,$filename);
if(!strcmp("jpg","$ext") || !strcmp("jpeg","$ext"))
imagejpeg($src_img,$filename);
imagedestroy($src_img);
}
}
Il problema: se ridimensiono immagini .png (trasparenti magari) mi ci inserisce uno sfondo nero.
Immagino ci sia un qualche parametro di una qualche funzione che dice "sfondo bianco".. ma non saprei dove..
idee brillanti ben accette!