WordPress.org

Make WordPress Core

Changeset 6309


Ignore:
Timestamp:
11/03/2007 06:33:19 PM (11 years ago)
Author:
westi
Message:

Refactor of wp-admin/includes/image.php. Fixes #5312, #4151, #4709, #5304 props DD32.

Location:
trunk
Files:
2 edited

Legend:

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

    r5700 r6309  
    11<?php
    2 
    3 function get_udims( $width, $height) {
    4     if ( $height <= 96 && $width <= 128 )
    5         return array( $width, $height);
    6     elseif ( $width / $height > 4 / 3 )
    7         return array( 128, (int) ($height / $width * 128 ));
    8     else
    9         return array( (int) ($width / $height * 96 ), 96 );
    10 }
    11 
    12 function wp_create_thumbnail( $file, $max_side, $effect = '' ) {
    13 
    14         // 1 = GIF, 2 = JPEG, 3 = PNG
    15 
    16     if ( file_exists( $file ) ) {
    17         $type = getimagesize( $file );
    18 
    19         // if the associated function doesn't exist - then it's not
    20         // handle. duh. i hope.
    21 
    22         if (!function_exists( 'imagegif' ) && $type[2] == 1 ) {
    23             $error = __( 'Filetype not supported. Thumbnail not created.' );
    24         }
    25         elseif (!function_exists( 'imagejpeg' ) && $type[2] == 2 ) {
    26             $error = __( 'Filetype not supported. Thumbnail not created.' );
    27         }
    28         elseif (!function_exists( 'imagepng' ) && $type[2] == 3 ) {
    29             $error = __( 'Filetype not supported. Thumbnail not created.' );
    30         } else {
    31 
    32             // create the initial copy from the original file
    33             if ( $type[2] == 1 ) {
    34                 $image = imagecreatefromgif( $file );
    35             }
    36             elseif ( $type[2] == 2 ) {
    37                 $image = imagecreatefromjpeg( $file );
    38             }
    39             elseif ( $type[2] == 3 ) {
    40                 $image = imagecreatefrompng( $file );
    41             }
    42 
    43             if ( function_exists( 'imageantialias' ))
    44                 imageantialias( $image, TRUE );
    45 
    46             $image_attr = getimagesize( $file );
    47 
    48             // figure out the longest side
    49 
    50             if ( $image_attr[0] > $image_attr[1] ) {
    51                 $image_width = $image_attr[0];
    52                 $image_height = $image_attr[1];
    53                 $image_new_width = $max_side;
    54 
    55                 $image_ratio = $image_width / $image_new_width;
    56                 $image_new_height = $image_height / $image_ratio;
    57                 //width is > height
    58             } else {
    59                 $image_width = $image_attr[0];
    60                 $image_height = $image_attr[1];
    61                 $image_new_height = $max_side;
    62 
    63                 $image_ratio = $image_height / $image_new_height;
    64                 $image_new_width = $image_width / $image_ratio;
    65                 //height > width
    66             }
    67 
    68             $thumbnail = imagecreatetruecolor( $image_new_width, $image_new_height);
    69             @ imagecopyresampled( $thumbnail, $image, 0, 0, 0, 0, $image_new_width, $image_new_height, $image_attr[0], $image_attr[1] );
    70 
    71             // If no filters change the filename, we'll do a default transformation.
    72             if ( basename( $file ) == $thumb = apply_filters( 'thumbnail_filename', basename( $file ) ) )
    73                 $thumb = preg_replace( '!(\.[^.]+)?$!', '.thumbnail' . '$1', basename( $file ), 1 );
    74 
    75             $thumbpath = str_replace( basename( $file ), $thumb, $file );
    76 
    77             // move the thumbnail to its final destination
    78             if ( $type[2] == 1 ) {
    79                 if (!imagegif( $thumbnail, $thumbpath ) ) {
    80                     $error = __( "Thumbnail path invalid" );
    81                 }
    82             }
    83             elseif ( $type[2] == 2 ) {
    84                 if (!imagejpeg( $thumbnail, $thumbpath ) ) {
    85                     $error = __( "Thumbnail path invalid" );
    86                 }
    87             }
    88             elseif ( $type[2] == 3 ) {
    89                 if (!imagepng( $thumbnail, $thumbpath ) ) {
    90                     $error = __( "Thumbnail path invalid" );
    91                 }
    92             }
    93 
    94         }
    95     } else {
    96         $error = __( 'File not found' );
    97     }
    98 
    99     if (!empty ( $error ) ) {
    100         return $error;
    101     } else {
    102         return apply_filters( 'wp_create_thumbnail', $thumbpath );
    103     }
    104 }
    105 
     2/**
     3 * File contains all the administration image manipulation functions.
     4 *
     5 * @package WordPress
     6 */
     7
     8/**
     9 * wp_create_thumbnail() - Create a thumbnail from an Image given a maximum side size.
     10 *
     11 * @package WordPress
     12 * @param   mixed   $file   Filename of the original image, Or attachment id
     13 * @param   int     $max_side   Maximum length of a single side for the thumbnail
     14 * @return  string          Thumbnail path on success, Error string on failure
     15 *
     16 * This function can handle most image file formats which PHP supports.
     17 * If PHP does not have the functionality to save in a file of the same format, the thumbnail will be created as a jpeg.
     18 */
     19function wp_create_thumbnail( $file, $max_side, $depreciated = '' ) {
     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    @ imagecopyresampled( $thumbnail, $image, 0, 0, 0, 0, $image_new_width, $image_new_height, $sourceImageWidth, $sourceImageHeight );
     37
     38    imagedestroy( $image ); // Free up memory
     39
     40    // If no filters change the filename, we'll do a default transformation.
     41    if ( basename( $file ) == $thumb = apply_filters( 'thumbnail_filename', basename( $file ) ) )
     42        $thumb = preg_replace( '!(\.[^.]+)?$!', '.thumbnail$1', basename( $file ), 1 );
     43
     44    $thumbpath = str_replace( basename( $file ), $thumb, $file );
     45
     46    switch( $sourceImageType ){
     47        default: // We'll create a Jpeg if we cant use its native file format
     48            $thumb = preg_replace( '/\\.[^\\.]+$/', '.jpg', $thumb ); //Change file extension to Jpg
     49        case IMAGETYPE_JPEG:
     50            if (!imagejpeg( $thumbnail, $thumbpath ) )
     51                return __( 'Thumbnail path invalid' );
     52            break;
     53        case IMAGETYPE_GIF:
     54            if (!imagegif( $thumbnail, $thumbpath ) )
     55                return __( 'Thumbnail path invalid' );
     56            break;
     57        case IMAGETYPE_PNG:
     58            if (!imagepng( $thumbnail, $thumbpath ) )
     59                return __( 'Thumbnail path invalid' );
     60            break;
     61    }
     62
     63    imagedestroy( $thumbnail ); // Free up memory
     64   
     65    // Set correct file permissions
     66    $stat = stat( dirname( $thumbpath ));
     67    $perms = $stat['mode'] & 0000666; //same permissions as parent folder, strip off the executable bits
     68    @ chmod( $thumbpath, $perms );
     69
     70    return apply_filters( 'wp_create_thumbnail', $thumbpath );
     71}
     72
     73/**
     74 * wp_crop_image() - Crop an Image to a given size.
     75 *
     76 * @package WordPress
     77 * @internal Missing Long Description
     78 * @param   int $src_file   The source file
     79 * @param   int $src_x      The start x position to crop from
     80 * @param   int $src_y      The start y position to crop from
     81 * @param   int $src_w      The width to crop
     82 * @param   int $src_h      The height to crop
     83 * @param   int $dst_w      The destination width
     84 * @param   int $dst_h      The destination height
     85 * @param   int $src_abs    If the source crop points are absolute
     86 * @param   int $dst_file   The destination file to write to
     87 * @return  string          New filepath on success, String error message on failure
     88 *
     89 */
    10690function wp_crop_image( $src_file, $src_x, $src_y, $src_w, $src_h, $dst_w, $dst_h, $src_abs = false, $dst_file = false ) {
    10791    if ( ctype_digit( $src_file ) ) // Handle int as attachment ID
     
    124108
    125109    imagecopyresampled( $dst, $src, 0, 0, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h );
    126 
    127     if ( !$dst_file )
    128         $dst_file = str_replace( basename( $src_file ), 'cropped-'.basename( $src_file ), $src_file );
     110   
     111    imagedestroy( $src ); // Free up memory
     112
     113    if ( ! $dst_file )
     114        $dst_file = str_replace( basename( $src_file ), 'cropped-' . basename( $src_file ), $src_file );
    129115
    130116    $dst_file = preg_replace( '/\\.[^\\.]+$/', '.jpg', $dst_file );
     
    136122}
    137123
     124/**
     125 * wp_generate_attachment_metadata() - Generate post Image attachment Metadata
     126 *
     127 * @package WordPress
     128 * @internal Missing Long Description
     129 * @param   int     $attachment_id  Attachment Id to process
     130 * @param   string  $file   Filepath of the Attached image
     131 * @return  mixed           Metadata for attachment
     132 *
     133 */
    138134function wp_generate_attachment_metadata( $attachment_id, $file ) {
    139135    $attachment = get_post( $attachment_id );
     
    141137    $metadata = array();
    142138    if ( preg_match('!^image/!', get_post_mime_type( $attachment )) ) {
    143         $imagesize = getimagesize($file);
    144         $metadata['width'] = $imagesize['0'];
    145         $metadata['height'] = $imagesize['1'];
    146         list($uwidth, $uheight) = get_udims($metadata['width'], $metadata['height']);
     139        $imagesize = getimagesize( $file );
     140        $metadata['width'] = $imagesize[0];
     141        $metadata['height'] = $imagesize[1];
     142        list($uwidth, $uheight) = wp_shrink_dimensions($metadata['width'], $metadata['height']);
    147143        $metadata['hwstring_small'] = "height='$uheight' width='$uwidth'";
    148144        $metadata['file'] = $file;
     
    161157}
    162158
     159/**
     160 * wp_load_image() - Load an image which PHP Supports.
     161 *
     162 * @package WordPress
     163 * @internal Missing Long Description
     164 * @param   string  $file   Filename of the image to load
     165 * @return  resource        The resulting image resource on success, Error string on failure.
     166 *
     167 */
    163168function wp_load_image( $file ) {
    164169    if ( ctype_digit( $file ) )
    165170        $file = get_attached_file( $file );
    166171
    167     if ( !file_exists( $file ) )
     172    if ( ! file_exists( $file ) )
    168173        return sprintf(__("File '%s' doesn't exist?"), $file);
    169174
     
    171176        return __('The GD image library is not installed.');
    172177
    173     $contents = file_get_contents( $file );
    174 
    175     $image = imagecreatefromstring( $contents );
     178    $image = @imagecreatefromstring( @file_get_contents( $file ) );
    176179
    177180    if ( !is_resource( $image ) )
     
    181184}
    182185
     186/**
     187 * get_udims() - Calculated the new dimentions for downsampled images
     188 *
     189 * @package WordPress
     190 * @internal Missing Description
     191 * @see wp_shrink_dimensions()
     192 * @param   int     $width  Current width of the image
     193 * @param   int     $height Current height of the image
     194 * @return  mixed           Array(height,width) of shrunk dimensions.
     195 *
     196 */
     197function get_udims( $width, $height) {
     198    return wp_shrink_dimensions( $width, $height );
     199}
     200/**
     201 * wp_shrink_dimensions() - Calculates the new dimentions for a downsampled image.
     202 *
     203 * @package WordPress
     204 * @internal Missing Long Description
     205 * @param   int     $width  Current width of the image
     206 * @param   int     $height Current height of the image
     207 * @param   int     $wmax   Maximum wanted width
     208 * @param   int     $hmax   Maximum wanted height
     209 * @return  mixed           Array(height,width) of shrunk dimensions.
     210 *
     211 */
    183212function wp_shrink_dimensions( $width, $height, $wmax = 128, $hmax = 96 ) {
    184     if ( $height <= $hmax && $width <= $wmax )
     213    if ( $height <= $hmax && $width <= $wmax ){
     214        //Image is smaller than max
    185215        return array( $width, $height);
    186     elseif ( $width / $height > $wmax / $hmax )
     216    } elseif ( $width / $height > $wmax / $hmax ) {
     217        //Image Width will be greatest
    187218        return array( $wmax, (int) ($height / $width * $wmax ));
    188     else
     219    } else {
     220        //Image Height will be greatest
    189221        return array( (int) ($width / $height * $hmax ), $hmax );
     222    }
    190223}
    191224
  • trunk/wp-includes/compat.php

    r6070 r6309  
    148148}
    149149
     150// Added in PHP 4.3.0?
     151if (!defined('IMAGETYPE_GIF'))
     152    define('IMAGETYPE_GIF', 1);
     153
     154if (!defined('IMAGETYPE_JPEG'))
     155    define('IMAGETYPE_JPEG', 2);
     156
     157if (!defined('IMAGETYPE_PNG'))
     158    define('IMAGETYPE_PNG', 3);
     159
     160
    150161?>
Note: See TracChangeset for help on using the changeset viewer.