From dc8492b9f5acc081970a4faae928fff9a81427b5 Mon Sep 17 00:00:00 2001 From: markjaquith Date: Tue, 8 Dec 2009 21:08:19 +0000 Subject: [PATCH] Introducing set_post_image_size(w, h, crop) so themes can register their special size/crop for canonical post images. WP will create this size/crop upon upload, so your canonical post images fit your space exactly! git-svn-id: http://svn.automattic.com/wordpress/trunk@12342 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-admin/includes/image.php | 29 ++++++++++++++++++++++++++--- wp-admin/includes/post.php | 9 ++++++++- wp-includes/media.php | 22 +++++++++++++++++++++- wp-includes/post-image-template.php | 6 +++--- 4 files changed, 58 insertions(+), 8 deletions(-) diff --git a/wp-admin/includes/image.php b/wp-admin/includes/image.php index 0ae30ce46a..ba0c99c5d2 100644 --- a/wp-admin/includes/image.php +++ b/wp-admin/includes/image.php @@ -98,10 +98,33 @@ function wp_generate_attachment_metadata( $attachment_id, $file ) { $metadata['file'] = _wp_relative_upload_path($file); // make thumbnails and other intermediate sizes - $sizes = apply_filters( 'intermediate_image_sizes', array('thumbnail', 'medium', 'large') ); + global $_wp_additional_image_sizes; + $temp_sizes = array('thumbnail', 'medium', 'large'); // Standard sizes + if ( isset( $_wp_additional_image_sizes ) && count( $_wp_additional_image_sizes ) ) + $temp_sizes = array_merge( $temp_sizes, array_keys( $_wp_additional_image_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") ); + $temp_sizes = apply_filters( 'intermediate_image_sizes', $temp_sizes ); + + foreach ( $temp_sizes as $s ) { + $sizes[$s] = array( 'width' => '', 'height' => '', 'crop' => FALSE ); + if ( isset( $_wp_additional_image_sizes[$s]['width'] ) ) + $sizes[$s]['width'] = intval( $_wp_additional_image_sizes[$s]['width'] ); // For theme-added sizes + else + $sizes[$s]['width'] = get_option( "{$size}_size_w" ); // For default sizes set in options + if ( isset( $_wp_additional_image_sizes[$s]['height'] ) ) + $sizes[$s]['height'] = intval( $_wp_additional_image_sizes[$s]['height'] ); // For theme-added sizes + else + $sizes[$s]['height'] = get_option( "{$size}_size_h" ); // For default sizes set in options + if ( isset( $_wp_additional_image_sizes[$s]['crop'] ) ) + $sizes[$s]['crop'] = intval( $_wp_additional_image_sizes[$s]['crop'] ); // For theme-added sizes + else + $sizes[$s]['crop'] = get_option( "{$size}_crop" ); // For default sizes set in options + } + + $sizes = apply_filters( 'intermediate_image_sizes_advanced', $sizes ); + + foreach ($sizes as $size => $size_data ) { + $resized = image_make_intermediate_size( $file, $size_data['width'], $size_data['height'], $size_data['crop'] ); if ( $resized ) $metadata['sizes'][$size] = $resized; } diff --git a/wp-admin/includes/post.php b/wp-admin/includes/post.php index 692dc58df7..2b3111d5d9 100644 --- a/wp-admin/includes/post.php +++ b/wp-admin/includes/post.php @@ -1070,14 +1070,21 @@ function get_sample_permalink_html( $id, $new_title = null, $new_slug = null ) { * @return string html */ function _wp_post_thumbnail_html( $thumbnail_id = NULL ) { + global $content_width, $_wp_additional_image_sizes; $content = '

' . esc_html__( 'Set thumbnail' ) . '

'; if ( $thumbnail_id && get_post( $thumbnail_id ) ) { - $thumbnail_html = wp_get_attachment_image($thumbnail_id, array( 266, 266 ) ); + $old_content_width = $content_width; + $content_width = 266; + if ( !isset( $_wp_additional_image_sizes['post-image'] ) ) + $thumbnail_html = wp_get_attachment_image( $thumbnail_id, array( $content_width, $content_width ) ); + else + $thumbnail_html = wp_get_attachment_image( $thumbnail_id, 'post-image' ); if ( !empty( $thumbnail_html ) ) { $content = '' . $thumbnail_html . ''; $content .= '

' . esc_html__( 'Remove thumbnail' ) . '

'; } + $content_width = $old_content_width; } return apply_filters( 'admin_post_thumbnail_html', $content ); diff --git a/wp-includes/media.php b/wp-includes/media.php index f0b5f2404b..cb96bf7ea7 100644 --- a/wp-includes/media.php +++ b/wp-includes/media.php @@ -32,7 +32,7 @@ * @return array Width and height of what the result image should resize to. */ function image_constrain_size_for_editor($width, $height, $size = 'medium') { - global $content_width; + global $content_width, $_wp_additional_image_sizes; if ( is_array($size) ) { $max_width = $size[0]; @@ -61,6 +61,11 @@ function image_constrain_size_for_editor($width, $height, $size = 'medium') { $max_height = intval(get_option('large_size_h')); if ( intval($content_width) > 0 ) $max_width = min( intval($content_width), $max_width ); + } elseif ( isset( $_wp_additional_image_sizes ) && count( $_wp_additional_image_sizes ) && in_array( $size, array_keys( $_wp_additional_image_sizes ) ) ) { + $max_width = intval( $_wp_additional_image_sizes[$size]['width'] ); + $max_height = intval( $_wp_additional_image_sizes[$size]['height'] ); + if ( intval($content_width) > 0 ) + $max_width = min( intval($content_width), $max_width ); } // $size == 'full' has no constraint else { @@ -169,6 +174,21 @@ function image_downsize($id, $size = 'medium') { } +/** + * Registers a new image size + */ +function add_image_size( $name, $width = 0, $height = 0, $crop = FALSE ) { + global $_wp_additional_image_sizes; + $_wp_additional_image_sizes[$name] = array( 'width' => absint( $width ), 'height' => absint( $height ), 'crop' => !!$crop ); +} + +/** + * Registers an image size for the post image + */ +function set_post_image_size( $width = 0, $height = 0, $crop = FALSE ) { + add_image_size( 'post-image', $width, $height, $crop ); +} + /** * An tag for an image attachment, scaling it down if requested. * diff --git a/wp-includes/post-image-template.php b/wp-includes/post-image-template.php index c2c813140d..1a6b30e7ae 100644 --- a/wp-includes/post-image-template.php +++ b/wp-includes/post-image-template.php @@ -42,10 +42,10 @@ function get_post_image_id( $post_id = NULL ) { * * @since 2.9.0 * - * @param int $size Optional. Image size. Defaults to 'thumbnail'. + * @param int $size Optional. Image size. Defaults to 'post-image', which theme sets using set_post_image_size( $width, $height, $crop_flag );. * @param string|array $attr Optional. Query string or array of attributes. */ -function the_post_image( $size = 'thumbnail', $attr = '' ) { +function the_post_image( $size = 'post-image', $attr = '' ) { echo get_the_post_image( NULL, $size, $attr ); } @@ -58,7 +58,7 @@ function the_post_image( $size = 'thumbnail', $attr = '' ) { * @param string $size Optional. Image size. Defaults to 'thumbnail'. * @param string|array $attr Optional. Query string or array of attributes. */ -function get_the_post_image( $post_id = NULL, $size = 'thumbnail', $attr = '' ) { +function get_the_post_image( $post_id = NULL, $size = 'post-image', $attr = '' ) { global $id; $post_id = ( NULL === $post_id ) ? $id : $post_id; $post_image_id = get_post_image_id( $post_id );