The m is for maus~. I do what I please. About m~, The Archives and the Lifestream RSS

Resize an Image Proportionally

I’ve solved this a lot of times in various scripting and programming languages. This seems to be the most elegant solution (read: less code doing the exact same stuff as more code).

function get_resizeDimensions ($whateverParameters) {
	// or you could just know them but it's besides
	// the point. Just get them somehow.
	list($width, $height) = getimagesize($imagePath);
	$widthRatio = $maxWidth / $width;
	$heightRatio = $maxHeight / $height;
	// this is the brilliance of it
	$ratio = min($heightRatio, $widthRatio);
	// get new dimensions and return them
	$width = intval($ratio * $width);
	$height = intval($ratio * $height);

	return array($width, $height);