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
This commit is contained in:
markjaquith 2009-12-08 21:08:19 +00:00
parent cff2ea75c7
commit dc8492b9f5
4 changed files with 58 additions and 8 deletions

View File

@ -98,10 +98,33 @@ function wp_generate_attachment_metadata( $attachment_id, $file ) {
$metadata['file'] = _wp_relative_upload_path($file); $metadata['file'] = _wp_relative_upload_path($file);
// make thumbnails and other intermediate sizes // 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) { $temp_sizes = apply_filters( 'intermediate_image_sizes', $temp_sizes );
$resized = image_make_intermediate_size( $file, get_option("{$size}_size_w"), get_option("{$size}_size_h"), get_option("{$size}_crop") );
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 ) if ( $resized )
$metadata['sizes'][$size] = $resized; $metadata['sizes'][$size] = $resized;
} }

View File

@ -1070,14 +1070,21 @@ function get_sample_permalink_html( $id, $new_title = null, $new_slug = null ) {
* @return string html * @return string html
*/ */
function _wp_post_thumbnail_html( $thumbnail_id = NULL ) { function _wp_post_thumbnail_html( $thumbnail_id = NULL ) {
global $content_width, $_wp_additional_image_sizes;
$content = '<p class="hide-if-no-js"><a href="#" id="set-post-thumbnail" onclick="jQuery(\'#add_image\').click();return false;">' . esc_html__( 'Set thumbnail' ) . '</a></p>'; $content = '<p class="hide-if-no-js"><a href="#" id="set-post-thumbnail" onclick="jQuery(\'#add_image\').click();return false;">' . esc_html__( 'Set thumbnail' ) . '</a></p>';
if ( $thumbnail_id && get_post( $thumbnail_id ) ) { 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 ) ) { if ( !empty( $thumbnail_html ) ) {
$content = '<a href="#" id="set-post-thumbnail" onclick="jQuery(\'#add_image\').click();return false;">' . $thumbnail_html . '</a>'; $content = '<a href="#" id="set-post-thumbnail" onclick="jQuery(\'#add_image\').click();return false;">' . $thumbnail_html . '</a>';
$content .= '<p class="hide-if-no-js"><a href="#" id="remove-post-thumbnail" onclick="WPRemoveThumbnail();return false;">' . esc_html__( 'Remove thumbnail' ) . '</a></p>'; $content .= '<p class="hide-if-no-js"><a href="#" id="remove-post-thumbnail" onclick="WPRemoveThumbnail();return false;">' . esc_html__( 'Remove thumbnail' ) . '</a></p>';
} }
$content_width = $old_content_width;
} }
return apply_filters( 'admin_post_thumbnail_html', $content ); return apply_filters( 'admin_post_thumbnail_html', $content );

View File

@ -32,7 +32,7 @@
* @return array Width and height of what the result image should resize to. * @return array Width and height of what the result image should resize to.
*/ */
function image_constrain_size_for_editor($width, $height, $size = 'medium') { function image_constrain_size_for_editor($width, $height, $size = 'medium') {
global $content_width; global $content_width, $_wp_additional_image_sizes;
if ( is_array($size) ) { if ( is_array($size) ) {
$max_width = $size[0]; $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')); $max_height = intval(get_option('large_size_h'));
if ( intval($content_width) > 0 ) if ( intval($content_width) > 0 )
$max_width = min( intval($content_width), $max_width ); $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 // $size == 'full' has no constraint
else { 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 <img src /> tag for an image attachment, scaling it down if requested. * An <img src /> tag for an image attachment, scaling it down if requested.
* *

View File

@ -42,10 +42,10 @@ function get_post_image_id( $post_id = NULL ) {
* *
* @since 2.9.0 * @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. * @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 ); 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 $size Optional. Image size. Defaults to 'thumbnail'.
* @param string|array $attr Optional. Query string or array of attributes. * @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; global $id;
$post_id = ( NULL === $post_id ) ? $id : $post_id; $post_id = ( NULL === $post_id ) ? $id : $post_id;
$post_image_id = get_post_image_id( $post_id ); $post_image_id = get_post_image_id( $post_id );