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
This commit is contained in:
matt 2008-03-03 04:17:37 +00:00
parent bf18e2fd3f
commit bb22054028
7 changed files with 93 additions and 83 deletions

View File

@ -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);
}
?>

View File

@ -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 = "<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 @@ function image_attachment_fields_to_edit($form_fields, $post) {
'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>

View File

@ -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');

View File

@ -68,6 +68,8 @@ endforeach;
<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 @@ endforeach;
<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>

View File

@ -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;
}
?>
// 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];
}
?>

View File

@ -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

View File

@ -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;