From 0456afb16d10ad4ccc0d1a5a9b979f35ce7135e0 Mon Sep 17 00:00:00 2001 From: matt Date: Tue, 4 Mar 2008 04:21:37 +0000 Subject: [PATCH] New functions for attachment image display, fixes #6086. Hat tip: tellyworth. git-svn-id: http://svn.automattic.com/wordpress/trunk@7149 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-admin/includes/schema.php | 4 +-- wp-includes/media.php | 51 ++++++++++++++++++++++++++++------- wp-includes/post-template.php | 37 ++++++++++++++++++++++--- 3 files changed, 76 insertions(+), 16 deletions(-) diff --git a/wp-admin/includes/schema.php b/wp-admin/includes/schema.php index e8928cbf7a..e94c39d0a5 100644 --- a/wp-admin/includes/schema.php +++ b/wp-admin/includes/schema.php @@ -247,8 +247,8 @@ function populate_options() { add_option('thumbnail_size_w', 150); add_option('thumbnail_size_h', 150); add_option('thumbnail_crop', 1); - add_option('medium_size_w', ''); - add_option('medium_size_h', ''); + add_option('medium_size_w', 300); + add_option('medium_size_h', 300); add_option('autosave_interval', 60); // Delete unused options diff --git a/wp-includes/media.php b/wp-includes/media.php index d546204b09..6e352e5d8f 100644 --- a/wp-includes/media.php +++ b/wp-includes/media.php @@ -18,18 +18,16 @@ function image_constrain_size_for_editor($width, $height, $size = 'medium') { $max_width = intval(get_option('medium_size_w')); $max_height = intval(get_option('medium_size_h')); // if no width is set, default to the theme content width if available - if ( !$max_width ) { - // $content_width might be set in the current theme's functions.php - if ( !empty($GLOBALS['content_width']) ) { - $max_width = $GLOBALS['content_width']; - } - else - $max_width = 500; - } } else { // $size == 'full' - $max_width = 0; - $max_height = 0; + // we're inserting a full size image into the editor. if it's a really big image we'll scale it down to fit reasonably + // within the editor itself, and within the theme's content width if it's known. the user can resize it in the editor + // if they wish. + if ( !empty($GLOBALS['content_width']) ) { + $max_width = $GLOBALS['content_width']; + } + else + $max_width = 500; } list( $max_width, $max_height ) = apply_filters( 'editor_max_image_size', array( $max_width, $max_height ), $size ); @@ -258,5 +256,38 @@ function image_get_intermediate_size($post_id, $size='thumbnail') { return $imagedata['sizes'][$size]; } +// get an image to represent an attachment - a mime icon for files, thumbnail or intermediate size for images +// returns an array (url, width, height), or false if no image is available +function wp_get_attachment_image_src($attachment_id, $size='thumbnail') { + + // get a thumbnail or intermediate image if there is one + $image = image_downsize($attachment_id, $size); + if ( $image ) { + list ( $src, $width, $height ) = $image; + } + elseif ( $src = wp_mime_type_icon($attachment_id) ) { + $icon_dir = apply_filters( 'icon_dir', get_template_directory() . '/images' ); + $src_file = $icon_dir . '/' . basename($src); + @list($width, $height) = getimagesize($src_file); + } + + if ( $src && $width && $height ) + return array( $src, $width, $height ); + return false; +} + +// as per wp_get_attachment_image_src, but returns an tag +function wp_get_attachment_image($attachment_id, $size='thumbnail') { + + $html = ''; + $image = wp_get_attachment_image_src($attachment_id, $size); + if ( $image ) { + list($src, $width, $height) = $image; + $hwstring = image_hwstring($width, $height); + $html = ''; + } + + return $html; +} ?> diff --git a/wp-includes/post-template.php b/wp-includes/post-template.php index b99af8e9db..a729c24739 100644 --- a/wp-includes/post-template.php +++ b/wp-includes/post-template.php @@ -362,10 +362,34 @@ function walk_page_dropdown_tree() { // Attachments // -function the_attachment_link($id = 0, $fullsize = false, $max_dims = false, $permalink = false) { - echo get_the_attachment_link($id, $fullsize, $max_dims, $permalink); +function the_attachment_link($id = 0, $fullsize = false, $deprecated = false, $permalink = false) { + if ( $fullsize ) + echo wp_get_attachment_link($id, 'full', $permalink); + else + echo wp_get_attachment_link($id, 'thumbnail', $permalink); } +// get an attachment page link using an image or icon if possible +function wp_get_attachment_link($id = 0, $size = 'thumbnail', $permalink = false) { + $_post = & get_post( intval($id) ); + + if ( ('attachment' != $_post->post_type) || !$url = wp_get_attachment_url($_post->ID) ) + return __('Missing Attachment'); + + if ( $permalink ) + $url = get_attachment_link($_post->ID); + + $post_title = attribute_escape($_post->post_title); + + $link_text = wp_get_attachment_image($attachment_id, $size); + if ( !$link_text ) + $link_text = $_post->post_title; + + return "$link_text"; + +} + +// deprecated - use wp_get_attachment_link() function get_the_attachment_link($id = 0, $fullsize = false, $max_dims = false, $permalink = false) { $id = (int) $id; $_post = & get_post($id); @@ -382,6 +406,8 @@ function get_the_attachment_link($id = 0, $fullsize = false, $max_dims = false, return "$innerHTML"; } + +// deprecated: use wp_get_attachment_image_src() function get_attachment_icon_src( $id = 0, $fullsize = false ) { $id = (int) $id; if ( !$post = & get_post($id) ) @@ -413,11 +439,12 @@ function get_attachment_icon_src( $id = 0, $fullsize = false ) { return array($src, $src_file); } +// deprecated: use wp_get_attachment_image() function get_attachment_icon( $id = 0, $fullsize = false, $max_dims = false ) { $id = (int) $id; if ( !$post = & get_post($id) ) return false; - + if ( !$src = get_attachment_icon_src( $post->ID, $fullsize ) ) return false; @@ -456,6 +483,7 @@ function get_attachment_icon( $id = 0, $fullsize = false, $max_dims = false ) { return apply_filters( 'attachment_icon', $icon, $post->ID ); } +// deprecated: use wp_get_attachment_image() function get_attachment_innerHTML($id = 0, $fullsize = false, $max_dims = false) { $id = (int) $id; if ( !$post = & get_post($id) ) @@ -472,7 +500,8 @@ function get_attachment_innerHTML($id = 0, $fullsize = false, $max_dims = false) function prepend_attachment($content) { $p = '

'; - $p .= get_the_attachment_link(false, true, array(400, 300)); + // show the medium sized image representation of the attachment if available, and link to the raw file + $p .= wp_get_attachment_link(0, 'medium', false); $p .= '

'; $p = apply_filters('prepend_attachment', $p);