Index: src/wp-includes/media.php
===================================================================
--- src/wp-includes/media.php	(revision 35853)
+++ src/wp-includes/media.php	(working copy)
@@ -647,32 +647,38 @@
 			}
 			// If it's not an exact match but it's at least the dimensions requested.
 			if ( $data['width'] >= $size[0] && $data['height'] >= $size[1] ) {
-				$candidates[ $data['width'] * $data['height'] ] = $_size;
+				$pixels = $data['width'] * $data['height'];
+				if ( ! isset( $candidates[ $pixels ] ) ) {
+					$candidates[ $pixels ] = array();
+				}
+				$candidates[ $pixels ][] = $_size;
 			}
 		}
 
 		if ( ! empty( $candidates ) ) {
 			// find for the smallest image not smaller than the desired size
 			ksort( $candidates );
-			foreach ( $candidates as $_size ) {
-				$data = $imagedata['sizes'][$_size];
+			foreach ( $candidates as $_candidates ) {
+				foreach ( $_candidates as $_size ) {
+					$data = $imagedata['sizes'][$_size];
 
-				// Skip images with unexpectedly divergent aspect ratios (crops)
-				// First, we calculate what size the original image would be if constrained to a box the size of the current image in the loop
-				$maybe_cropped = image_resize_dimensions($imagedata['width'], $imagedata['height'], $data['width'], $data['height'], false );
-				// If the size doesn't match within one pixel, then it is of a different aspect ratio, so we skip it, unless it's the thumbnail size
-				if ( 'thumbnail' != $_size &&
-				  ( ! $maybe_cropped
-				    || ( $maybe_cropped[4] != $data['width'] && $maybe_cropped[4] + 1 != $data['width'] )
-				    || ( $maybe_cropped[5] != $data['height'] && $maybe_cropped[5] + 1 != $data['height'] )
-				  ) ) {
-				  continue;
+					// Skip images with unexpectedly divergent aspect ratios (crops)
+					// First, we calculate what size the original image would be if constrained to a box the size of the current image in the loop
+					$maybe_cropped = image_resize_dimensions($imagedata['width'], $imagedata['height'], $data['width'], $data['height'], false );
+					// If the size doesn't match within one pixel, then it is of a different aspect ratio, so we skip it, unless it's the thumbnail size
+					if ( 'thumbnail' != $_size &&
+					  ( ! $maybe_cropped
+					    || ( $maybe_cropped[4] != $data['width'] && $maybe_cropped[4] + 1 != $data['width'] )
+					    || ( $maybe_cropped[5] != $data['height'] && $maybe_cropped[5] + 1 != $data['height'] )
+					  ) ) {
+					  continue;
+					}
+					// If we're still here, then we're going to use this size.
+					list( $data['width'], $data['height'] ) = image_constrain_size_for_editor( $data['width'], $data['height'], $size );
+
+					/** This filter is documented in wp-includes/media.php */
+					return apply_filters( 'image_get_intermediate_size', $data, $post_id, $size );
 				}
-				// If we're still here, then we're going to use this size.
-				list( $data['width'], $data['height'] ) = image_constrain_size_for_editor( $data['width'], $data['height'], $size );
-
-				/** This filter is documented in wp-includes/media.php */
-				return apply_filters( 'image_get_intermediate_size', $data, $post_id, $size );
 			}
 		}
 	}
