Make WordPress Core


Ignore:
Timestamp:
03/03/2008 04:17:37 AM (16 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-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?>
Note: See TracChangeset for help on using the changeset viewer.