I've made a modification to the thumbnail creation fucntion in order to keep thumbnails in a fixed size. I achieved this by cropping what exceeds the proportion. I added this code to _node_images_create_thumbnail function:

function _node_images_create_thumbnail($path, $suffix='_tn') {
	$size = variable_get('node_images_thumb_resolution', '100x100');
	list($width, $height) = explode('x', $size);
	$dest_path = preg_replace('!(\.[^/.]+?)?$!', "$suffix\\1", $path, 1);

	if ($size = getimagesize($path)) {

		// Mod: Crop in order to maintain proportions.
		$currentWidth = $size[0];
		$currentHeight = $size[1];
		$currentDiff = $currentWidth/$currentHeight;
		$diff = $width/$height;
		if($diff >= 1) {
			if($currentDiff >= $diff) {
				image_scale($path, $dest_path, $currentWidth, $height);
			} else {
				image_scale($path, $dest_path, $width, $currentHeight);
			}
		} elseif($diff < 1) {
			if($currentDiff >= $diff){
				image_scale($path, $dest_path, $currentWidth, $height);
			} else {
				image_scale($path, $dest_path, $width, $currentHeight);
			}
		}
		$newSize = getimagesize($dest_path);
		$newWidth = $newSize[0];
		$newHeight = $newSize[1];
		$xPos = 0;
		$yPos = 0;
		if($newWidth > $width){
			$xPos = intval(($newWidth - $width)/2);
		} elseif($newHeight > $height){
			$yPos = intval(($newHeight - $height)/2);
		}
		image_crop($dest_path, $dest_path, $xPos, $yPos, $width, $height);
		//Mod end.

//		image_scale($path, $dest_path, $width, $height);		
		$info = image_get_info($dest_path);
		$thumb = new stdClass();
		$thumb->filename = basename($dest_path);
		$thumb->filepath = $dest_path;
		$thumb->filesize = $info['file_size'];
		$thumb->filemime = $info['mime_type'];
		return $thumb;
	}
	return NULL;
}

I'd love someone with more expertise in module construction to see if this could be included as an option in Node Images administration.

CommentFileSizeAuthor
#1 node_images.module-crop.patch2.95 KBcharly

Comments

charly’s picture

Status: Needs review » Reviewed & tested by the community
StatusFileSize
new2.95 KB

Thanks!

I made a patch using your code and adding a setting option.

Regards,

soflete’s picture

Well, thak you very much for making this patch! It worked perfectly. I'll make good use of it.

Ragards,

Vallenwood’s picture

Attention maintainers: please read my feature request/patch at http://drupal.org/node/147538 before committing this patch. I have expanded on this concept significantly and I believe this patch is no longer necessary. I encourage anyone reading to visit the new patch, and request that the module maintainers consider the one I just added instead of this one. Excellent idea by soflete, but I think I came up with something more robust. Thanks for a great idea, soflete!

soflete’s picture

Sorry I haven't read this before. I'm glad that this minimal contribution I tried to make inspired such an impressive work. I want to apoligize for my spelling mistakes. I'm from Argentina and here we speak spanish. I hope someday I can write such good code and texts in english as you do. You're awesome!