Make WordPress Core


Ignore:
Timestamp:
03/03/2008 04:17:37 AM (17 years ago)
Author:
matt
Message:

Creating intermediate sizes, better thumbnails, and other image improvements. Hat tip: tellyworth.

File:
1 edited

Legend:

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

    r7130 r7135  
    4949
    5050// Scale an image to fit a particular size (such as 'thumb' or 'medium'), and return an image URL, height and width.
    51 // The URL might be the original image, or it might be a resized version.
     51// 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.
    5252// returns an array($url, $width, $height)
    5353function image_downsize($id, $size = 'medium') {
     
    6161        return $out;
    6262
    63     if ( $size == 'thumb' ) {
    64         // thumbnail: use the thumb as the displayed image, and constrain based on its dimensions
    65         $thumb_path = wp_get_attachment_thumb_file($id);
    66         // the actual thumbnail size isn't stored so we'll have to calculate it
    67         if ( $thumb_path && ($info = getimagesize($thumb_path)) ) {
    68             list( $width, $height ) = image_constrain_size_for_editor( $info[0], $info[1], $size );
    69             $img_url = wp_get_attachment_thumb_url($id);
    70         }
    71         // this could be improved to provide a default thumbnail if one doesn't exist
     63    // try for a new style intermediate size
     64    if ( $intermediate = image_get_intermediate_size($id, $size) ) {
     65        $img_url = str_replace(basename($img_url), $intermediate['file'], $img_url);
     66        $width = $intermediate['width'];
     67        $height = $intermediate['height'];
     68    }
     69    elseif ( $size == 'thumbnail' ) {
     70        // fall back to the old thumbnail
     71        if ( $thumb_file = wp_get_attachment_thumb_file() && $info = getimagesize($thumb_file) ) {
     72            $img_url = str_replace(basename($img_url), basename($thumb_file), $img_url);
     73            $width = $info[0];
     74            $height = $info[1];
     75        }
    7276    }
    7377    elseif ( isset($meta['width'], $meta['height']) ) {
     
    227231}
    228232
     233// resize an image to make a thumbnail or intermediate size, and return metadata describing the new copy
     234// returns false if no image was created
     235function image_make_intermediate_size($file, $width, $height, $crop=false) {
     236    if ( $width || $height ) {
     237        $resized_file = image_resize($file, $width, $height, $crop);
     238        if ( $resized_file && $info = getimagesize($resized_file) ) {
     239            return array(
     240                'file' => basename( $resized_file ),
     241                'width' => $info[0],
     242                'height' => $info[1],
     243            );
     244        }
     245    }
     246    return false;
     247}
     248
     249function image_get_intermediate_size($post_id, $size='thumbnail') {
     250    if ( !$imagedata = wp_get_attachment_metadata( $post_id ) )
     251        return false;
     252       
     253    if ( empty($imagedata['sizes'][$size]) )
     254        return false;
     255       
     256    return $imagedata['sizes'][$size];
     257}
     258
     259
    229260?>
Note: See TracChangeset for help on using the changeset viewer.