The code is the same as Sven's, I just have som extra calculations of the new width and height.
Anyways, here is the whole function:
PHP Code:
function resize_image($resize_limit, $source, $target) {
list($img_width, $img_height, $img_type, $attr) = getimagesize($source);
if ($img_width <= $img_height) {
$new_height = $resize_limit;
// Calculate the thunmbnail sizes
$ratio = (int) $img_width / (int) $img_height;
$new_width = ceil( (int) $resize_limit * (float) $ratio );
}
if ($img_height <= $img_width) {
$new_width = $resize_limit;
// Calculate the thunmbnail sizes
$ratio = (int) $img_height / (int) $img_width;
$new_height = ceil( (int) $resize_limit * (float) $ratio );
}
if ($img_width == $img_height) {
// Sets the correct height and width
$new_width = $resize_limit;
$new_height = $resize_limit;
}
// create a new image-resource
$thumb = imagecreatetruecolor($new_width, $new_height);
$new_source = imagecreatefromjpeg($source);
// Creates the thumbnail
imagecopyresampled($thumb, $new_source, 0, 0, 0, 0, $new_width, $new_height, $img_width, $img_height);
imagejpeg($thumb, $target, 100);
}
Bookmarks