Make WordPress Core

Changeset 7135


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.

Location:
trunk
Files:
7 edited

Legend:

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

    r7041 r7135  
    1818 */
    1919function wp_create_thumbnail( $file, $max_side, $deprecated = '' ) {
    20     if ( ctype_digit( $file ) ) // Handle int as attachment ID
    21         $file = get_attached_file( $file );
    22 
    23     $image = wp_load_image( $file );
    24 
    25     if ( !is_resource( $image ) )
    26         return $image;
    27 
    28     list($sourceImageWidth, $sourceImageHeight, $sourceImageType) = getimagesize( $file );
    29 
    30     if ( function_exists( 'imageantialias' ))
    31         imageantialias( $image, true );
    32 
    33     list($image_new_width, $image_new_height) = wp_shrink_dimensions( $sourceImageWidth, $sourceImageHeight, $max_side, $max_side);
    34 
    35     $thumbnail = imagecreatetruecolor( $image_new_width, $image_new_height);
    36 
    37     // preserve PNG transparency
    38     if ( IMAGETYPE_PNG == $sourceImageType && function_exists( 'imagealphablending' ) && function_exists( 'imagesavealpha' ) ) {
    39         imagealphablending( $thumbnail, false);
    40         imagesavealpha( $thumbnail, true);
    41     }
    42 
    43     imagecopyresampled( $thumbnail, $image, 0, 0, 0, 0, $image_new_width, $image_new_height, $sourceImageWidth, $sourceImageHeight );
    44 
    45     imagedestroy( $image ); // Free up memory
    46 
    47     // If no filters change the filename, we'll do a default transformation.
    48     if ( basename( $file ) == $thumb = apply_filters( 'thumbnail_filename', basename( $file ) ) )
    49         $thumb = preg_replace( '!(\.[^.]+)?$!', '.thumbnail$1', basename( $file ), 1 );
    50 
    51     $thumbpath = str_replace( basename( $file ), $thumb, $file );
    52 
    53     switch( $sourceImageType ){
    54         default: // We'll create a Jpeg if we cant use its native file format
    55             $thumb = preg_replace( '/\\.[^\\.]+$/', '.jpg', $thumb ); //Change file extension to Jpg
    56         case IMAGETYPE_JPEG:
    57             if (!imagejpeg( $thumbnail, $thumbpath ) )
    58                 return __( 'Thumbnail path invalid' );
    59             break;
    60         case IMAGETYPE_GIF:
    61             if (!imagegif( $thumbnail, $thumbpath ) )
    62                 return __( 'Thumbnail path invalid' );
    63             break;
    64         case IMAGETYPE_PNG:
    65             if (!imagepng( $thumbnail, $thumbpath ) )
    66                 return __( 'Thumbnail path invalid' );
    67             break;
    68     }
    69 
    70     imagedestroy( $thumbnail ); // Free up memory
    71 
    72     // Set correct file permissions
    73     $stat = stat( dirname( $thumbpath ));
    74     $perms = $stat['mode'] & 0000666; //same permissions as parent folder, strip off the executable bits
    75     @ chmod( $thumbpath, $perms );
    76 
     20   
     21    $thumbpath = image_resize( $file, $max_side, $max_side );
    7722    return apply_filters( 'wp_create_thumbnail', $thumbpath );
    7823}
     
    14388
    14489    $metadata = array();
    145     if ( preg_match('!^image/!', get_post_mime_type( $attachment )) ) {
     90    if ( preg_match('!^image/!', get_post_mime_type( $attachment )) && file_is_displayable_image($file) ) {
    14691        $imagesize = getimagesize( $file );
    14792        $metadata['width'] = $imagesize[0];
     
    15196        $metadata['file'] = $file;
    15297
    153         $max = apply_filters( 'wp_thumbnail_creation_size_limit', absint( WP_MEMORY_LIMIT ) * 1024 * 1024, $attachment_id, $file );
    154 
    155         if ( $max < 0 || $metadata['width'] * $metadata['height'] < $max ) {
    156             $max_side = apply_filters( 'wp_thumbnail_max_side_length', 140, $attachment_id, $file );
    157             $thumb = wp_create_thumbnail( $file, $max_side );
    158             if ( @file_exists($thumb) )
    159                 $metadata['thumb'] = basename($thumb);
     98        // make thumbnails and other intermediate sizes
     99        $sizes = array('thumbnail', 'medium');
     100        $sizes = apply_filters('intermediate_image_sizes', $sizes);
     101       
     102        foreach ($sizes as $size) {
     103            $resized = image_make_intermediate_size( $file, get_option("{$size}_size_w"), get_option("{$size}_size_h"), get_option("{$size}_crop") );
     104            if ( $resized )
     105                $metadata['sizes'][$size] = $resized;
    160106        }
    161 
     107           
    162108        // fetch additional metadata from exif/iptc
    163109        $image_meta = wp_read_image_metadata( $file );
     
    310256}
    311257
     258// is the file a real image file?
     259function file_is_valid_image($path) {
     260    $size = @getimagesize($path);
     261    return !empty($size);
     262}
     263
     264// is the file an image suitable for displaying within a web page?
     265function file_is_displayable_image($path) {
     266    $info = @getimagesize($path);
     267    if ( empty($info) )
     268        $result = false;
     269    elseif ( !in_array($info[2], array(IMAGETYPE_GIF, IMAGETYPE_JPEG, IMAGETYPE_PNG)) )
     270        // only gif, jpeg and png images can reliably be displayed
     271        $result = false;
     272    elseif ( $info['channels'] > 0 && $info['channels'] != 3 ) {
     273        // some web browsers can't display cmyk or grayscale jpegs
     274        $result = false;
     275    }
     276    else
     277        $result = true;
     278       
     279    return apply_filters('file_is_displayable_image', $result, $path);
     280}
    312281
    313282?>
  • trunk/wp-admin/includes/media.php

    r7130 r7135  
    5656    if ( $url )
    5757        $html = "<a href='".attribute_escape($url)."'$rel>$html</a>";
    58     elseif ( $size == 'thumb' || $size == 'medium' )
     58    elseif ( $size == 'thumbnail' || $size == 'medium' )
    5959        $html = '<a href="'.get_attachment_link($id).'"'.$rel.'>'.$html.'</a>';
    6060
     
    468468            'input' => 'html',
    469469            'html'  => "
    470                 " . ( $thumb ? "<input type='radio' name='attachments[$post->ID][image-size]' id='image-size-thumb-$post->ID' value='thumb' />
     470                " . ( $thumb ? "<input type='radio' name='attachments[$post->ID][image-size]' id='image-size-thumb-$post->ID' value='thumbnail' />
    471471                <label for='image-size-thumb-$post->ID'>" . __('Thumbnail') . "</label>
    472472                " : '' ) . "<input type='radio' name='attachments[$post->ID][image-size]' id='image-size-medium-$post->ID' value='medium' checked='checked' />
  • trunk/wp-admin/includes/schema.php

    r6916 r7135  
    245245    add_option('avatar_rating', 'G');
    246246    add_option('upload_url_path', '');
     247    add_option('thumbnail_size_w', 150);
     248    add_option('thumbnail_size_h', 150);
     249    add_option('thumbnail_crop', 1);
     250    add_option('medium_size_w', '');
     251    add_option('medium_size_h', '');
    247252
    248253    // Delete unused options
  • trunk/wp-admin/options-writing.php

    r7000 r7135  
    6969<label for="thumbnail_size_h"><?php _e('Height'); ?></label>
    7070<input name="thumbnail_size_h" type="text" id="thumbnail_size_h" value="<?php form_option('thumbnail_size_h'); ?>" size="6" />
     71<input name="thumbnail_crop" type="checkbox" id="thumbnail_crop" value="1" <?php checked('1', get_option('thumbnail_crop')); ?>/>
     72<label for="thumbnail_crop"><?php _e('Crop to size'); ?></label>
    7173</td>
    7274</tr>
     
    135137<p class="submit">
    136138<input type="hidden" name="action" value="update" />
    137 <input type="hidden" name="page_options" value="default_post_edit_rows,use_smilies,ping_sites,mailserver_url,mailserver_port,mailserver_login,mailserver_pass,default_category,default_email_category,use_balanceTags,default_link_category" />
     139<input type="hidden" name="page_options" value="default_post_edit_rows,use_smilies,ping_sites,mailserver_url,mailserver_port,mailserver_login,mailserver_pass,default_category,default_email_category,use_balanceTags,default_link_category,thumbnail_size_w,thumbnail_size_h,thumbnail_crop,medium_size_w,medium_size_h" />
    138140<input type="submit" name="Submit" value="<?php _e('Save Changes') ?>" />
    139141</p>
  • 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?>
  • trunk/wp-includes/post-template.php

    r7073 r7135  
    390390    $file = get_attached_file( $post->ID );
    391391
    392     if ( !$fullsize && $thumbfile = wp_get_attachment_thumb_file( $post->ID ) ) {
     392    if ( !$fullsize && $src = wp_get_attachment_thumb_url( $post->ID ) ) {
    393393        // We have a thumbnail desired, specified and existing
    394394
    395         $src = wp_get_attachment_thumb_url( $post->ID );
    396         $src_file = $thumbfile;
     395        $src_file = basename($src);
    397396        $class = 'attachmentthumb';
    398397    } elseif ( wp_attachment_is_image( $post->ID ) ) {
  • trunk/wp-includes/post.php

    r7130 r7135  
    21292129    if ( $file )
    21302130        update_attached_file( $post_ID, $file );
    2131 
     2131       
    21322132    clean_post_cache($post_ID);
    21332133
     
    23172317    if ( !$url = wp_get_attachment_url( $post->ID ) )
    23182318        return false;
     2319       
     2320    $sized = image_downsize( $post_id, 'thumbnail' );
     2321    if ( $sized )
     2322        return $sized[0];
    23192323
    23202324    if ( !$thumb = wp_get_attachment_thumb_file( $post->ID ) )
Note: See TracChangeset for help on using the changeset viewer.