WordPress.org

Make WordPress Core

Changeset 12342


Ignore:
Timestamp:
12/08/09 21:08:19 (5 years ago)
Author:
markjaquith
Message:

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!

Location:
trunk
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-admin/includes/image.php

    r12163 r12342  
    9999 
    100100        // make thumbnails and other intermediate sizes 
    101         $sizes = apply_filters( 'intermediate_image_sizes', array('thumbnail', 'medium', 'large') ); 
    102  
    103         foreach ($sizes as $size) { 
    104             $resized = image_make_intermediate_size( $file, get_option("{$size}_size_w"), get_option("{$size}_size_h"), get_option("{$size}_crop") ); 
     101        global $_wp_additional_image_sizes; 
     102        $temp_sizes = array('thumbnail', 'medium', 'large'); // Standard sizes 
     103        if ( isset( $_wp_additional_image_sizes ) && count( $_wp_additional_image_sizes ) ) 
     104            $temp_sizes = array_merge( $temp_sizes, array_keys( $_wp_additional_image_sizes ) ); 
     105 
     106        $temp_sizes = apply_filters( 'intermediate_image_sizes', $temp_sizes ); 
     107 
     108        foreach ( $temp_sizes as $s ) { 
     109            $sizes[$s] = array( 'width' => '', 'height' => '', 'crop' => FALSE ); 
     110            if ( isset( $_wp_additional_image_sizes[$s]['width'] ) ) 
     111                $sizes[$s]['width'] = intval( $_wp_additional_image_sizes[$s]['width'] ); // For theme-added sizes 
     112            else 
     113                $sizes[$s]['width'] = get_option( "{$size}_size_w" ); // For default sizes set in options 
     114            if ( isset( $_wp_additional_image_sizes[$s]['height'] ) ) 
     115                $sizes[$s]['height'] = intval( $_wp_additional_image_sizes[$s]['height'] ); // For theme-added sizes 
     116            else 
     117                $sizes[$s]['height'] = get_option( "{$size}_size_h" ); // For default sizes set in options 
     118            if ( isset( $_wp_additional_image_sizes[$s]['crop'] ) ) 
     119                $sizes[$s]['crop'] = intval( $_wp_additional_image_sizes[$s]['crop'] ); // For theme-added sizes 
     120            else 
     121                $sizes[$s]['crop'] = get_option( "{$size}_crop" ); // For default sizes set in options 
     122        } 
     123 
     124        $sizes = apply_filters( 'intermediate_image_sizes_advanced', $sizes ); 
     125 
     126        foreach ($sizes as $size => $size_data ) { 
     127            $resized = image_make_intermediate_size( $file, $size_data['width'], $size_data['height'], $size_data['crop'] ); 
    105128            if ( $resized ) 
    106129                $metadata['sizes'][$size] = $resized; 
  • trunk/wp-admin/includes/post.php

    r12336 r12342  
    10711071 */ 
    10721072function _wp_post_thumbnail_html( $thumbnail_id = NULL ) { 
     1073    global $content_width, $_wp_additional_image_sizes; 
    10731074    $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>'; 
    10741075 
    10751076    if ( $thumbnail_id && get_post( $thumbnail_id ) ) { 
    1076         $thumbnail_html = wp_get_attachment_image($thumbnail_id, array( 266, 266 ) ); 
     1077        $old_content_width = $content_width; 
     1078        $content_width = 266; 
     1079        if ( !isset( $_wp_additional_image_sizes['post-image'] ) ) 
     1080            $thumbnail_html = wp_get_attachment_image( $thumbnail_id, array( $content_width, $content_width ) ); 
     1081        else 
     1082            $thumbnail_html = wp_get_attachment_image( $thumbnail_id, 'post-image' ); 
    10771083        if ( !empty( $thumbnail_html ) ) { 
    10781084            $content = '<a href="#" id="set-post-thumbnail" onclick="jQuery(\'#add_image\').click();return false;">' . $thumbnail_html . '</a>'; 
    10791085            $content .= '<p class="hide-if-no-js"><a href="#" id="remove-post-thumbnail" onclick="WPRemoveThumbnail();return false;">' . esc_html__( 'Remove thumbnail' ) . '</a></p>'; 
    10801086        } 
     1087        $content_width = $old_content_width; 
    10811088    } 
    10821089 
  • trunk/wp-includes/media.php

    r12338 r12342  
    3333 */ 
    3434function image_constrain_size_for_editor($width, $height, $size = 'medium') { 
    35     global $content_width; 
     35    global $content_width, $_wp_additional_image_sizes; 
    3636 
    3737    if ( is_array($size) ) { 
     
    6262        if ( intval($content_width) > 0 ) 
    6363            $max_width = min( intval($content_width), $max_width ); 
     64    } elseif ( isset( $_wp_additional_image_sizes ) && count( $_wp_additional_image_sizes ) && in_array( $size, array_keys( $_wp_additional_image_sizes ) ) ) { 
     65        $max_width = intval( $_wp_additional_image_sizes[$size]['width'] ); 
     66        $max_height = intval( $_wp_additional_image_sizes[$size]['height'] ); 
     67        if ( intval($content_width) > 0 ) 
     68            $max_width = min( intval($content_width), $max_width ); 
    6469    } 
    6570    // $size == 'full' has no constraint 
     
    168173    return false; 
    169174 
     175} 
     176 
     177/** 
     178 * Registers a new image size 
     179 */ 
     180function add_image_size( $name, $width = 0, $height = 0, $crop = FALSE ) { 
     181    global $_wp_additional_image_sizes; 
     182    $_wp_additional_image_sizes[$name] = array( 'width' => absint( $width ), 'height' => absint( $height ), 'crop' => !!$crop ); 
     183} 
     184 
     185/** 
     186 * Registers an image size for the post image 
     187 */ 
     188function set_post_image_size( $width = 0, $height = 0, $crop = FALSE ) { 
     189    add_image_size( 'post-image', $width, $height, $crop ); 
    170190} 
    171191 
  • trunk/wp-includes/post-image-template.php

    r12339 r12342  
    4343 * @since 2.9.0 
    4444 * 
    45  * @param int $size Optional. Image size.  Defaults to 'thumbnail'. 
     45 * @param int $size Optional. Image size.  Defaults to 'post-image', which theme sets using set_post_image_size( $width, $height, $crop_flag );. 
    4646 * @param string|array $attr Optional. Query string or array of attributes. 
    4747 */ 
    48 function the_post_image( $size = 'thumbnail', $attr = '' ) { 
     48function the_post_image( $size = 'post-image', $attr = '' ) { 
    4949    echo get_the_post_image( NULL, $size, $attr ); 
    5050} 
     
    5959 * @param string|array $attr Optional. Query string or array of attributes. 
    6060  */ 
    61 function get_the_post_image( $post_id = NULL, $size = 'thumbnail', $attr = '' ) { 
     61function get_the_post_image( $post_id = NULL, $size = 'post-image', $attr = '' ) { 
    6262    global $id; 
    6363    $post_id = ( NULL === $post_id ) ? $id : $post_id; 
Note: See TracChangeset for help on using the changeset viewer.