Index: media.php
===================================================================
--- media.php	(revision 18079)
+++ media.php	(working copy)
@@ -528,9 +528,24 @@
 
 	// get the best one for a specified set of dimensions
 	if ( is_array($size) && !empty($imagedata['sizes']) ) {
+		// sort $imagemeta['sizes'] descending by width, height
+		function compare_sizes( $a, $b ) {
+			$sorted = strnatcmp( $a['width'], $b['width'] );
+			if( ! $sorted ) return strnatcmp( $a['height'], $b['height'] );
+			array_reverse( $sorted );
+			return $sorted;
+		}
+		uasort( $imagedata['sizes'], 'compare_sizes' );
+		// loop through $imagemeta['sizes'] to find the right thumbnail size
 		foreach ( $imagedata['sizes'] as $_size => $data ) {
+			// already cropped exactly to width and height; so use this size
+			if ( $data['width'] == $size[0] && $data['height'] == $size[1] ) {
+				$file = $data['file'];
+				list($width, $height) = image_constrain_size_for_editor( $data['width'], $data['height'], $size );
+				return compact( 'file', 'width', 'height' );
+			}
 			// already cropped to width or height; so use this size
-			if ( ( $data['width'] == $size[0] && $data['height'] <= $size[1] ) || ( $data['height'] == $size[1] && $data['width'] <= $size[0] ) ) {
+			else if ( ( $data['width'] == $size[0] && $data['height'] <= $size[1] ) || ( $data['height'] == $size[1] && $data['width'] <= $size[0] ) ) {
 				$file = $data['file'];
 				list($width, $height) = image_constrain_size_for_editor( $data['width'], $data['height'], $size );
 				return compact( 'file', 'width', 'height' );
