function resize($newWidth, $targetFile, $originalFile) {
$mime = $info['mime'];
switch ($mime) {
case 'image/jpeg':
$image_create_func = 'imagecreatefromjpeg';
$image_save_func = 'imagejpeg';
$new_image_ext = 'jpg';
break;
case 'image/png':
$image_create_func = 'imagecreatefrompng';
$image_save_func = 'imagepng';
$new_image_ext = 'png';
break;
case 'image/gif':
$image_create_func = 'imagecreatefromgif';
$image_save_func = 'imagegif';
$new_image_ext = 'gif';
break;
default:
throw Exception('Unknown image type.');
}
$img = $image_create_func($originalFile);
if ((!$width) || (!$height)) { $GLOBALS['errors'][] = 'Image couldn\'t be resized because it wasn\'t a valid image.'; return false; }
if (($width <= $newWidth) && ($height <= $newWidth)) { return $image; } //no resizing needed
//try max width first...
$ratio = $newWidth / $width;
$new_w = $newWidth;
$new_h = $height * $ratio;
//if that didn't work
if ($new_h > $newWidth) {
$ratio = $newWidth / $height;
$new_h = $newWidth;
$new_w = $width * $ratio;
}
}
$image_save_func($tmp, "$targetFile.$new_image_ext");
}