<!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 { overflow: auto; background: #ffc; border: 1px #fc0 solid; padding: 6px; }
#msg ul, pre { 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>[12342] trunk: Introducing set_post_image_size(w, h, crop)
so themes can register their special size/crop for canonical post images.</title>
</head>
<body>
<div id="msg">
<dl>
<dt>Revision</dt> <dd><a href="http://trac.wordpress.org/changeset/12342">12342</a></dd>
<dt>Author</dt> <dd>markjaquith</dd>
<dt>Date</dt> <dd>2009-12-08 21:08:19 +0000 (Tue, 08 Dec 2009)</dd>
</dl>
<h3>Log Message</h3>
<pre>Introducing set_post_image_size(w, h, crop) so themes can register their special size/crop for canonical post images. WP will create this size/crop upon upload, so your canonical post images fit your space exactly!</pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkwpadminincludesimagephp">trunk/wp-admin/includes/image.php</a></li>
<li><a href="#trunkwpadminincludespostphp">trunk/wp-admin/includes/post.php</a></li>
<li><a href="#trunkwpincludesmediaphp">trunk/wp-includes/media.php</a></li>
<li><a href="#trunkwpincludespostimagetemplatephp">trunk/wp-includes/post-image-template.php</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkwpadminincludesimagephp"></a>
<div class="modfile"><h4>Modified: trunk/wp-admin/includes/image.php (12341 => 12342)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-admin/includes/image.php        2009-12-08 19:59:34 UTC (rev 12341)
+++ trunk/wp-admin/includes/image.php        2009-12-08 21:08:19 UTC (rev 12342)
</span><span class="lines">@@ -98,10 +98,33 @@
</span><span class="cx">                 $metadata['file'] = _wp_relative_upload_path($file);
</span><span class="cx">
</span><span class="cx">                 // make thumbnails and other intermediate sizes
</span><del>-                $sizes = apply_filters( 'intermediate_image_sizes', array('thumbnail', 'medium', 'large') );
</del><ins>+                global $_wp_additional_image_sizes;
+                $temp_sizes = array('thumbnail', 'medium', 'large'); // Standard sizes
+                if ( isset( $_wp_additional_image_sizes ) && count( $_wp_additional_image_sizes ) )
+                        $temp_sizes = array_merge( $temp_sizes, array_keys( $_wp_additional_image_sizes ) );
</ins><span class="cx">
</span><del>-                foreach ($sizes as $size) {
-                        $resized = image_make_intermediate_size( $file, get_option("{$size}_size_w"), get_option("{$size}_size_h"), get_option("{$size}_crop") );
</del><ins>+                $temp_sizes = apply_filters( 'intermediate_image_sizes', $temp_sizes );
+
+                foreach ( $temp_sizes as $s ) {
+                        $sizes[$s] = array( 'width' => '', 'height' => '', 'crop' => FALSE );
+                        if ( isset( $_wp_additional_image_sizes[$s]['width'] ) )
+                                $sizes[$s]['width'] = intval( $_wp_additional_image_sizes[$s]['width'] ); // For theme-added sizes
+                        else
+                                $sizes[$s]['width'] = get_option( "{$size}_size_w" ); // For default sizes set in options
+                        if ( isset( $_wp_additional_image_sizes[$s]['height'] ) )
+                                $sizes[$s]['height'] = intval( $_wp_additional_image_sizes[$s]['height'] ); // For theme-added sizes
+                        else
+                                $sizes[$s]['height'] = get_option( "{$size}_size_h" ); // For default sizes set in options
+                        if ( isset( $_wp_additional_image_sizes[$s]['crop'] ) )
+                                $sizes[$s]['crop'] = intval( $_wp_additional_image_sizes[$s]['crop'] ); // For theme-added sizes
+                        else
+                                $sizes[$s]['crop'] = get_option( "{$size}_crop" ); // For default sizes set in options
+                }
+
+                $sizes = apply_filters( 'intermediate_image_sizes_advanced', $sizes );
+
+                foreach ($sizes as $size => $size_data ) {
+                        $resized = image_make_intermediate_size( $file, $size_data['width'], $size_data['height'], $size_data['crop'] );
</ins><span class="cx">                         if ( $resized )
</span><span class="cx">                                 $metadata['sizes'][$size] = $resized;
</span><span class="cx">                 }
</span></span></pre></div>
<a id="trunkwpadminincludespostphp"></a>
<div class="modfile"><h4>Modified: trunk/wp-admin/includes/post.php (12341 => 12342)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-admin/includes/post.php        2009-12-08 19:59:34 UTC (rev 12341)
+++ trunk/wp-admin/includes/post.php        2009-12-08 21:08:19 UTC (rev 12342)
</span><span class="lines">@@ -1070,14 +1070,21 @@
</span><span class="cx"> * @return string html
</span><span class="cx"> */
</span><span class="cx"> function _wp_post_thumbnail_html( $thumbnail_id = NULL ) {
</span><ins>+        global $content_width, $_wp_additional_image_sizes;
</ins><span class="cx">         $content = '<p class="hide-if-no-js"><a href="#" id="set-post-thumbnail" onclick="jQuery(\'#add_image\').click();return false;">' . esc_html__( 'Set thumbnail' ) . '</a></p>';
</span><span class="cx">
</span><span class="cx">         if ( $thumbnail_id && get_post( $thumbnail_id ) ) {
</span><del>-                $thumbnail_html = wp_get_attachment_image($thumbnail_id, array( 266, 266 ) );
</del><ins>+                $old_content_width = $content_width;
+                $content_width = 266;
+                if ( !isset( $_wp_additional_image_sizes['post-image'] ) )
+                        $thumbnail_html = wp_get_attachment_image( $thumbnail_id, array( $content_width, $content_width ) );
+                else
+                        $thumbnail_html = wp_get_attachment_image( $thumbnail_id, 'post-image' );
</ins><span class="cx">                 if ( !empty( $thumbnail_html ) ) {
</span><span class="cx">                         $content = '<a href="#" id="set-post-thumbnail" onclick="jQuery(\'#add_image\').click();return false;">' . $thumbnail_html . '</a>';
</span><span class="cx">                         $content .= '<p class="hide-if-no-js"><a href="#" id="remove-post-thumbnail" onclick="WPRemoveThumbnail();return false;">' . esc_html__( 'Remove thumbnail' ) . '</a></p>';
</span><span class="cx">                 }
</span><ins>+                $content_width = $old_content_width;
</ins><span class="cx">         }
</span><span class="cx">
</span><span class="cx">         return apply_filters( 'admin_post_thumbnail_html', $content );
</span></span></pre></div>
<a id="trunkwpincludesmediaphp"></a>
<div class="modfile"><h4>Modified: trunk/wp-includes/media.php (12341 => 12342)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-includes/media.php        2009-12-08 19:59:34 UTC (rev 12341)
+++ trunk/wp-includes/media.php        2009-12-08 21:08:19 UTC (rev 12342)
</span><span class="lines">@@ -32,7 +32,7 @@
</span><span class="cx"> * @return array Width and height of what the result image should resize to.
</span><span class="cx"> */
</span><span class="cx"> function image_constrain_size_for_editor($width, $height, $size = 'medium') {
</span><del>-        global $content_width;
</del><ins>+        global $content_width, $_wp_additional_image_sizes;
</ins><span class="cx">
</span><span class="cx">         if ( is_array($size) ) {
</span><span class="cx">                 $max_width = $size[0];
</span><span class="lines">@@ -61,6 +61,11 @@
</span><span class="cx">                 $max_height = intval(get_option('large_size_h'));
</span><span class="cx">                 if ( intval($content_width) > 0 )
</span><span class="cx">                         $max_width = min( intval($content_width), $max_width );
</span><ins>+        } elseif ( isset( $_wp_additional_image_sizes ) && count( $_wp_additional_image_sizes ) && in_array( $size, array_keys( $_wp_additional_image_sizes ) ) ) {
+                $max_width = intval( $_wp_additional_image_sizes[$size]['width'] );
+                $max_height = intval( $_wp_additional_image_sizes[$size]['height'] );
+                if ( intval($content_width) > 0 )
+                        $max_width = min( intval($content_width), $max_width );
</ins><span class="cx">         }
</span><span class="cx">         // $size == 'full' has no constraint
</span><span class="cx">         else {
</span><span class="lines">@@ -170,6 +175,21 @@
</span><span class="cx"> }
</span><span class="cx">
</span><span class="cx"> /**
</span><ins>+ * Registers a new image size
+ */
+function add_image_size( $name, $width = 0, $height = 0, $crop = FALSE ) {
+        global $_wp_additional_image_sizes;
+        $_wp_additional_image_sizes[$name] = array( 'width' => absint( $width ), 'height' => absint( $height ), 'crop' => !!$crop );
+}
+
+/**
+ * Registers an image size for the post image
+ */
+function set_post_image_size( $width = 0, $height = 0, $crop = FALSE ) {
+        add_image_size( 'post-image', $width, $height, $crop );
+}
+
+/**
</ins><span class="cx"> * An <img src /> tag for an image attachment, scaling it down if requested.
</span><span class="cx"> *
</span><span class="cx"> * The filter 'get_image_tag_class' allows for changing the class name for the
</span></span></pre></div>
<a id="trunkwpincludespostimagetemplatephp"></a>
<div class="modfile"><h4>Modified: trunk/wp-includes/post-image-template.php (12341 => 12342)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-includes/post-image-template.php        2009-12-08 19:59:34 UTC (rev 12341)
+++ trunk/wp-includes/post-image-template.php        2009-12-08 21:08:19 UTC (rev 12342)
</span><span class="lines">@@ -42,10 +42,10 @@
</span><span class="cx"> *
</span><span class="cx"> * @since 2.9.0
</span><span class="cx"> *
</span><del>- * @param int $size Optional. Image size. Defaults to 'thumbnail'.
</del><ins>+ * @param int $size Optional. Image size. Defaults to 'post-image', which theme sets using set_post_image_size( $width, $height, $crop_flag );.
</ins><span class="cx"> * @param string|array $attr Optional. Query string or array of attributes.
</span><span class="cx"> */
</span><del>-function the_post_image( $size = 'thumbnail', $attr = '' ) {
</del><ins>+function the_post_image( $size = 'post-image', $attr = '' ) {
</ins><span class="cx">         echo get_the_post_image( NULL, $size, $attr );
</span><span class="cx"> }
</span><span class="cx">
</span><span class="lines">@@ -58,7 +58,7 @@
</span><span class="cx"> * @param string $size Optional. Image size. Defaults to 'thumbnail'.
</span><span class="cx"> * @param string|array $attr Optional. Query string or array of attributes.
</span><span class="cx"> */
</span><del>-function get_the_post_image( $post_id = NULL, $size = 'thumbnail', $attr = '' ) {
</del><ins>+function get_the_post_image( $post_id = NULL, $size = 'post-image', $attr = '' ) {
</ins><span class="cx">         global $id;
</span><span class="cx">         $post_id = ( NULL === $post_id ) ? $id : $post_id;
</span><span class="cx">         $post_image_id = get_post_image_id( $post_id );
</span></span></pre>
</div>
</div>
</body>
</html>