From bb220540289ec5603552ff63e653b0709a0a6381 Mon Sep 17 00:00:00 2001 From: matt Date: Mon, 3 Mar 2008 04:17:37 +0000 Subject: [PATCH] Creating intermediate sizes, better thumbnails, and other image improvements. Hat tip: tellyworth. git-svn-id: http://svn.automattic.com/wordpress/trunk@7135 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-admin/includes/image.php | 101 ++++++++++++---------------------- wp-admin/includes/media.php | 4 +- wp-admin/includes/schema.php | 5 ++ wp-admin/options-writing.php | 4 +- wp-includes/media.php | 51 +++++++++++++---- wp-includes/post-template.php | 5 +- wp-includes/post.php | 6 +- 7 files changed, 93 insertions(+), 83 deletions(-) diff --git a/wp-admin/includes/image.php b/wp-admin/includes/image.php index d2b0bb7c29..1c2e36a484 100644 --- a/wp-admin/includes/image.php +++ b/wp-admin/includes/image.php @@ -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 @@ function wp_generate_attachment_metadata( $attachment_id, $file ) { $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,15 +95,16 @@ function wp_generate_attachment_metadata( $attachment_id, $file ) { $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) @@ -309,5 +255,28 @@ function wp_read_image_metadata( $file ) { } +// 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); +} ?> diff --git a/wp-admin/includes/media.php b/wp-admin/includes/media.php index 5ca5303ccc..7dbd116ac3 100644 --- a/wp-admin/includes/media.php +++ b/wp-admin/includes/media.php @@ -55,7 +55,7 @@ function get_image_send_to_editor($id, $alt, $title, $align, $url='', $rel = fal $rel = $rel ? ' rel="attachment wp-att-'.attribute_escape($id).'"' : ''; if ( $url ) $html = "$html"; - elseif ( $size == 'thumb' || $size == 'medium' ) + elseif ( $size == 'thumbnail' || $size == 'medium' ) $html = ''.$html.''; $html = apply_filters( 'image_send_to_editor', $html, $id, $alt, $title, $align, $url ); @@ -467,7 +467,7 @@ function image_attachment_fields_to_edit($form_fields, $post) { 'label' => __('Size'), 'input' => 'html', 'html' => " - " . ( $thumb ? " + " . ( $thumb ? " " : '' ) . " diff --git a/wp-admin/includes/schema.php b/wp-admin/includes/schema.php index ece6b5beec..58d649e54a 100644 --- a/wp-admin/includes/schema.php +++ b/wp-admin/includes/schema.php @@ -244,6 +244,11 @@ function populate_options() { 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'); diff --git a/wp-admin/options-writing.php b/wp-admin/options-writing.php index 6adc673053..f8e23a44f3 100644 --- a/wp-admin/options-writing.php +++ b/wp-admin/options-writing.php @@ -68,6 +68,8 @@ endforeach; +/> + @@ -134,7 +136,7 @@ endforeach;

- +

diff --git a/wp-includes/media.php b/wp-includes/media.php index 030d5d50ba..4e4335bdf5 100644 --- a/wp-includes/media.php +++ b/wp-includes/media.php @@ -48,7 +48,7 @@ function image_hwstring($width, $height) { } // 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 @@ function image_downsize($id, $size = 'medium') { 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 @@ function image_resize( $file, $max_w, $max_h, $crop=false, $suffix=null, $dest_p return $destfilename; } -?> \ No newline at end of file +// 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]; +} + + +?> diff --git a/wp-includes/post-template.php b/wp-includes/post-template.php index 636bbb2818..b99af8e9db 100644 --- a/wp-includes/post-template.php +++ b/wp-includes/post-template.php @@ -389,11 +389,10 @@ function get_attachment_icon_src( $id = 0, $fullsize = false ) { $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 diff --git a/wp-includes/post.php b/wp-includes/post.php index 4db6d54e47..5b556620a9 100644 --- a/wp-includes/post.php +++ b/wp-includes/post.php @@ -2128,7 +2128,7 @@ function wp_insert_attachment($object, $file = false, $parent = 0) { if ( $file ) update_attached_file( $post_ID, $file ); - + clean_post_cache($post_ID); if ( $update) { @@ -2316,6 +2316,10 @@ function wp_get_attachment_thumb_url( $post_id = 0 ) { 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;