I found Ian Atkin's solution here - Resize image in PHP - and am trying to modify it to accommodate png and gif, and to save the resized image. Jpg works, but I'm getting black images (of correct size) for png and gif. I can't figure out what am I doing wrong. Here's the code:
function resize_image($file, $w, $h, $crop=FALSE) {
list($width, $height) = getimagesize($file);
$r = $width / $height;
if ($crop) {
if ($width > $height) {
$width = ceil($width-($width*abs($r-$w/$h)));
} else {
$height = ceil($height-($height*abs($r-$w/$h)));
}
$newwidth = $w;
$newheight = $h;
} else {
if ($w/$h > $r) {
$newwidth = $h*$r;
$newheight = $h;
} else {
$newheight = $w/$r;
$newwidth = $w;
}
}
if($extension=="gif") {
$src = imagecreatefromgif($file);
} elseif($extension=="png") {
$src = imagecreatefrompng($file);
} else {
$src = imagecreatefromjpeg($file);
}
$dst = imagecreatetruecolor($newwidth, $newheight);
imagecopyresampled($dst, $src, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
if($extension=="gif") {
imagegif($dst, $file);
} elseif($extension=="png") {
imagepng($dst, $file);
} else {
imagejpeg($dst, $file);
}
return $dst;
}
Thank you very much!