I whipped this function up really quickly. It should do what you want
PHP Code:
<?php
/*
* Name: resizeImage.php
* Version: 1.0
* Author: John Collins celtboy[at]gmail.com
*
* Purpose:
* Function takes an image and maximum width and height,
* and returns new width and height based on maximum values.
* It returns an associative array with the new width and height
*/
function resizeImage($max_width, $max_height, $image_file) {
/* image, width & height variables */
$image_info = getImageSize($image_file);
$width = $image_info[0];
$height = $image_info[1];
/* our return values, default to current image dimensions */
$ar_return = array();
$ar_return["width"] = $width;
$ar_return["height"] = $height;
/* Only perform if current dimensions are outside max */
if (($width > $max_width) || ($height > $max_height)) {
if ($width >= $height) {
$ar_return["width"] = (int)($max_width);
$ar_return["height"] = (int)($height * ($max_width / $width));
} else {
$ar_return["height"] = (int)($max_height);
$ar_return["width"] = (int)($width * ($max_height / $height));
}
}
return $ar_return;
} // resizeImage()
?>
Sample usage:
PHP Code:
<?php
include("resizeImage.php");
/* Image Variables */
$img_source = "my_image.jpg";
$max_width = 200;
$max_height = 400;
$new_image_data = resizeImage($max_width, $max_height, $img_source);
/* Store results in simple vars */
$width = $new_image_data["width"];
$height = $new_image_data["height"];
/* Print resized image */
print "<img src=\"" . $img_source . "\" width=\"" . $width . "\" height=\"" . $height . "\" alt=\"Image of: " . $img_source . "\" />";
?>
hth,
-Celt