WordPress.org

Make WordPress Core

Ticket #5933: image-size-options-r6937.patch

File image-size-options-r6937.patch, 12.6 KB (added by tellyworth, 6 years ago)
  • wp-includes/image.php

     
     1<?php 
     2 
     3// functions for image display 
     4 
     5// scale down the default size of an image so it's a better fit for the editor and theme 
     6function image_constrain_size_for_editor($width, $height, $size = 'medium') { 
     7         
     8        if ( $size == 'thumb' ) { 
     9                $max_width = intval(get_option('thumbnail_size_w')); 
     10                $max_height = intval(get_option('thumbnail_size_h')); 
     11                // last chance thumbnail size defaults 
     12                if ( !$max_width && !$max_height ) { 
     13                        $max_width = 128; 
     14                        $max_height = 96; 
     15                } 
     16        } 
     17        elseif ( $size == 'medium' ) { 
     18                $max_width = intval(get_option('medium_size_w')); 
     19                $max_height = intval(get_option('medium_size_h')); 
     20                // if no width is set, default to the theme content width if available 
     21                if ( !$max_width ) { 
     22                        // $content_width might be set in the current theme's functions.php 
     23                        if ( !empty($GLOBALS['content_width']) ) { 
     24                                $max_width = $GLOBALS['content_width']; 
     25                        } 
     26                        else 
     27                                $max_width = 500; 
     28                } 
     29        } 
     30        else { // $size == 'full' 
     31                $max_width = 0; 
     32                $max_height = 0; 
     33        } 
     34 
     35        list( $max_width, $max_height ) = apply_filters( 'editor_max_image_size', array( $max_width, $max_height ), $size ); 
     36         
     37        return wp_constrain_dimensions( $width, $height, $max_width, $max_height ); 
     38} 
     39 
     40// return a width/height string for use in an <img /> tag.  Empty values will be omitted. 
     41function image_hwstring($width, $height) { 
     42        $out = ''; 
     43        if ($width) 
     44                $out .= 'width="'.intval($width).'" '; 
     45        if ($height) 
     46                $out .= 'height="'.intval($height).'" '; 
     47        return $out; 
     48} 
     49 
     50// Scale an image to fit a particular size (such as 'thumb' or 'medium'), and return an image URL, height and width. 
     51// The URL might be the original image, or it might be a resized version. 
     52// returns an array($url, $width, $height) 
     53function image_downsize($id, $size = 'medium') { 
     54         
     55        $img_url = wp_get_attachment_url($id); 
     56        $meta = wp_get_attachment_metadata($id); 
     57        $width = $height = 0; 
     58         
     59        // plugins can use this to provide resize services 
     60        if ( $out = apply_filters('image_downsize', false, $id, $size) ) 
     61                return $out; 
     62         
     63        if ( $size == 'thumb' ) { 
     64                // thumbnail: use the thumb as the displayed image, and constrain based on its dimensions 
     65                $thumb_path = wp_get_attachment_thumb_file($id); 
     66                // the actual thumbnail size isn't stored so we'll have to calculate it 
     67                if ( $thumb_path && ($info = getimagesize($thumb_path)) ) { 
     68                        list( $width, $height ) = image_constrain_size_for_editor( $info[0], $info[1], $size ); 
     69                        $img_url = wp_get_attachment_thumb_url($id); 
     70                } 
     71                // this could be improved to provide a default thumbnail if one doesn't exist 
     72        } 
     73        elseif ( isset($meta['width'], $meta['height']) ) { 
     74                // any other type: use the real image and constrain it 
     75                list( $width, $height ) = image_constrain_size_for_editor( $meta['width'], $meta['height'], $size ); 
     76        } 
     77         
     78        return array( $img_url, $width, $height ); 
     79         
     80} 
     81 
     82// return an <img src /> tag for the given image attachment, scaling it down if requested 
     83function get_image_tag($id, $alt, $title, $align, $rel = false, $size='medium') { 
     84 
     85        list( $img_src, $width, $height ) = image_downsize($id, $size); 
     86        $hwstring = image_hwstring($width, $height); 
     87 
     88        $html = '<img src="'.attribute_escape($img_src).'" alt="'.attribute_escape($alt).'" title="'.attribute_escape($title).'"'.$hwstring.' class="align-'.attribute_escape($align).' size-'.attribute_escape($size).' attachment wp-att-'.attribute_escape($id).'" />'; 
     89 
     90        $html = apply_filters( 'image_send_to_editor', $html, $id, $alt, $title, $align, $url ); 
     91 
     92        return $html; 
     93} 
     94 
     95?> 
     96 No newline at end of file 
  • wp-settings.php

     
    263263require (ABSPATH . WPINC . '/update.php'); 
    264264require (ABSPATH . WPINC . '/canonical.php'); 
    265265require (ABSPATH . WPINC . '/shortcodes.php'); 
     266require (ABSPATH . WPINC . '/image.php'); 
    266267 
    267268if (strpos($_SERVER['PHP_SELF'], 'install.php') === false) { 
    268269        // Used to guarantee unique hash cookies 
  • wp-admin/includes/media.php

     
    131131        <input type="radio" name="image-align" id="image-align-right" value="right"  <?php if ($image_align == 'right') echo ' checked="checked"'; ?>/> 
    132132        <label for="image-align-right" id="image-align-right-label"><?php _e('Right'); ?></label> 
    133133</fieldset> 
     134<fieldset id="image-size"> 
     135        <legend><?php _e('Size'); ?></legend> 
     136        <input type="radio" name="image-size" id="image-size-thumb" value="thumb" <?php if ($image_size == 'thumb') echo ' checked="checked"'; ?>/> 
     137        <label for="image-size-thumb" id="image-size-thumb-label"><?php _e('Thumbnail'); ?></label> 
     138        <input type="radio" name="image-size" id="image-size-medium" value="medium" <?php if ($image_size == 'medium' || !$image_size) echo ' checked="checked"'; ?>/> 
     139        <label for="image-size-medium" id="image-size-medium-label"><?php _e('Medium'); ?></label> 
     140        <input type="radio" name="image-size" id="image-size-full" value="full" <?php if ($image_size == 'full') echo ' checked="checked"'; ?>/> 
     141        <label for="image-size-full" id="image-size-full-label"><?php _e('Full size'); ?></label> 
     142</fieldset> 
    134143<p> 
    135144        <button name="image-add" id="image-add" class="button-ok" value="1"><?php _e('Add Image'); ?></button> 
    136145        <a href="#" onClick="return top.tb_remove();" id="image-cancel" class="button-cancel"><?php _e('Cancel'); ?></a> 
     
    174183                if ( is_wp_error($id) ) 
    175184                        wp_iframe( 'image_upload_form', get_option('siteurl') . '/wp-admin/media-upload.php?type=image', $_POST, $id ); 
    176185                else { 
    177                         media_send_to_editor(get_image_send_to_editor($id, $_POST['image-alt'], $_POST['image-title'], $_POST['image-align'], $_POST['image-url'])); 
     186                        media_send_to_editor(get_image_send_to_editor($id, $_POST['image-alt'], $_POST['image-title'], $_POST['image-align'], $_POST['image-url'], true, $_POST['image-size'])); 
    178187                } 
    179188        } 
    180189} 
     
    217226 
    218227add_filter('async_upload_image', 'async_image_callback'); 
    219228 
    220 // scale down the default size of an image so it's a better fit for the editor and theme 
    221 function image_constrain_size_for_editor($width, $height) { 
    222         // pick a reasonable default width for the image 
    223         // $content_width might be set in the theme's functions.php 
    224         if ( !empty($GLOBALS['content_width']) ) 
    225                 $max_width = $GLOBALS['content_width']; 
    226         else 
    227                 $max_width = 500; 
    228229 
    229         $max_width = apply_filters( 'editor_max_image_width', $max_width ); 
    230         $max_height = apply_filters( 'editor_max_image_height', $max_width ); 
    231          
    232         return wp_shrink_dimensions( $width, $height, $max_width, $max_height ); 
    233 } 
     230function get_image_send_to_editor($id, $alt, $title, $align, $url='', $rel = false, $size='medium') { 
    234231 
    235 function get_image_send_to_editor($id, $alt, $title, $align, $url='', $rel = false) { 
     232        $html = get_image_tag($id, $alt, $title, $align, $rel, $size); 
    236233 
    237         $img_src = wp_get_attachment_url($id); 
    238         $meta = wp_get_attachment_metadata($id); 
    239  
    240         $hwstring = ''; 
    241         if ( isset($meta['width'], $meta['height']) ) { 
    242                 list( $width, $height ) = image_constrain_size_for_editor( $meta['width'], $meta['height'] ); 
    243                 $hwstring = ' width="'.intval($width).'" height="'.intval($height).'"'; 
    244         } 
    245  
    246         $html = '<img src="'.attribute_escape($img_src).'" alt="'.attribute_escape($alt).'" title="'.attribute_escape($title).'"'.$hwstring.' class="align-'.attribute_escape($align).'" />'; 
    247  
    248234        $rel = $rel ? ' rel="attachment wp-att-'.attribute_escape($id).'"' : ''; 
    249235        if ( $url ) 
    250236                $html = "<a href='".attribute_escape($url)."'$rel>$html</a>"; 
     237        elseif ( $size == 'thumb' || $size == 'medium' ) 
     238                $html = '<a href="'.get_attachment_link($id).'"'.$rel.'>'.$html.'</a>'; 
    251239                 
    252240        $html = apply_filters( 'image_send_to_editor', $html, $id, $alt, $title, $align, $url ); 
    253241 
     
    392380        $multimedia_upload_iframe_src = apply_filters('multimedia_upload_iframe_src', $multimedia_upload_iframe_src); 
    393381        $out = <<<EOF 
    394382 
    395 <a href="{$image_upload_iframe_src}&TB_iframe=true&height=500&width=480" class="thickbox"><img src='images/media-button-image.gif' alt='' /></a> 
     383<a href="{$image_upload_iframe_src}&TB_iframe=true&height=550&width=480" class="thickbox"><img src='images/media-button-image.gif' alt='' /></a> 
    396384<a href="{$multimedia_upload_iframe_src}&TB_iframe=true&height=500&width=640" class="thickbox"><img src='images/media-button-gallery.gif' alt='' /></a> 
    397385<a href="{$image_upload_iframe_src}&TB_iframe=true&height=500&width=640" class="thickbox"><img src='images/media-button-video.gif' alt='' /></a> 
    398386<a href="{$image_upload_iframe_src}&TB_iframe=true&height=500&width=640" class="thickbox"><img src='images/media-button-music.gif' alt='' /></a> 
  • wp-admin/includes/image.php

     
    224224 * 
    225225 */ 
    226226function wp_shrink_dimensions( $width, $height, $wmax = 128, $hmax = 96 ) { 
    227         if ( $height <= $hmax && $width <= $wmax ){ 
    228                 //Image is smaller than max 
    229                 return array( $width, $height); 
    230         } elseif ( $width / $height > $wmax / $hmax ) { 
    231                 //Image Width will be greatest 
    232                 return array( $wmax, (int) ($height / $width * $wmax )); 
    233         } else { 
    234                 //Image Height will be greatest 
    235                 return array( (int) ($width / $height * $hmax ), $hmax ); 
    236         } 
     227        return wp_constrain_dimensions( $width, $height, $wmax, $hmax ); 
    237228} 
    238229 
     230// same as wp_shrink_dimensions, except the max parameters are optional. 
     231// if either width or height are empty, no constraint is applied on that dimension. 
     232function wp_constrain_dimensions( $current_width, $current_height, $max_width=0, $max_height=0 ) { 
     233        if ( !$max_width and !$max_height ) 
     234                return array( $current_width, $current_height ); 
     235         
     236        $width_ratio = $height_ratio = 1.0; 
     237         
     238        if ( $max_width > 0 && $current_width > $max_width ) 
     239                $width_ratio = $max_width / $current_width; 
     240         
     241        if ( $max_height > 0 && $current_height > $max_height ) 
     242                $height_ratio = $max_height / $current_height; 
     243         
     244        // the smaller ratio is the one we need to fit it to the constraining box 
     245        $ratio = min( $width_ratio, $height_ratio ); 
     246         
     247        return array( intval($current_width * $ratio), intval($current_height * $ratio) ); 
     248} 
     249 
    239250// convert a fraction string to a decimal 
    240251function wp_exif_frac2dec($str) { 
    241252        @list( $n, $d ) = explode( '/', $str ); 
  • wp-admin/css/media.css

     
    109109        background: url(../images/align-right.png) no-repeat center left; 
    110110} 
    111111 
     112.media-upload-form fieldset#image-size label { 
     113        display: inline; 
     114        margin: 0 1em 0 0; 
     115} 
     116 
    112117.pinkynail { 
    113118        max-width: 40px; 
    114119        max-height: 40px; 
  • wp-admin/options-writing.php

     
    5757</tr> 
    5858</table> 
    5959 
     60<h3><?php _e('Image sizes') ?></h3> 
     61<p><?php _e('The sizes listed below determine the maximum dimensions to use when inserting an image into the body of a post.'); ?></p> 
     62 
     63<table class="niceblue"> 
     64<tr valign="top"> 
     65<th scope="row"><?php _e('Thumbnail size:') ?></th> 
     66<td> 
     67<label for="thumbnail_size_w"><?php _e('Width:'); ?></label> 
     68<input name="thumbnail_size_w" type="text" id="thumbnail_size_w" value="<?php form_option('thumbnail_size_w'); ?>" size="6" /> 
     69<label for="thumbnail_size_h"><?php _e('Height:'); ?></label> 
     70<input name="thumbnail_size_h" type="text" id="thumbnail_size_h" value="<?php form_option('thumbnail_size_h'); ?>" size="6" /> 
     71</td> 
     72</tr> 
     73<tr valign="top"> 
     74<th scope="row"><?php _e('Medium size:') ?></th> 
     75<td> 
     76<label for="medium_size_w"><?php _e('Width:'); ?></label> 
     77<input name="medium_size_w" type="text" id="medium_size_w" value="<?php form_option('medium_size_w'); ?>" size="6" /> 
     78<label for="medium_size_h"><?php _e('Height:'); ?></label> 
     79<input name="medium_size_h" type="text" id="thumbnail_size_h" value="<?php form_option('medium_size_h'); ?>" size="6" /> 
     80</td> 
     81</tr> 
     82</table> 
     83 
    6084<h3><?php _e('Post via e-mail') ?></h3> 
    6185<p><?php printf(__('To post to WordPress by e-mail you must set up a secret e-mail account with POP3 access. Any mail received at this address will be posted, so it&#8217;s a good idea to keep this address very secret. Here are three random strings you could use: <code>%s</code>, <code>%s</code>, <code>%s</code>.'), wp_generate_password(), wp_generate_password(), wp_generate_password()) ?></p> 
    6286