WordPress.org

Make WordPress Core

Changeset 11746


Ignore:
Timestamp:
07/27/2009 06:32:30 PM (11 years ago)
Author:
azaozz
Message:

Use exif rotation data when creating sub-sizes of uploaded jpeg images, see #7042

File:
1 edited

Legend:

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

    r11737 r11746  
    331331 * @return mixed WP_Error on failure. String with new destination path. Array of dimensions from {@link image_resize_dimensions()}
    332332 */
    333 function image_resize( $file, $max_w, $max_h, $crop=false, $suffix=null, $dest_path=null, $jpeg_quality=90) {
     333function image_resize( $file, $max_w, $max_h, $crop = false, $suffix = null, $dest_path = null, $jpeg_quality = 90 ) {
    334334
    335335    $image = wp_load_image( $file );
     
    337337        return new WP_Error('error_loading_image', $image);
    338338
    339     list($orig_w, $orig_h, $orig_type) = getimagesize( $file );
     339    $size = @getimagesize( $file );
     340    if ( !$size )
     341        return new WP_Error('invalid_image', __('Could not read image size'), $file);
     342    list($orig_w, $orig_h, $orig_type) = $size;
     343
    340344    $dims = image_resize_dimensions($orig_w, $orig_h, $max_w, $max_h, $crop);
    341     if (!$dims)
     345    if ( !$dims )
    342346        return $dims;
    343347    list($dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h) = $dims;
     
    372376    $destfilename = "{$dir}/{$name}-{$suffix}.{$ext}";
    373377
    374     if ( $orig_type == IMAGETYPE_GIF ) {
    375         if (!imagegif( $newimage, $destfilename ) )
     378    if ( IMAGETYPE_GIF == $orig_type ) {
     379        if ( !imagegif( $newimage, $destfilename ) )
    376380            return new WP_Error('resize_path_invalid', __( 'Resize path invalid' ));
    377     }
    378     elseif ( $orig_type == IMAGETYPE_PNG ) {
     381    } elseif ( IMAGETYPE_PNG == $orig_type ) {
    379382        if (!imagepng( $newimage, $destfilename ) )
    380383            return new WP_Error('resize_path_invalid', __( 'Resize path invalid' ));
    381     }
    382     else {
     384    } else {
     385        $rotated = false;
     386        if ( IMAGETYPE_JPEG == $orig_type ) {
     387            // rotate if EXIF 'Orientation' is set
     388            $exif = exif_read_data($file, null, true);
     389            if ( $exif && isset($exif['IFD0']) && is_array($exif['IFD0']) && isset($exif['IFD0']['Orientation']) ) {
     390                if ( 6 == $exif['IFD0']['Orientation'] )
     391                    $rotated = rotate_image($newimage, 90);
     392                elseif ( 8 == $exif['IFD0']['Orientation'] )
     393                    $rotated = rotate_image($newimage, 270);
     394            }
     395        }
     396
    383397        // all other formats are converted to jpg
    384398        $destfilename = "{$dir}/{$name}-{$suffix}.jpg";
    385         if (!imagejpeg( $newimage, $destfilename, apply_filters( 'jpeg_quality', $jpeg_quality, 'image_resize' ) ) )
     399        if ( $rotated ) {
     400            $return = imagejpeg( $rotated, $destfilename, apply_filters( 'jpeg_quality', $jpeg_quality, 'image_resize' ) );
     401            imagedestroy($rotated);
     402        } else {
     403            $return = imagejpeg( $newimage, $destfilename, apply_filters( 'jpeg_quality', $jpeg_quality, 'image_resize' ) );
     404        }
     405
     406        if ( !$return )
    386407            return new WP_Error('resize_path_invalid', __( 'Resize path invalid' ));
    387408    }
     
    798819}
    799820
    800 ?>
     821/**
     822 * Rotate an image.
     823 *
     824 * @since 2.9.0
     825 *
     826 * @param resource $src_img GD image handle of source image.
     827 * @param $angle int clockwise angle of rotation, only 90, 180 and 270 are supported.
     828 * @param $keep_transparency bool preserve transparency.
     829 * @return mixed GD image handle of rotated image or false on error.
     830 */
     831function rotate_image( $src_img, $angle, $keep_transparency = false ) {
     832
     833    if ( function_exists('imagerotate') )
     834        return imagerotate($src_img, 360 - $angle, 0); // imagerotate() rotates CCW
     835
     836    if ( 180 == $angle )
     837        return flip_image( $src_img, 'both', $keep_transparency );
     838
     839    $width = imagesx( $src_img );
     840    $height = imagesy( $src_img );
     841
     842    $dest_img = imagecreatetruecolor( $height, $width );
     843    if ( $keep_transparency ) {
     844        imagealphablending($dest_img, false);
     845        imagesavealpha($dest_img, true);
     846    }
     847
     848    if ( 90 == $angle ) {
     849        for( $x = 0; $x < $width; $x++ ) {
     850            for( $y = 0; $y < $height; $y++ ) {
     851                if ( !imagecopy($dest_img, $src_img, $height - $y - 1, $x, $x, $y, 1, 1) )
     852                    return false;
     853            }
     854        }
     855    } elseif ( 270 == $angle ) {
     856        for( $x = 0; $x < $width; $x++ ) {
     857            for( $y = 0; $y < $height; $y++ ) {
     858                if ( !imagecopy($dest_img, $src_img, $y, $width - $x - 1, $x, $y, 1, 1) )
     859                    return false;
     860            }
     861        }
     862    } else {
     863        return false;
     864    }
     865
     866    return $dest_img;
     867}
     868
     869/**
     870 * Flip an image.
     871 *
     872 * @since 2.9.0
     873 *
     874 * @param resource $src_img GD image handle of source image.
     875 * @param $mode string 'horizontal', 'vertical' or 'both'.
     876 * @param $keep_transparency bool preserve transparency.
     877 * @return mixed GD image handle of flipped image or false on error.
     878 */
     879function flip_image( $src_img, $mode, $keep_transparency = false ) {
     880
     881    $width = $src_width = imagesx( $src_img );
     882    $height = $src_height = imagesy( $src_img );
     883    $src_x = $src_y = 0;
     884
     885    switch ( $mode ) {
     886        case 'vertical':
     887            $src_y = $height -1;
     888            $src_height = -$height;
     889            break;
     890        case 'horizontal':
     891            $src_x = $width -1;
     892            $src_width = -$width;
     893            break;
     894        case 'both':
     895            if ( function_exists('imagerotate') )
     896                return imagerotate($src_img, 180, 0);
     897
     898            $src_x = $width -1;
     899            $src_y = $height -1;
     900            $src_width = -$width;
     901            $src_height = -$height;
     902            break;
     903        default:
     904            return false;
     905    }
     906
     907    $dest_img = imagecreatetruecolor( $width, $height );
     908    if ( $keep_transparency ) {
     909        imagealphablending($dest_img, false);
     910        imagesavealpha($dest_img, true);
     911    }
     912
     913    if ( imagecopyresampled( $dest_img, $src_img, 0, 0, $src_x, $src_y , $width, $height, $src_width, $src_height ) )
     914        return $dest_img;
     915
     916    return false;
     917}
Note: See TracChangeset for help on using the changeset viewer.