Posted Friday, November 25th 2011, at 3:13pm
Here's a function that will take a JPG or PNG image file and resize it to the dimensions you want. It maintains the aspect ratio of the original file even if the dimensions do not match, so anything that falls outside of the sample area will be cropped. I wrote this function for use with Wordpress, but if you're running PHP it wouldn't be hard to adapt for your purposes. I use the original image ID (given by Wordpress) to name the new file. You obviously don't have to use this. If you do, refer to my last post for a function that will return the ID of an image based on a given URL.
function fixedCropImage($originalImage, $newImageLoc, $originalImageID, $destwidth, $destheight) {
	//get the blog URL and add a slash to the end.
	$theWPurl = get_bloginfo('wpurl').'/';
	//find the relative path of the original file. This is useful if you use a testing server.
	$originalFileRelativePath = str_replace($theWPurl, '', $originalImage);
	//check to see if the file we're asking to resize exists.
	if (file_exists($originalFileRelativePath) == FALSE) {
		//if it doesn't, point to a default image.
		$originalImage = 'images/default.jpg'; //PATH MUST BE RELATIVE FOR file_exists to work!
	//gather basic info about the original file, for use in making the path of the new file.
	$path_parts = pathinfo($originalImage);
	//create the path of the new file.
	$theNewImage = 'wp-content/'.$newImageLoc.'/'.$originalImageID.'-'.$destwidth.'x'.$destheight.'.'.$path_parts['extension'];
	//first, check to see if this file exists already. If it does, then don't do anything. Just return the location of the resized file.
	if (file_exists($theNewImage) == FALSE) //check to see if the file already exists. if it does, then don't perform the function!
		//dest ratio - the image dimension ratio of desired destination image!
		$destratio = $destwidth / $destheight;
		// Create image instance of $src.
		if(strtolower($path_parts['extension']) == "jpg" || strtolower($path_parts['extension']) == "jpeg"){
			$src = imagecreatefromjpeg($originalImage);
		} else if(strtolower($path_parts['extension']) == "png"){
			$src = imagecreatefrompng($originalImage);
		//create instance of $dest file.
		$dest = imagecreatetruecolor($destwidth, $destheight);
		// Ifs - determine the layout of the image, portrait or landscape.
		$imageSize = getimagesize($originalImage); //returns array, 0 width, 1 height.
		$sourceRatio = $imageSize[0]/$imageSize[1];
		//determine size of sample!
		if($destratio < $sourceRatio) {
			//if the destination ratio is smaller than the source, we must use the whole height of the image and a portion of the width.
			$sampleY = round($imageSize[1]);
			$sampleX = round($sampleY * $destratio);
			//this means the sample must start on the y axis at 0, and on the x axis, somewhere in between.
			$startY = 0;
			$startX = ($imageSize[0] - $sampleX) / 2; //take sample from the middle of X axis
		} else {
			//the destination ratio is larger than the source. use whole X width and a portion of the source height.
			$sampleX = round($imageSize[0]); //always use 100% of the sample width in this case.
			$sampleY = round($sampleX / $destratio);
			//this means the sample must start on the x axis at 0, and on the y axis, somewhere in between.
			$startX = 0;
			$startY = ($imageSize[1] - $sampleY) / 2; //take sample from the middle of Y axis
		//actually crop or resize the image.
		imagecopyresampled($dest, $src, 0, 0, $startX, $startY, $destwidth, $destheight, $sampleX, $sampleY);
		// Output and free from memory
		imagejpeg($dest, $theNewImage, 100);
	//set up the new image location and return
	$theNewImage = get_bloginfo('wpurl').'/'.$theNewImage;
	return $theNewImage;