<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" /><style type="text/css"><!--
#msg dl { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre, #msg p { overflow: auto; background: #ffc; border: 1px #fc0 solid; padding: 6px; }
#msg ul { overflow: auto; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<title>[11746] trunk/wp-includes/media.php:
  Use exif rotation data when creating sub-sizes of uploaded jpeg images,
  see #7042</title>
</head>
<body>

<div id="msg">
<dl>
<dt>Revision</dt> <dd><a href="http://trac.wordpress.org/changeset/11746">11746</a></dd>
<dt>Author</dt> <dd>azaozz</dd>
<dt>Date</dt> <dd>2009-07-27 18:32:30 +0000 (Mon, 27 Jul 2009)</dd>
</dl>

<h3>Log Message</h3>
<pre>Use exif rotation data when creating sub-sizes of uploaded jpeg images, see <a href="http://trac.wordpress.org/ticket/7042">#7042</a></pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkwpincludesmediaphp">trunk/wp-includes/media.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkwpincludesmediaphp"></a>
<div class="modfile"><h4>Modified: trunk/wp-includes/media.php (11745 => 11746)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-includes/media.php        2009-07-26 17:16:53 UTC (rev 11745)
+++ trunk/wp-includes/media.php        2009-07-27 18:32:30 UTC (rev 11746)
</span><span class="lines">@@ -330,15 +330,19 @@
</span><span class="cx">  * @param int $jpeg_quality Optional, default is 90. Image quality percentage.
</span><span class="cx">  * @return mixed WP_Error on failure. String with new destination path. Array of dimensions from {@link image_resize_dimensions()}
</span><span class="cx">  */
</span><del>-function image_resize( $file, $max_w, $max_h, $crop=false, $suffix=null, $dest_path=null, $jpeg_quality=90) {
</del><ins>+function image_resize( $file, $max_w, $max_h, $crop = false, $suffix = null, $dest_path = null, $jpeg_quality = 90 ) {
</ins><span class="cx"> 
</span><span class="cx">         $image = wp_load_image( $file );
</span><span class="cx">         if ( !is_resource( $image ) )
</span><span class="cx">                 return new WP_Error('error_loading_image', $image);
</span><span class="cx"> 
</span><del>-        list($orig_w, $orig_h, $orig_type) = getimagesize( $file );
</del><ins>+        $size = @getimagesize( $file );
+        if ( !$size )
+                return new WP_Error('invalid_image', __('Could not read image size'), $file);
+        list($orig_w, $orig_h, $orig_type) = $size;
+
</ins><span class="cx">         $dims = image_resize_dimensions($orig_w, $orig_h, $max_w, $max_h, $crop);
</span><del>-        if (!$dims)
</del><ins>+        if ( !$dims )
</ins><span class="cx">                 return $dims;
</span><span class="cx">         list($dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h) = $dims;
</span><span class="cx"> 
</span><span class="lines">@@ -371,18 +375,35 @@
</span><span class="cx">                 $dir = $_dest_path;
</span><span class="cx">         $destfilename = &quot;{$dir}/{$name}-{$suffix}.{$ext}&quot;;
</span><span class="cx"> 
</span><del>-        if ( $orig_type == IMAGETYPE_GIF ) {
-                if (!imagegif( $newimage, $destfilename ) )
</del><ins>+        if ( IMAGETYPE_GIF == $orig_type ) {
+                if ( !imagegif( $newimage, $destfilename ) )
</ins><span class="cx">                         return new WP_Error('resize_path_invalid', __( 'Resize path invalid' ));
</span><del>-        }
-        elseif ( $orig_type == IMAGETYPE_PNG ) {
</del><ins>+        } elseif ( IMAGETYPE_PNG == $orig_type ) {
</ins><span class="cx">                 if (!imagepng( $newimage, $destfilename ) )
</span><span class="cx">                         return new WP_Error('resize_path_invalid', __( 'Resize path invalid' ));
</span><del>-        }
-        else {
</del><ins>+        } else {
+                $rotated = false;
+                if ( IMAGETYPE_JPEG == $orig_type ) {
+                        // rotate if EXIF 'Orientation' is set
+                        $exif = exif_read_data($file, null, true);
+                        if ( $exif &amp;&amp; isset($exif['IFD0']) &amp;&amp; is_array($exif['IFD0']) &amp;&amp; isset($exif['IFD0']['Orientation']) ) {
+                                if ( 6 == $exif['IFD0']['Orientation'] )
+                                        $rotated = rotate_image($newimage, 90);
+                                elseif ( 8 == $exif['IFD0']['Orientation'] )
+                                        $rotated = rotate_image($newimage, 270);
+                        }
+                }
+
</ins><span class="cx">                 // all other formats are converted to jpg
</span><span class="cx">                 $destfilename = &quot;{$dir}/{$name}-{$suffix}.jpg&quot;;
</span><del>-                if (!imagejpeg( $newimage, $destfilename, apply_filters( 'jpeg_quality', $jpeg_quality, 'image_resize' ) ) )
</del><ins>+                if ( $rotated ) {
+                        $return = imagejpeg( $rotated, $destfilename, apply_filters( 'jpeg_quality', $jpeg_quality, 'image_resize' ) );
+                        imagedestroy($rotated);
+                } else {
+                        $return = imagejpeg( $newimage, $destfilename, apply_filters( 'jpeg_quality', $jpeg_quality, 'image_resize' ) );
+                }
+
+                if ( !$return )
</ins><span class="cx">                         return new WP_Error('resize_path_invalid', __( 'Resize path invalid' ));
</span><span class="cx">         }
</span><span class="cx"> 
</span><span class="lines">@@ -797,4 +818,100 @@
</span><span class="cx">         return array_unique($taxonomies);
</span><span class="cx"> }
</span><span class="cx"> 
</span><del>-?&gt;
</del><ins>+/**
+ * Rotate an image.
+ *
+ * @since 2.9.0
+ *
+ * @param resource $src_img GD image handle of source image.
+ * @param $angle int clockwise angle of rotation, only 90, 180 and 270 are supported.
+ * @param $keep_transparency bool preserve transparency.
+ * @return mixed GD image handle of rotated image or false on error.
+ */
+function rotate_image( $src_img, $angle, $keep_transparency = false ) {
+
+        if ( function_exists('imagerotate') )
+                return imagerotate($src_img, 360 - $angle, 0); // imagerotate() rotates CCW
+
+        if ( 180 == $angle )
+                return flip_image( $src_img, 'both', $keep_transparency );
+
+        $width = imagesx( $src_img );
+    $height = imagesy( $src_img );
+
+    $dest_img = imagecreatetruecolor( $height, $width );
+    if ( $keep_transparency ) {
+                imagealphablending($dest_img, false);
+                imagesavealpha($dest_img, true);
+        }
+
+        if ( 90 == $angle ) {
+                for( $x = 0; $x &lt; $width; $x++ ) {
+                        for( $y = 0; $y &lt; $height; $y++ ) {
+                                if ( !imagecopy($dest_img, $src_img, $height - $y - 1, $x, $x, $y, 1, 1) )
+                                        return false;
+                        }
+                }
+        } elseif ( 270 == $angle ) {
+                for( $x = 0; $x &lt; $width; $x++ ) {
+                        for( $y = 0; $y &lt; $height; $y++ ) {
+                                if ( !imagecopy($dest_img, $src_img, $y, $width - $x - 1, $x, $y, 1, 1) )
+                                        return false;
+                        }
+                }
+        } else {
+                return false;
+        }
+
+        return $dest_img;
+}
+
+/**
+ * Flip an image.
+ *
+ * @since 2.9.0
+ *
+ * @param resource $src_img GD image handle of source image.
+ * @param $mode string 'horizontal', 'vertical' or 'both'.
+ * @param $keep_transparency bool preserve transparency.
+ * @return mixed GD image handle of flipped image or false on error.
+ */
+function flip_image( $src_img, $mode, $keep_transparency = false ) {
+
+        $width = $src_width = imagesx( $src_img );
+        $height = $src_height = imagesy( $src_img );
+        $src_x = $src_y = 0;
+
+        switch ( $mode ) {
+                case 'vertical':
+                        $src_y = $height -1;
+                        $src_height = -$height;
+                        break;
+                case 'horizontal':
+                        $src_x = $width -1;
+                        $src_width = -$width;
+                        break;
+                case 'both':
+                        if ( function_exists('imagerotate') )
+                                return imagerotate($src_img, 180, 0);
+
+                        $src_x = $width -1;
+                        $src_y = $height -1;
+                        $src_width = -$width;
+                        $src_height = -$height;
+                        break;
+                default:
+                        return false;
+        }
+
+        $dest_img = imagecreatetruecolor( $width, $height );
+        if ( $keep_transparency ) {
+                imagealphablending($dest_img, false);
+                imagesavealpha($dest_img, true);
+        }
+
+        if ( imagecopyresampled( $dest_img, $src_img, 0, 0, $src_x, $src_y , $width, $height, $src_width, $src_height ) )
+                return $dest_img;
+
+        return false;
+}
</ins></span></pre>
</div>
</div>

</body>
</html>