Make WordPress Core

Ticket #6074: intermediate-image-sizing-r7133-3.patch

File intermediate-image-sizing-r7133-3.patch, 14.3 KB (added by tellyworth, 19 years ago)
  • wordpress/wp-includes/post-template.php

     
    389389
    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 ) ) {
    399398                // We have an image without a thumbnail
  • wordpress/wp-includes/post.php

     
    21282128
    21292129        if ( $file )
    21302130                update_attached_file( $post_ID, $file );
    2131 
     2131               
    21322132        clean_post_cache($post_ID);
    21332133
    21342134        if ( $update) {
     
    23162316                return false;
    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 ) )
    23212325                return false;
  • wordpress/wp-includes/media.php

     
    4848}
    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') {
    5454
     
    6060        if ( $out = apply_filters('image_downsize', false, $id, $size) )
    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);
     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];
    7075                }
    71                 // this could be improved to provide a default thumbnail if one doesn't exist
    7276        }
    7377        elseif ( isset($meta['width'], $meta['height']) ) {
    7478                // any other type: use the real image and constrain it
     
    226230        return $destfilename;
    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?>
     261 No newline at end of file
  • wordpress/wp-admin/includes/schema.php

     
    244244        add_option('show_avatars', '1');
    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
    249254        $unusedoptions = array ('blodotgsping_url', 'bodyterminator', 'emailtestonly', 'phoneemail_separator', 'smilies_directory', 'subjectprefix', 'use_bbcode', 'use_blodotgsping', 'use_phoneemail', 'use_quicktags', 'use_weblogsping', 'weblogs_cache_file', 'use_preview', 'use_htmltrans', 'smilies_directory', 'fileupload_allowedusers', 'use_phoneemail', 'default_post_status', 'default_post_category', 'archive_mode', 'time_difference', 'links_minadminlevel', 'links_use_adminlevels', 'links_rating_type', 'links_rating_char', 'links_rating_ignore_zero', 'links_rating_single_image', 'links_rating_image0', 'links_rating_image1', 'links_rating_image2', 'links_rating_image3', 'links_rating_image4', 'links_rating_image5', 'links_rating_image6', 'links_rating_image7', 'links_rating_image8', 'links_rating_image9', 'weblogs_cacheminutes', 'comment_allowed_tags', 'search_engine_friendly_urls', 'default_geourl_lat', 'default_geourl_lon', 'use_default_geourl', 'weblogs_xml_url', 'new_users_can_blog', '_wpnonce', '_wp_http_referer', 'Update', 'action', 'rich_editing');
  • wordpress/wp-admin/includes/media.php

     
    5555        $rel = $rel ? ' rel="attachment wp-att-'.attribute_escape($id).'"' : '';
    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
    6161        $html = apply_filters( 'image_send_to_editor', $html, $id, $alt, $title, $align, $url );
     
    467467                        'label' => __('Size'),
    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' />
    473473                                <label for='image-size-medium-$post->ID'>" . __('Medium') . "</label>
  • wordpress/wp-admin/includes/image.php

     
    1717 * If PHP does not have the functionality to save in a file of the same format, the thumbnail will be created as a jpeg.
    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}
    7924
     
    14287        $attachment = get_post( $attachment_id );
    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];
    14893                $metadata['height'] = $imagesize[1];
     
    15095                $metadata['hwstring_small'] = "height='$uheight' width='$uwidth'";
    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 );
    164110                if ($image_meta)
    165111                        $metadata['image_meta'] = $image_meta;
    166112
    167113        }
     114        var_dump($metadata);
    168115        return apply_filters( 'wp_generate_attachment_metadata', $metadata );
    169116}
    170117
     
    309256
    310257}
    311258
     259// is the file a real image file?
     260function file_is_valid_image($path) {
     261        $size = @getimagesize($path);
     262        return !empty($size);
     263}
    312264
     265// is the file an image suitable for displaying within a web page?
     266function file_is_displayable_image($path) {
     267        $info = @getimagesize($path);
     268        if ( empty($info) )
     269                $result = false;
     270        elseif ( !in_array($info[2], array(IMAGETYPE_GIF, IMAGETYPE_JPEG, IMAGETYPE_PNG)) )
     271                // only gif, jpeg and png images can reliably be displayed
     272                $result = false;
     273        elseif ( $info['channels'] > 0 && $info['channels'] != 3 ) {
     274                // some web browsers can't display cmyk or grayscale jpegs
     275                $result = false;
     276        }
     277        else
     278                $result = true;
     279               
     280        return apply_filters('file_is_displayable_image', $result, $path);
     281}
     282
    313283?>
  • wordpress/wp-admin/includes/upload.php

     
    5959                $thumb_base = wp_mime_type_icon();
    6060        if ( $thumb_base ) {
    6161                $thumb_rel = wp_make_link_relative( $thumb_base );
     62                var_dump("thumb_base = $thumb_base", "thumb_rel = $thumb_rel");
    6263                $thumb_base = str_replace( $thumb_rel, '', $thumb_base );
    6364                $r .= "\t\t\t\t<input type='hidden' name='attachment-thumb-url-$id' id='attachment-thumb-url-$id' value='$thumb_rel' />\n";
    6465                $r .= "\t\t\t\t<input type='hidden' name='attachment-thumb-url-base-$id' id='attachment-thumb-url-base-$id' value='$thumb_base' />\n";
  • wordpress/wp-admin/options-writing.php

     
    6868<input name="thumbnail_size_w" type="text" id="thumbnail_size_w" value="<?php form_option('thumbnail_size_w'); ?>" size="6" />
    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>
    7375<tr valign="top">
     
    134136
    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>
    140142</form>