Make WordPress Core


Ignore:
Timestamp:
03/12/2008 08:10:00 AM (19 years ago)
Author:
ryan
Message:

Image fixes from andy. see #5911

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/media.php

    r7233 r7263  
    66function image_constrain_size_for_editor($width, $height, $size = 'medium') {
    77
    8         if ( $size == 'thumb' ) {
     8        if ( is_array($size) ) {
     9                $max_width = $size[0];
     10                $max_height = $size[1];
     11        }
     12        elseif ( $size == 'thumb' || $size == 'thumbnail' ) {
    913                $max_width = intval(get_option('thumbnail_size_w'));
    1014                $max_height = intval(get_option('thumbnail_size_h'));
     
    7680                }
    7781        }
    78         elseif ( isset($meta['width'], $meta['height']) ) {
     82        if ( !$width && !$height && isset($meta['width'], $meta['height']) ) {
    7983                // any other type: use the real image and constrain it
    8084                list( $width, $height ) = image_constrain_size_for_editor( $meta['width'], $meta['height'], $size );
     
    251255        if ( !$imagedata = wp_get_attachment_metadata( $post_id ) )
    252256                return false;
    253                
    254         if ( empty($imagedata['sizes'][$size]) )
     257
     258        // get the best one for a specified set of dimensions
     259        if ( is_array($size) && !empty($imagedata['sizes']) ) {
     260                foreach ( $imagedata['sizes'] as $_size => $data ) {
     261                        // already cropped to width or height; so use this size
     262                        if ( ( $data['width'] == $size[0] && $data['height'] <= $size[1] ) || ( $data['height'] == $size[1] && $data['width'] <= $size[0] ) ) {
     263                                $file = $data['file'];
     264                                list($width, $height) = image_constrain_size_for_editor( $data['width'], $data['height'], $size );
     265                                return compact( 'file', 'width', 'height' );
     266                        }
     267                        // add to lookup table: area => size
     268                        $areas[$data['width'] * $data['height']] = $_size;
     269                }
     270                if ( !$size || !empty($areas) ) {
     271                        // find for the smallest image not smaller than the desired size
     272                        ksort($areas);
     273                        foreach ( $areas as $_size ) {
     274                                $data = $imagedata['sizes'][$_size];
     275                                if ( $data['width'] >= $size[0] || $data['height'] >= $size[1] ) {
     276                                        $file = $data['file'];
     277                                        list($width, $height) = image_constrain_size_for_editor( $data['width'], $data['height'], $size );
     278                                        return compact( 'file', 'width', 'height' );
     279                                }
     280                        }
     281                }
     282        }
     283
     284        if ( is_array($size) || empty($size) || empty($imagedata['sizes'][$size]) )
    255285                return false;
    256286               
     
    260290// get an image to represent an attachment - a mime icon for files, thumbnail or intermediate size for images
    261291// returns an array (url, width, height), or false if no image is available
    262 function wp_get_attachment_image_src($attachment_id, $size='thumbnail') {
     292function wp_get_attachment_image_src($attachment_id, $size='thumbnail', $icon = false) {
    263293       
    264294        // get a thumbnail or intermediate image if there is one
     
    266296                return $image;
    267297
    268         if ( $src = wp_mime_type_icon($attachment_id) ) {
     298        if ( $icon && $src = wp_mime_type_icon($attachment_id) ) {
    269299                $icon_dir = apply_filters( 'icon_dir', ABSPATH . WPINC . '/images/crystal' );
    270300                $src_file = $icon_dir . '/' . basename($src);
     
    277307
    278308// as per wp_get_attachment_image_src, but returns an <img> tag
    279 function wp_get_attachment_image($attachment_id, $size='thumbnail') {
     309function wp_get_attachment_image($attachment_id, $size='thumbnail', $icon = false) {
    280310
    281311        $html = '';
    282         $image = wp_get_attachment_image_src($attachment_id, $size);
     312        $image = wp_get_attachment_image_src($attachment_id, $size, $icon);
    283313        if ( $image ) {
    284314                list($src, $width, $height) = $image;
    285315                $hwstring = image_hwstring($width, $height);
     316                if ( is_array($size) )
     317                        $size = join('x', $size);
    286318                $html = '<img src="'.attribute_escape($src).'" '.$hwstring.'class="attachment-'.attribute_escape($size).'" />';
    287319        }
     
    323355
    324356        foreach ( $attachments as $id => $attachment ) {
    325                 $link = get_the_attachment_link($id, false, array(128, 96), true);
     357                $link = wp_get_attachment_link($id, 'thumbnail', true);
    326358                $output .= "
    327359                        <div>
     
    359391
    360392        if ( isset($attachments[$k]) )
    361                 echo get_the_attachment_link($attachments[$k]->ID, true, array(128, 96), true);
     393                echo wp_get_attachment_link($attachments[$k]->ID, 'thumbnail', true);
    362394}
    363395
Note: See TracChangeset for help on using the changeset viewer.