Comment

Christians More Supportive of Torture Than Non-Religious Americans

35
Charles Johnson12/20/2014 1:12:57 pm PST

Here’s a really tiny, efficient function for proportionally scaling a rectangle, by passing in either a new width or a new height. It returns an associative array with the scaled width and height.

This is used in the code that does the image previews for YouTube and Vimeo videos.

<?php
function imageScale($srcWidth, $srcHeight, $newWidth, $newHeight = FALSE) {
	$ratio = $newHeight
		? min($newWidth / $srcWidth, $newHeight / $srcHeight)
		: $newWidth / $srcWidth;
	return array(
		'width'		=> ceil($srcWidth * $ratio),
		'height'	=> ceil($srcHeight * $ratio)
	);
}
?>