function resize($file, $newfile, $width = 0, $height = 0) {
$imginfo = getimagesize($file);
$info = array(
'width' => $imginfo[0],
'height' => $imginfo[1],
'bits' => $imginfo['bits'],
'mime' => $imginfo['mime']
);
$mime = $info['mime'];
if ($mime == 'image/gif')
$image = imagecreatefromgif($image);
elseif ($mime == 'image/png')
$image = imagecreatefrompng($image);
elseif ($mime == 'image/jpeg')
$image = imagecreatefromjpeg($image);
$xpos = 0;
$ypos = 0;
$scale = min($width / $info['width'], $height / $info['height']);
$new_width = (int)($info['width'] * $scale);
$new_height = (int)($info['height'] * $scale);
$xpos = (int)(($width - $new_width) / 2);
$ypos = (int)(($height - $new_height) / 2);
$newimage = imagecreatetruecolor($width, $height);
if (isset($info['mime']) and $info['mime'] == 'image/png') {
imagealphablending($newimage, false);
imagesavealpha($newimage, true);
$background = imagecolorallocatealpha($newimage, 255, 255, 255, 127);
imagecolortransparent($newimage, $background);
} else {
$background = imagecolorallocate($newimage, 255, 255, 255);
}
imagefilledrectangle($this->image, 0, 0, $width, $height, $background);
imagecopyresampled($newimage, $image, $xpos, $ypos, 0, 0, $new_width, $new_height, $this->info['width'], $this->info['height']);
if ($mime == 'image/gif') {
imagegif($newimage, $newfile);
} elseif ($mime == 'image/png') {
imagepng($newimage, $newfile, 0);
} elseif ($mime == 'image/jpeg') {
imagejpeg($newimage, $newfile, 80);
}
imagedestroy($image);
imagedestroy($newimage);
}