Changeset 11899
- Timestamp:
- 09/04/2009 02:36:34 AM (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/wp-includes/media.php
r11838 r11899 380 380 return new WP_Error('resize_path_invalid', __( 'Resize path invalid' )); 381 381 } elseif ( IMAGETYPE_PNG == $orig_type ) { 382 if ( !imagepng( $newimage, $destfilename ) )382 if ( !imagepng( $newimage, $destfilename ) ) 383 383 return new WP_Error('resize_path_invalid', __( 'Resize path invalid' )); 384 384 } else { 385 $rotated = false;386 if ( IMAGETYPE_JPEG == $orig_type && function_exists('exif_read_data') ) {387 // rotate if EXIF 'Orientation' is set388 $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 397 385 // all other formats are converted to jpg 398 386 $destfilename = "{$dir}/{$name}-{$suffix}.jpg"; 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 ) 387 if ( !imagejpeg( $newimage, $destfilename, apply_filters( 'jpeg_quality', $jpeg_quality, 'image_resize' ) ) ) 407 388 return new WP_Error('resize_path_invalid', __( 'Resize path invalid' )); 408 389 } … … 836 817 return array_unique($taxonomies); 837 818 } 838 839 /**840 * Rotate an image.841 *842 * @since 2.9.0843 *844 * @param resource $src_img GD image handle of source image.845 * @param $angle int clockwise angle of rotation, only 90, 180 and 270 are supported.846 * @param $keep_transparency bool preserve transparency.847 * @return mixed GD image handle of rotated image or false on error.848 */849 function rotate_image( $src_img, $angle, $keep_transparency = false ) {850 851 if ( function_exists('imagerotate') )852 return imagerotate($src_img, 360 - $angle, 0); // imagerotate() rotates CCW853 854 if ( 180 == $angle )855 return flip_image( $src_img, 'both', $keep_transparency );856 857 $width = imagesx( $src_img );858 $height = imagesy( $src_img );859 860 $dest_img = imagecreatetruecolor( $height, $width );861 if ( $keep_transparency ) {862 imagealphablending($dest_img, false);863 imagesavealpha($dest_img, true);864 }865 866 if ( 90 == $angle ) {867 for( $x = 0; $x < $width; $x++ ) {868 for( $y = 0; $y < $height; $y++ ) {869 if ( !imagecopy($dest_img, $src_img, $height - $y - 1, $x, $x, $y, 1, 1) )870 return false;871 }872 }873 } elseif ( 270 == $angle ) {874 for( $x = 0; $x < $width; $x++ ) {875 for( $y = 0; $y < $height; $y++ ) {876 if ( !imagecopy($dest_img, $src_img, $y, $width - $x - 1, $x, $y, 1, 1) )877 return false;878 }879 }880 } else {881 return false;882 }883 884 return $dest_img;885 }886 887 /**888 * Flip an image.889 *890 * @since 2.9.0891 *892 * @param resource $src_img GD image handle of source image.893 * @param $mode string 'horizontal', 'vertical' or 'both'.894 * @param $keep_transparency bool preserve transparency.895 * @return mixed GD image handle of flipped image or false on error.896 */897 function flip_image( $src_img, $mode, $keep_transparency = false ) {898 899 $width = $src_width = imagesx( $src_img );900 $height = $src_height = imagesy( $src_img );901 $src_x = $src_y = 0;902 903 switch ( $mode ) {904 case 'vertical':905 $src_y = $height -1;906 $src_height = -$height;907 break;908 case 'horizontal':909 $src_x = $width -1;910 $src_width = -$width;911 break;912 case 'both':913 if ( function_exists('imagerotate') )914 return imagerotate($src_img, 180, 0);915 916 $src_x = $width -1;917 $src_y = $height -1;918 $src_width = -$width;919 $src_height = -$height;920 break;921 default:922 return false;923 }924 925 $dest_img = imagecreatetruecolor( $width, $height );926 if ( $keep_transparency ) {927 imagealphablending($dest_img, false);928 imagesavealpha($dest_img, true);929 }930 931 if ( imagecopyresampled( $dest_img, $src_img, 0, 0, $src_x, $src_y , $width, $height, $src_width, $src_height ) )932 return $dest_img;933 934 return false;935 }
Note: See TracChangeset
for help on using the changeset viewer.