<!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>[36700] trunk/src/wp-includes/class-wp-image-editor-imagick.php: Media: Optimize Imagick settings for quality and filesize</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/36700">36700</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/36700","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-02-24 22:50:10 +0000 (Wed, 24 Feb 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: Optimize Imagick settings for quality and filesize
- Resamples and sharpens larger images before resize.
- Uses imagick::FILTER_TRIANGLE for a smoother resize.
- Introduces `WP_Image_Editor_Imagick::thumbnail_image()` protected method to efficiently resize images. Similar to the functionality of Imagick's `thumbnailImage()`.
- Introduces `WP_Image_Editor_Imagick::strip_meta()` protected method and `image_strip_meta` filter that, by default, strip image profiles to reduce file size, while leaving color profiles intact.
See: <a href="https://core.trac.wordpress.org/ticket/33642">#33642</a>, <a href="https://core.trac.wordpress.org/ticket/30402">#30402</a>, <a href="https://core.trac.wordpress.org/ticket/28634">#28634</a>.
Props: joemcgill, dnewton, 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-02-24 22:11:50 UTC (rev 36699)
+++ trunk/src/wp-includes/class-wp-image-editor-imagick.php 2016-02-24 22:50:10 UTC (rev 36700)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -62,20 +62,25 @@
</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><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ 'getimagedepth',
</ins><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><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ 'setimagedepth',
</ins><span class="cx" style="display: block; padding: 0 10px"> 'setimagepage',
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ 'setimageproperty',
+ 'setinterlacescheme',
</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><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ 'unsharpmaskimage',
</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"> // 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' ) )
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( ! ( defined( 'imagick::COMPRESSION_JPEG' ) && defined( 'imagick::FILTER_TRIANGLE' ) ) )
</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">@@ -249,18 +254,131 @@
</span><span class="cx" style="display: block; padding: 0 10px"> return $this->crop( $src_x, $src_y, $src_w, $src_h, $dst_w, $dst_h );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ // Execute the resize
+ $thumb_result = $this->thumbnail_image( $dst_w, $dst_h );
+ if ( is_wp_error( $thumb_result ) ) {
+ return $thumb_result;
+ }
+
+ return $this->update_size( $dst_w, $dst_h );
+ }
+
+ /**
+ * Efficiently resize the current image
+ *
+ * This is a WordPress specific implementation of Imagick::thumbnailImage(),
+ * which resizes an image to given dimensions and removes any associated profiles.
+ *
+ * @since 4.5.0
+ * @access protected
+ *
+ * @param int $dst_w The destination width.
+ * @param int $dst_h The destination height.
+ * @param string $filter_name Optional. The Imagick filter to use when resizing. Default 'FILTER_TRIANGLE'.
+ * @param bool $strip_meta Optional. Strip all profiles, excluding color profiles, from the image. Default true.
+ * @return bool|WP_Error
+ */
+ protected function thumbnail_image( $dst_w, $dst_h, $filter_name = 'FILTER_TRIANGLE', $strip_meta = true ) {
+ $allowed_filters = array(
+ 'FILTER_POINT',
+ 'FILTER_BOX',
+ 'FILTER_TRIANGLE',
+ 'FILTER_HERMITE',
+ 'FILTER_HANNING',
+ 'FILTER_HAMMING',
+ 'FILTER_BLACKMAN',
+ 'FILTER_GAUSSIAN',
+ 'FILTER_QUADRATIC',
+ 'FILTER_CUBIC',
+ 'FILTER_CATROM',
+ 'FILTER_MITCHELL',
+ 'FILTER_LANCZOS',
+ 'FILTER_BESSEL',
+ 'FILTER_SINC',
+ );
+
+ /**
+ * Set the filter value if '$filter_name' name is in our whitelist and the related
+ * Imagick constant is defined or fall back to our default filter.
+ */
+ if ( in_array( $filter_name, $allowed_filters ) && defined( 'Imagick::' . $filter_name ) ) {
+ $filter = constant( 'Imagick::' . $filter_name );
+ } else {
+ $filter = Imagick::FILTER_TRIANGLE;
+ }
+
+ /**
+ * Filter to override stripping metadata from images when they're resized.
+ *
+ * This filter only applies when resizing using the Imagick editor since GD
+ * always strips profiles by default.
+ *
+ * @since 4.5.0
+ *
+ * @param bool $strip_meta Whether to strip image metadata during resizing. Default true.
+ */
+ $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;
+ }
+ }
+
</ins><span class="cx" style="display: block; padding: 0 10px"> try {
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ /*
+ * To be more efficient, resample large images to 5x the destination size before resizing
+ * whenever the output size is less that 1/3 of the original image size (1/3^2 ~= .111),
+ * unless we would be resampling to a scale smaller than 128x128.
+ */
+ $resize_ratio = ( $dst_w / $this->size['width'] ) * ( $dst_h / $this->size['height'] );
+ $sample_factor = 5;
+
+ 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 );
+ }
+
+ // Resize to the final output size.
+ $this->image->setOption( 'filter:support', '2.0' );
+ $this->image->resizeImage( $dst_w, $dst_h, $filter, 1 );
+
+ // Set appropriate quality settings after resizing.
+ if ( 'image/jpeg' == $this->mime_type ) {
+ $this->image->unsharpMaskImage( 0.25, 0.25, 8, 0.065 );
+ $this->image->setOption( 'jpeg:fancy-upsampling', 'off' );
+ }
+
+ if ( 'image/png' === $this->mime_type ) {
+ $this->image->setOption( 'png:compression-filter', '5' );
+ $this->image->setOption( 'png:compression-level', '9' );
+ $this->image->setOption( 'png:compression-strategy', '1' );
+ $this->image->setOption( 'png:exclude-chunk', 'all' );
+ }
+
</ins><span class="cx" style="display: block; padding: 0 10px"> /**
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * @TODO: Thumbnail is more efficient, given a newer version of Imagemagick.
- * $this->image->thumbnailImage( $dst_w, $dst_h );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * If alpha channel is not defined, set it opaque.
+ *
+ * Note that Imagick::getImageAlphaChannel() is only available if Imagick
+ * has been compiled against ImageMagick version 6.4.0 or newer.
</ins><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->scaleImage( $dst_w, $dst_h );
</del><ins style="background-color: #dfd; 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 );
+ }
+
+ // Limit the bit depth of resized images to 8 bits per channel.
+ if ( 8 < $this->image->getImageDepth() ) {
+ $this->image->setImageDepth( 8 );
+ }
+
+ $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"> catch ( Exception $e ) {
</span><span class="cx" style="display: block; padding: 0 10px"> return new WP_Error( 'image_resize_error', $e->getMessage() );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-
- return $this->update_size( $dst_w, $dst_h );
</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"> /**
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -367,7 +485,11 @@
</span><span class="cx" style="display: block; padding: 0 10px"> if ( ! $dst_h )
</span><span class="cx" style="display: block; padding: 0 10px"> $dst_h = $src_h;
</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->scaleImage( $dst_w, $dst_h );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $thumb_result = $this->thumbnail_image( $dst_w, $dst_h );
+ if ( is_wp_error( $thumb_result ) ) {
+ return $thumb_result;
+ }
+
</ins><span class="cx" style="display: block; padding: 0 10px"> return $this->update_size();
</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">@@ -530,4 +652,49 @@
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> return true;
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+ /**
+ * Strip all image meta except color profiles from an image.
+ *
+ * @access protected
+ * @since 4.5.0
+ */
+ protected function strip_meta() {
+ try {
+ // Strip profiles.
+ foreach ( $this->image->getImageProfiles( '*', true ) as $key => $value ) {
+ if ( $key != 'icc' && $key != 'icm' ) {
+ $this->image->removeImageProfile( $key );
+ }
+ }
+
+ // 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', '' );
+ }
+ } catch ( Excpetion $e ) {
+ return new WP_Error( 'image_strip_meta_error', $e->getMessage() );
+ }
+
+ return true;
+ }
+
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span></span></pre>
</div>
</div>
</body>
</html>