From 147843f96f92e744b8d8d9c965d8ce40247c8a76 Mon Sep 17 00:00:00 2001 From: Ryan Boren Date: Thu, 7 Feb 2008 04:22:38 +0000 Subject: [PATCH] Constrain image size when adding to editor. Props tellyworth. fixes #5777 git-svn-id: https://develop.svn.wordpress.org/trunk@6747 602fd350-edb4-49c9-b593-d223f7449a82 --- wp-admin/includes/media.php | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/wp-admin/includes/media.php b/wp-admin/includes/media.php index 4a50e29790..edfec90c9b 100644 --- a/wp-admin/includes/media.php +++ b/wp-admin/includes/media.php @@ -182,6 +182,20 @@ EOF; add_filter('async_upload_image', 'async_image_callback'); +// scale down the default size of an image so it's a better fit for the editor and theme +function image_constrain_size_for_editor($width, $height) { + // pick a reasonable default width for the image + // $content_width might be set in the theme's functions.php + if ( !empty($GLOBALS['content_width']) ) + $max_width = $GLOBALS['content_width']; + else + $max_width = 500; + + $max_width = apply_filters( 'editor_max_image_width', $max_width ); + $max_height = apply_filters( 'editor_max_image_height', $max_width ); + + return wp_shrink_dimensions( $width, $height, $max_width, $max_height ); +} function image_send_to_editor($id, $alt, $title, $align, $url='') { @@ -189,13 +203,17 @@ function image_send_to_editor($id, $alt, $title, $align, $url='') { $meta = wp_get_attachment_metadata($id); $hwstring = ''; - if ( isset($meta['width'], $meta['height']) ) - $hwstring = ' width="'.intval($meta['width']).'" height="'.intval($meta['height']).'"'; + if ( isset($meta['width'], $meta['height']) ) { + list( $width, $height ) = image_constrain_size_for_editor( $meta['width'], $meta['height'] ); + $hwstring = ' width="'.intval($width).'" height="'.intval($height).'"'; + } $html = ''.attribute_escape($alt).''; if ( $url ) $html = ''.$html.''; + + $html = apply_filters( 'image_send_to_editor', $html, $id, $alt, $title, $align, $url ); media_send_to_editor($html); }