<!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" />
<title>[36891] trunk/src/wp-includes/class-wp-image-editor-imagick.php: Media: Progressive enhancement for Imagick; add profiles to whitelist.</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { 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 { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#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>
<div id="msg">
<dl class="meta" style="font-size: 105%">
<dt style="float: left; width: 6em; font-weight: bold">Revision</dt> <dd><a style="font-weight: bold" href="https://core.trac.wordpress.org/changeset/36891">36891</a><script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","description":"Review this Commit","action":{"@type":"ViewAction","url":"https://core.trac.wordpress.org/changeset/36891","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>mikeschroder</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2016-03-09 04:43:40 +0000 (Wed, 09 Mar 2016)</dd>
</dl>

<pre style='padding-left: 1em; margin: 2em 0; border-left: 2px solid #ccc; line-height: 1.25; font-size: 105%; font-family: sans-serif'>Media: Progressive enhancement for Imagick; add profiles to whitelist.

- Progressive enhancement for optional compression improvements and stripping meta.
- Whitelist IPTC and XMP profiles to maintain Copyright and Rights Usage Terms.
- Whitelist EXIF profile to maintain orientation information. If handled on upload in the future, it can be stripped as well.

Fixes <a href="https://core.trac.wordpress.org/ticket/33642">#33642</a>. See <a href="https://core.trac.wordpress.org/ticket/28634">#28634</a>.
Props joemcgill, juliobox, ahockley, markoheijnen, adamsilverstein, wonderboymusic, mikeschroder.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpincludesclasswpimageeditorimagickphp">trunk/src/wp-includes/class-wp-image-editor-imagick.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcwpincludesclasswpimageeditorimagickphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/class-wp-image-editor-imagick.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/class-wp-image-editor-imagick.php   2016-03-09 04:36:51 UTC (rev 36890)
+++ trunk/src/wp-includes/class-wp-image-editor-imagick.php     2016-03-09 04:43:40 UTC (rev 36891)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -62,25 +62,21 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        'writeimage',
</span><span class="cx" style="display: block; padding: 0 10px">                        'getimageblob',
</span><span class="cx" style="display: block; padding: 0 10px">                        'getimagegeometry',
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        'getimagedepth',
</del><span class="cx" style="display: block; padding: 0 10px">                         'getimageformat',
</span><span class="cx" style="display: block; padding: 0 10px">                        'setimageformat',
</span><span class="cx" style="display: block; padding: 0 10px">                        'setimagecompression',
</span><span class="cx" style="display: block; padding: 0 10px">                        'setimagecompressionquality',
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        'setimagedepth',
</del><span class="cx" style="display: block; padding: 0 10px">                         'setimagepage',
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        'setimageproperty',
-                       'setinterlacescheme',
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 'setoption',
</ins><span class="cx" style="display: block; padding: 0 10px">                         'scaleimage',
</span><span class="cx" style="display: block; padding: 0 10px">                        'cropimage',
</span><span class="cx" style="display: block; padding: 0 10px">                        'rotateimage',
</span><span class="cx" style="display: block; padding: 0 10px">                        'flipimage',
</span><span class="cx" style="display: block; padding: 0 10px">                        'flopimage',
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        'unsharpmaskimage',
</del><span class="cx" style="display: block; padding: 0 10px">                 );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                // Now, test for deep requirements within Imagick.
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                if ( ! ( defined( 'imagick::COMPRESSION_JPEG' ) && defined( 'imagick::FILTER_TRIANGLE' ) ) )
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         if ( ! defined( 'imagick::COMPRESSION_JPEG' ) )
</ins><span class="cx" style="display: block; padding: 0 10px">                         return false;
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                if ( array_diff( $required_methods, get_class_methods( 'Imagick' ) ) )
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -304,7 +300,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">                if ( in_array( $filter_name, $allowed_filters ) && defined( 'Imagick::' . $filter_name ) ) {
</span><span class="cx" style="display: block; padding: 0 10px">                        $filter = constant( 'Imagick::' . $filter_name );
</span><span class="cx" style="display: block; padding: 0 10px">                } else {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        $filter = Imagick::FILTER_TRIANGLE;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 $filter = defined( 'Imagick::FILTER_TRIANGLE' ) ? Imagick::FILTER_TRIANGLE : false;
</ins><span class="cx" style="display: block; padding: 0 10px">                 }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                /**
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -317,15 +313,8 @@
</span><span class="cx" style="display: block; padding: 0 10px">                 *
</span><span class="cx" style="display: block; padding: 0 10px">                 * @param bool $strip_meta Whether to strip image metadata during resizing. Default true.
</span><span class="cx" style="display: block; padding: 0 10px">                 */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $strip_meta = apply_filters( 'image_strip_meta', $strip_meta );
-
-               // Strip image meta.
-               if ( $strip_meta ) {
-                       $strip_result = $this->strip_meta();
-
-                       if ( is_wp_error( $strip_result ) ) {
-                               return $strip_result;
-                       }
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         if ( apply_filters( 'image_strip_meta', $strip_meta ) ) {
+                       $this->strip_meta(); // Fail silently if not supported.
</ins><span class="cx" style="display: block; padding: 0 10px">                 }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                try {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -334,20 +323,34 @@
</span><span class="cx" style="display: block; padding: 0 10px">                         * whenever the output size is less that 1/3 of the original image size (1/3^2 ~= .111),
</span><span class="cx" style="display: block; padding: 0 10px">                         * unless we would be resampling to a scale smaller than 128x128.
</span><span class="cx" style="display: block; padding: 0 10px">                         */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        $resize_ratio = ( $dst_w / $this->size['width'] ) * ( $dst_h / $this->size['height'] );
-                       $sample_factor = 5;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 if ( is_callable( array( $this->image, 'sampleImage' ) ) ) {
+                               $resize_ratio = ( $dst_w / $this->size['width'] ) * ( $dst_h / $this->size['height'] );
+                               $sample_factor = 5;
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        if ( $resize_ratio < .111 && ( $dst_w * $sample_factor > 128 && $dst_h * $sample_factor > 128 ) ) {
-                               $this->image->sampleImage( $dst_w * $sample_factor, $dst_h * $sample_factor );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                         if ( $resize_ratio < .111 && ( $dst_w * $sample_factor > 128 && $dst_h * $sample_factor > 128 ) ) {
+                                       $this->image->sampleImage( $dst_w * $sample_factor, $dst_h * $sample_factor );
+                               }
</ins><span class="cx" style="display: block; padding: 0 10px">                         }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        // Resize to the final output size.
-                       $this->image->setOption( 'filter:support', '2.0' );
-                       $this->image->resizeImage( $dst_w, $dst_h, $filter, 1 );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 /*
+                        * Use resizeImage() when it's available and a valid filter value is set.
+                        * Otherwise, fall back to the scaleImage() method for resizing, which
+                        * results in better image quality over resizeImage() with default filter
+                        * settings and retains backwards compatibility with pre 4.5 functionality.
+                        */
+                       if ( is_callable( array( $this->image, 'resizeImage' ) ) && $filter ) {
+                               $this->image->setOption( 'filter:support', '2.0' );
+                               $this->image->resizeImage( $dst_w, $dst_h, $filter, 1 );
+                       } else {
+                               $this->image->scaleImage( $dst_w, $dst_h );
+                       }
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                        // Set appropriate quality settings after resizing.
</span><span class="cx" style="display: block; padding: 0 10px">                        if ( 'image/jpeg' == $this->mime_type ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                $this->image->unsharpMaskImage( 0.25, 0.25, 8, 0.065 );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                         if ( is_callable( array( $this->image, 'unsharpMaskImage' ) ) ) {
+                                       $this->image->unsharpMaskImage( 0.25, 0.25, 8, 0.065 );
+                               }
+
</ins><span class="cx" style="display: block; padding: 0 10px">                                 $this->image->setOption( 'jpeg:fancy-upsampling', 'off' );
</span><span class="cx" style="display: block; padding: 0 10px">                        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -358,22 +361,32 @@
</span><span class="cx" style="display: block; padding: 0 10px">                                $this->image->setOption( 'png:exclude-chunk', 'all' );
</span><span class="cx" style="display: block; padding: 0 10px">                        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        /**
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 /*
</ins><span class="cx" style="display: block; padding: 0 10px">                          * If alpha channel is not defined, set it opaque.
</span><span class="cx" style="display: block; padding: 0 10px">                         *
</span><span class="cx" style="display: block; padding: 0 10px">                         * Note that Imagick::getImageAlphaChannel() is only available if Imagick
</span><span class="cx" style="display: block; padding: 0 10px">                         * has been compiled against ImageMagick version 6.4.0 or newer.
</span><span class="cx" style="display: block; padding: 0 10px">                         */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        if ( method_exists( $this->image, 'getImageAlphaChannel') && $this->image->getImageAlphaChannel() === Imagick::ALPHACHANNEL_UNDEFINED ) {
-                               $this->image->setImageAlphaChannel( Imagick::ALPHACHANNEL_OPAQUE );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 if ( is_callable( array( $this->image, 'getImageAlphaChannel' ) )
+                               && is_callable( array( $this->image, 'setImageAlphaChannel' ) )
+                               && defined( Imagick::ALPHACHANNEL_UNDEFINED )
+                               && defined( Imagick::ALPHACHANNEL_OPAQUE )
+                       ) {
+                               if ( $this->image->getImageAlphaChannel() === Imagick::ALPHACHANNEL_UNDEFINED ) {
+                                       $this->image->setImageAlphaChannel( Imagick::ALPHACHANNEL_OPAQUE );
+                               }
</ins><span class="cx" style="display: block; padding: 0 10px">                         }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        // Limit the  bit depth of resized images to 8 bits per channel.
-                       if ( 8 < $this->image->getImageDepth() ) {
-                               $this->image->setImageDepth( 8 );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 // Limit the bit depth of resized images to 8 bits per channel.
+                       if ( is_callable( array( $this->image, 'getImageDepth' ) ) && is_callable( array( $this->image, 'setImageDepth' ) ) ) {
+                               if ( 8 < $this->image->getImageDepth() ) {
+                                       $this->image->setImageDepth( 8 );
+                               }
</ins><span class="cx" style="display: block; padding: 0 10px">                         }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        $this->image->setInterlaceScheme( Imagick::INTERLACE_NO );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 if ( is_callable( array( $this->image, 'setInterlaceScheme' ) ) && defined( 'Imagick::INTERLACE_NO' ) ) {
+                               $this->image->setInterlaceScheme( Imagick::INTERLACE_NO );
+                       }
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><span class="cx" style="display: block; padding: 0 10px">                catch ( Exception $e ) {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -662,36 +675,40 @@
</span><span class="cx" style="display: block; padding: 0 10px">         * @return true|WP_Error True if stripping metadata was successful. WP_Error object on error.
</span><span class="cx" style="display: block; padding: 0 10px">         */
</span><span class="cx" style="display: block; padding: 0 10px">        protected function strip_meta() {
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+               if ( ! is_callable( array( $this->image, 'getImageProfiles' ) ) ) {
+                       return new WP_Error( 'image_strip_meta_error', __('Imagick::getImageProfiles() is required to strip image meta.') );
+               }
+
+               if ( ! is_callable( array( $this->image, 'removeImageProfile' ) ) ) {
+                       return new WP_Error( 'image_strip_meta_error', __('Imagick::removeImageProfile() is required to strip image meta.') );
+               }
+
+               /*
+                * Protect a few profiles from being stripped for the following reasons:
+                *
+                * - icc:  Color profile information
+                * - icm:  Color profile information
+                * - iptc: Copyright data
+                * - exif: Orientation data
+                * - xmp:  Rights usage data
+                */
+               $protected_profiles = array(
+                       'icc',
+                       'icm',
+                       'iptc',
+                       'exif',
+                       'xmp',
+               );
+
</ins><span class="cx" style="display: block; padding: 0 10px">                 try {
</span><span class="cx" style="display: block; padding: 0 10px">                        // Strip profiles.
</span><span class="cx" style="display: block; padding: 0 10px">                        foreach ( $this->image->getImageProfiles( '*', true ) as $key => $value ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                if ( $key != 'icc' && $key != 'icm' ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                         if ( ! in_array( $key, $protected_profiles ) ) {
</ins><span class="cx" style="display: block; padding: 0 10px">                                         $this->image->removeImageProfile( $key );
</span><span class="cx" style="display: block; padding: 0 10px">                                }
</span><span class="cx" style="display: block; padding: 0 10px">                        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        // Strip image properties.
-                       if ( method_exists( $this->image, 'deleteImageProperty' ) ) {
-                               $this->image->deleteImageProperty( 'comment' );
-                               $this->image->deleteImageProperty( 'Thumb::URI' );
-                               $this->image->deleteImageProperty( 'Thumb::MTime' );
-                               $this->image->deleteImageProperty( 'Thumb::Size' );
-                               $this->image->deleteImageProperty( 'Thumb::Mimetype' );
-                               $this->image->deleteImageProperty( 'software' );
-                               $this->image->deleteImageProperty( 'Thumb::Image::Width' );
-                               $this->image->deleteImageProperty( 'Thumb::Image::Height' );
-                               $this->image->deleteImageProperty( 'Thumb::Document::Pages' );
-                       } else {
-                               $this->image->setImageProperty( 'comment', '' );
-                               $this->image->setImageProperty( 'Thumb::URI', '' );
-                               $this->image->setImageProperty( 'Thumb::MTime', '' );
-                               $this->image->setImageProperty( 'Thumb::Size', '' );
-                               $this->image->setImageProperty( 'Thumb::Mimetype', '' );
-                               $this->image->setImageProperty( 'software', '' );
-                               $this->image->setImageProperty( 'Thumb::Image::Width', '' );
-                               $this->image->setImageProperty( 'Thumb::Image::Height', '' );
-                               $this->image->setImageProperty( 'Thumb::Document::Pages', '' );
-                       }
</del><span class="cx" style="display: block; padding: 0 10px">                 } catch ( Exception $e ) {
</span><span class="cx" style="display: block; padding: 0 10px">                        return new WP_Error( 'image_strip_meta_error', $e->getMessage() );
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span></span></pre>
</div>
</div>

</body>
</html>