Index: wordpress/wp-includes/post-template.php
===================================================================
--- wordpress/wp-includes/post-template.php	(revision 7133)
+++ wordpress/wp-includes/post-template.php	(working copy)
@@ -389,11 +389,10 @@
 
 	$file = get_attached_file( $post->ID );
 
-	if ( !$fullsize && $thumbfile = wp_get_attachment_thumb_file( $post->ID ) ) {
+	if ( !$fullsize && $src = wp_get_attachment_thumb_url( $post->ID ) ) {
 		// We have a thumbnail desired, specified and existing
 
-		$src = wp_get_attachment_thumb_url( $post->ID );
-		$src_file = $thumbfile;
+		$src_file = basename($src);
 		$class = 'attachmentthumb';
 	} elseif ( wp_attachment_is_image( $post->ID ) ) {
 		// We have an image without a thumbnail
Index: wordpress/wp-includes/post.php
===================================================================
--- wordpress/wp-includes/post.php	(revision 7133)
+++ wordpress/wp-includes/post.php	(working copy)
@@ -2128,7 +2128,7 @@
 
 	if ( $file )
 		update_attached_file( $post_ID, $file );
-
+		
 	clean_post_cache($post_ID);
 
 	if ( $update) {
@@ -2316,6 +2316,10 @@
 		return false;
 	if ( !$url = wp_get_attachment_url( $post->ID ) )
 		return false;
+		
+	$sized = image_downsize( $post_id, 'thumbnail' );
+	if ( $sized )
+		return $sized[0];
 
 	if ( !$thumb = wp_get_attachment_thumb_file( $post->ID ) )
 		return false;
Index: wordpress/wp-includes/media.php
===================================================================
--- wordpress/wp-includes/media.php	(revision 7133)
+++ wordpress/wp-includes/media.php	(working copy)
@@ -48,7 +48,7 @@
 }
 
 // Scale an image to fit a particular size (such as 'thumb' or 'medium'), and return an image URL, height and width.
-// The URL might be the original image, or it might be a resized version.
+// The URL might be the original image, or it might be a resized version.  This function won't create a new resized copy, it will just return an already resized one if it exists.
 // returns an array($url, $width, $height)
 function image_downsize($id, $size = 'medium') {
 
@@ -60,15 +60,19 @@
 	if ( $out = apply_filters('image_downsize', false, $id, $size) )
 		return $out;
 
-	if ( $size == 'thumb' ) {
-		// thumbnail: use the thumb as the displayed image, and constrain based on its dimensions
-		$thumb_path = wp_get_attachment_thumb_file($id);
-		// the actual thumbnail size isn't stored so we'll have to calculate it
-		if ( $thumb_path && ($info = getimagesize($thumb_path)) ) {
-			list( $width, $height ) = image_constrain_size_for_editor( $info[0], $info[1], $size );
-			$img_url = wp_get_attachment_thumb_url($id);
+	// try for a new style intermediate size
+	if ( $intermediate = image_get_intermediate_size($id, $size) ) {
+		$img_url = str_replace(basename($img_url), $intermediate['file'], $img_url);
+		$width = $intermediate['width'];
+		$height = $intermediate['height'];
+	}
+	elseif ( $size == 'thumbnail' ) {
+		// fall back to the old thumbnail
+		if ( $thumb_file = wp_get_attachment_thumb_file() && $info = getimagesize($thumb_file) ) {
+			$img_url = str_replace(basename($img_url), basename($thumb_file), $img_url);
+			$width = $info[0];
+			$height = $info[1];
 		}
-		// this could be improved to provide a default thumbnail if one doesn't exist
 	}
 	elseif ( isset($meta['width'], $meta['height']) ) {
 		// any other type: use the real image and constrain it
@@ -226,4 +230,31 @@
 	return $destfilename;
 }
 
+// resize an image to make a thumbnail or intermediate size, and return metadata describing the new copy
+// returns false if no image was created
+function image_make_intermediate_size($file, $width, $height, $crop=false) {
+	if ( $width || $height ) {
+		$resized_file = image_resize($file, $width, $height, $crop);
+		if ( $resized_file && $info = getimagesize($resized_file) ) {
+			return array(
+				'file' => basename( $resized_file ),
+				'width' => $info[0],
+				'height' => $info[1],
+			);
+		}
+	}
+	return false;
+}
+
+function image_get_intermediate_size($post_id, $size='thumbnail') {
+	if ( !$imagedata = wp_get_attachment_metadata( $post_id ) )
+		return false;
+		
+	if ( empty($imagedata['sizes'][$size]) )
+		return false;
+		
+	return $imagedata['sizes'][$size];
+}
+
+
 ?>
\ No newline at end of file
Index: wordpress/wp-admin/includes/schema.php
===================================================================
--- wordpress/wp-admin/includes/schema.php	(revision 7133)
+++ wordpress/wp-admin/includes/schema.php	(working copy)
@@ -244,6 +244,11 @@
 	add_option('show_avatars', '1');
 	add_option('avatar_rating', 'G');
 	add_option('upload_url_path', '');
+	add_option('thumbnail_size_w', 150);
+	add_option('thumbnail_size_h', 150);
+	add_option('thumbnail_crop', 1);
+	add_option('medium_size_w', '');
+	add_option('medium_size_h', '');
 
 	// Delete unused options
 	$unusedoptions = array ('blodotgsping_url', 'bodyterminator', 'emailtestonly', 'phoneemail_separator', 'smilies_directory', 'subjectprefix', 'use_bbcode', 'use_blodotgsping', 'use_phoneemail', 'use_quicktags', 'use_weblogsping', 'weblogs_cache_file', 'use_preview', 'use_htmltrans', 'smilies_directory', 'fileupload_allowedusers', 'use_phoneemail', 'default_post_status', 'default_post_category', 'archive_mode', 'time_difference', 'links_minadminlevel', 'links_use_adminlevels', 'links_rating_type', 'links_rating_char', 'links_rating_ignore_zero', 'links_rating_single_image', 'links_rating_image0', 'links_rating_image1', 'links_rating_image2', 'links_rating_image3', 'links_rating_image4', 'links_rating_image5', 'links_rating_image6', 'links_rating_image7', 'links_rating_image8', 'links_rating_image9', 'weblogs_cacheminutes', 'comment_allowed_tags', 'search_engine_friendly_urls', 'default_geourl_lat', 'default_geourl_lon', 'use_default_geourl', 'weblogs_xml_url', 'new_users_can_blog', '_wpnonce', '_wp_http_referer', 'Update', 'action', 'rich_editing');
Index: wordpress/wp-admin/includes/media.php
===================================================================
--- wordpress/wp-admin/includes/media.php	(revision 7133)
+++ wordpress/wp-admin/includes/media.php	(working copy)
@@ -55,7 +55,7 @@
 	$rel = $rel ? ' rel="attachment wp-att-'.attribute_escape($id).'"' : '';
 	if ( $url )
 		$html = "<a href='".attribute_escape($url)."'$rel>$html</a>";
-	elseif ( $size == 'thumb' || $size == 'medium' )
+	elseif ( $size == 'thumbnail' || $size == 'medium' )
 		$html = '<a href="'.get_attachment_link($id).'"'.$rel.'>'.$html.'</a>';
 
 	$html = apply_filters( 'image_send_to_editor', $html, $id, $alt, $title, $align, $url );
@@ -467,7 +467,7 @@
 			'label' => __('Size'),
 			'input' => 'html',
 			'html'  => "
-				" . ( $thumb ? "<input type='radio' name='attachments[$post->ID][image-size]' id='image-size-thumb-$post->ID' value='thumb' />
+				" . ( $thumb ? "<input type='radio' name='attachments[$post->ID][image-size]' id='image-size-thumb-$post->ID' value='thumbnail' />
 				<label for='image-size-thumb-$post->ID'>" . __('Thumbnail') . "</label>
 				" : '' ) . "<input type='radio' name='attachments[$post->ID][image-size]' id='image-size-medium-$post->ID' value='medium' checked='checked' />
 				<label for='image-size-medium-$post->ID'>" . __('Medium') . "</label>
Index: wordpress/wp-admin/includes/image.php
===================================================================
--- wordpress/wp-admin/includes/image.php	(revision 7133)
+++ wordpress/wp-admin/includes/image.php	(working copy)
@@ -17,63 +17,8 @@
  * If PHP does not have the functionality to save in a file of the same format, the thumbnail will be created as a jpeg.
  */
 function wp_create_thumbnail( $file, $max_side, $deprecated = '' ) {
-	if ( ctype_digit( $file ) ) // Handle int as attachment ID
-		$file = get_attached_file( $file );
-
-	$image = wp_load_image( $file );
-
-	if ( !is_resource( $image ) )
-		return $image;
-
-	list($sourceImageWidth, $sourceImageHeight, $sourceImageType) = getimagesize( $file );
-
-	if ( function_exists( 'imageantialias' ))
-		imageantialias( $image, true );
-
-	list($image_new_width, $image_new_height) = wp_shrink_dimensions( $sourceImageWidth, $sourceImageHeight, $max_side, $max_side);
-
-	$thumbnail = imagecreatetruecolor( $image_new_width, $image_new_height);
-
-	// preserve PNG transparency
-	if ( IMAGETYPE_PNG == $sourceImageType && function_exists( 'imagealphablending' ) && function_exists( 'imagesavealpha' ) ) {
-		imagealphablending( $thumbnail, false);
-		imagesavealpha( $thumbnail, true);
-	}
-
-	imagecopyresampled( $thumbnail, $image, 0, 0, 0, 0, $image_new_width, $image_new_height, $sourceImageWidth, $sourceImageHeight );
-
-	imagedestroy( $image ); // Free up memory
-
-	// If no filters change the filename, we'll do a default transformation.
-	if ( basename( $file ) == $thumb = apply_filters( 'thumbnail_filename', basename( $file ) ) )
-		$thumb = preg_replace( '!(\.[^.]+)?$!', '.thumbnail$1', basename( $file ), 1 );
-
-	$thumbpath = str_replace( basename( $file ), $thumb, $file );
-
-	switch( $sourceImageType ){
-		default: // We'll create a Jpeg if we cant use its native file format
-			$thumb = preg_replace( '/\\.[^\\.]+$/', '.jpg', $thumb ); //Change file extension to Jpg
-		case IMAGETYPE_JPEG:
-			if (!imagejpeg( $thumbnail, $thumbpath ) )
-				return __( 'Thumbnail path invalid' );
-			break;
-		case IMAGETYPE_GIF:
-			if (!imagegif( $thumbnail, $thumbpath ) )
-				return __( 'Thumbnail path invalid' );
-			break;
-		case IMAGETYPE_PNG:
-			if (!imagepng( $thumbnail, $thumbpath ) )
-				return __( 'Thumbnail path invalid' );
-			break;
-	}
-
-	imagedestroy( $thumbnail ); // Free up memory
-
-	// Set correct file permissions
-	$stat = stat( dirname( $thumbpath ));
-	$perms = $stat['mode'] & 0000666; //same permissions as parent folder, strip off the executable bits
-	@ chmod( $thumbpath, $perms );
-
+	
+	$thumbpath = image_resize( $file, $max_side, $max_side );
 	return apply_filters( 'wp_create_thumbnail', $thumbpath );
 }
 
@@ -142,7 +87,7 @@
 	$attachment = get_post( $attachment_id );
 
 	$metadata = array();
-	if ( preg_match('!^image/!', get_post_mime_type( $attachment )) ) {
+	if ( preg_match('!^image/!', get_post_mime_type( $attachment )) && file_is_displayable_image($file) ) {
 		$imagesize = getimagesize( $file );
 		$metadata['width'] = $imagesize[0];
 		$metadata['height'] = $imagesize[1];
@@ -150,21 +95,23 @@
 		$metadata['hwstring_small'] = "height='$uheight' width='$uwidth'";
 		$metadata['file'] = $file;
 
-		$max = apply_filters( 'wp_thumbnail_creation_size_limit', absint( WP_MEMORY_LIMIT ) * 1024 * 1024, $attachment_id, $file );
-
-		if ( $max < 0 || $metadata['width'] * $metadata['height'] < $max ) {
-			$max_side = apply_filters( 'wp_thumbnail_max_side_length', 140, $attachment_id, $file );
-			$thumb = wp_create_thumbnail( $file, $max_side );
-			if ( @file_exists($thumb) )
-				$metadata['thumb'] = basename($thumb);
+		// make thumbnails and other intermediate sizes
+		$sizes = array('thumbnail', 'medium');
+		$sizes = apply_filters('intermediate_image_sizes', $sizes);
+		
+		foreach ($sizes as $size) {
+			$resized = image_make_intermediate_size( $file, get_option("{$size}_size_w"), get_option("{$size}_size_h"), get_option("{$size}_crop") );
+			if ( $resized )
+				$metadata['sizes'][$size] = $resized;
 		}
-
+			
 		// fetch additional metadata from exif/iptc
 		$image_meta = wp_read_image_metadata( $file );
 		if ($image_meta)
 			$metadata['image_meta'] = $image_meta;
 
 	}
+	var_dump($metadata);
 	return apply_filters( 'wp_generate_attachment_metadata', $metadata );
 }
 
@@ -309,5 +256,28 @@
 
 }
 
+// is the file a real image file?
+function file_is_valid_image($path) {
+	$size = @getimagesize($path);
+	return !empty($size);
+}
 
+// is the file an image suitable for displaying within a web page?
+function file_is_displayable_image($path) {
+	$info = @getimagesize($path);
+	if ( empty($info) )
+		$result = false;
+	elseif ( !in_array($info[2], array(IMAGETYPE_GIF, IMAGETYPE_JPEG, IMAGETYPE_PNG)) )
+		// only gif, jpeg and png images can reliably be displayed
+		$result = false;
+	elseif ( $info['channels'] > 0 && $info['channels'] != 3 ) {
+		// some web browsers can't display cmyk or grayscale jpegs
+		$result = false;
+	}
+	else
+		$result = true;
+		
+	return apply_filters('file_is_displayable_image', $result, $path);
+}
+
 ?>
Index: wordpress/wp-admin/includes/upload.php
===================================================================
--- wordpress/wp-admin/includes/upload.php	(revision 7133)
+++ wordpress/wp-admin/includes/upload.php	(working copy)
@@ -59,6 +59,7 @@
 		$thumb_base = wp_mime_type_icon();
 	if ( $thumb_base ) {
 		$thumb_rel = wp_make_link_relative( $thumb_base );
+		var_dump("thumb_base = $thumb_base", "thumb_rel = $thumb_rel");
 		$thumb_base = str_replace( $thumb_rel, '', $thumb_base );
 		$r .= "\t\t\t\t<input type='hidden' name='attachment-thumb-url-$id' id='attachment-thumb-url-$id' value='$thumb_rel' />\n";
 		$r .= "\t\t\t\t<input type='hidden' name='attachment-thumb-url-base-$id' id='attachment-thumb-url-base-$id' value='$thumb_base' />\n";
Index: wordpress/wp-admin/options-writing.php
===================================================================
--- wordpress/wp-admin/options-writing.php	(revision 7133)
+++ wordpress/wp-admin/options-writing.php	(working copy)
@@ -68,6 +68,8 @@
 <input name="thumbnail_size_w" type="text" id="thumbnail_size_w" value="<?php form_option('thumbnail_size_w'); ?>" size="6" />
 <label for="thumbnail_size_h"><?php _e('Height'); ?></label>
 <input name="thumbnail_size_h" type="text" id="thumbnail_size_h" value="<?php form_option('thumbnail_size_h'); ?>" size="6" />
+<input name="thumbnail_crop" type="checkbox" id="thumbnail_crop" value="1" <?php checked('1', get_option('thumbnail_crop')); ?>/>
+<label for="thumbnail_crop"><?php _e('Crop to size'); ?></label>
 </td>
 </tr>
 <tr valign="top">
@@ -134,7 +136,7 @@
 
 <p class="submit">
 <input type="hidden" name="action" value="update" />
-<input type="hidden" name="page_options" value="default_post_edit_rows,use_smilies,ping_sites,mailserver_url,mailserver_port,mailserver_login,mailserver_pass,default_category,default_email_category,use_balanceTags,default_link_category" />
+<input type="hidden" name="page_options" value="default_post_edit_rows,use_smilies,ping_sites,mailserver_url,mailserver_port,mailserver_login,mailserver_pass,default_category,default_email_category,use_balanceTags,default_link_category,thumbnail_size_w,thumbnail_size_h,thumbnail_crop,medium_size_w,medium_size_h" />
 <input type="submit" name="Submit" value="<?php _e('Save Changes') ?>" />
 </p>
 </form>
