Found a great function for image resize in php. It requires php >= 4.3.0.
I found the source in
http://www.akemapa.com/2008/07/10/php-gd-resize-transparent-image-png-gif/
<?
function resize($img, $w, $h, $newfilename) {
#
//Check if GD extension is loaded
#
if (!extension_loaded('gd') && !extension_loaded('gd2')) {
#
trigger_error("GD is not loaded", E_USER_WARNING);
#
return false;
#
}
#
//Get Image size info
$imgInfo = getimagesize($img);
#
switch ($imgInfo[2]) {
#
case 1: $im = imagecreatefromgif($img); break;
#
case 2: $im = imagecreatefromjpeg($img); break;
#
case 3: $im = imagecreatefrompng($img); break;
#
default: trigger_error('Unsupported filetype!', E_USER_WARNING); break;
#
}
#
//If image dimension is smaller, do not resize
#
if ($imgInfo[0] <= $w && $imgInfo[1] <= $h) {
#
$nHeight = $imgInfo[1];
#
$nWidth = $imgInfo[0];
#
}else{
#
//yeah, resize it, but keep it proportional
#
$height = $imgInfo[1];
$width = $imgInfo[0];
$mheight = $h;
$mwidth = $w;
#
if( $height >= $width) {
$newheight = $mheight;
$newwidth = number_format( ( ($mheight/$height)*$width ) ,2,".","");
if ( $newwidth > $mwidth ) {
$temp = $newwidth;
$newwidth = $mwidth;
$newheight = number_format ( ( ($newwidth/$temp)*$newheight ),2,".","");
}
} elseif ($width > $height) {
$newwidth = $mwidth;
$newheight = number_format( ( ($mwidth/$width)*$height ) ,2,".","");
if ( $newheight > $mheight ) {
$temp = $newheight;
$newheight = $mheight;
$newwidth = number_format( ( ($newheight/$temp)*$newwidth ) ,2,".","");
}
}
$nWidth = $newwidth;
$nHeight = $newheight;
#
}
#
$nWidth = round($nWidth);
$nHeight = round($nHeight);
#
$newImg = imagecreatetruecolor($nWidth, $nHeight);
#
/* Check if this image is PNG or GIF, then set if Transparent*/
#
if(($imgInfo[2] == 1) OR ($imgInfo[2]==3)){
#
imagealphablending($newImg, false);
imagesavealpha($newImg,true);
$transparent = imagecolorallocatealpha($newImg, 255, 255, 255, 127);
imagefilledrectangle($newImg, 0, 0, $nWidth, $nHeight, $transparent);
}
#
imagecopyresampled($newImg, $im, 0, 0, 0, 0, $nWidth, $nHeight, $imgInfo[0], $imgInfo[1]);
#
//Generate the file, and rename it to $newfilename
#
switch ($imgInfo[2]) {
#
case 1: imagegif($newImg,$newfilename); break;
case 2: imagejpeg($newImg,$newfilename); break;
case 3: imagepng($newImg,$newfilename); break;
default: trigger_error('Failed resize image!', E_USER_WARNING); break;
}
#
return $newfilename;
//return $newImg;
#
}
?>