X-Git-Url: https://scripts.mit.edu/gitweb/autoinstalls/wordpress.git/blobdiff_plain/699231ae09f7057a4d0000cdf32e50a3df6a04ca..61343b82c4f0da4c68e4c6373daafff4a81efdd1:/wp-includes/media.php diff --git a/wp-includes/media.php b/wp-includes/media.php index ee4db93b..6111f363 100644 --- a/wp-includes/media.php +++ b/wp-includes/media.php @@ -3,6 +3,7 @@ * WordPress API for media display. * * @package WordPress + * @subpackage Media */ /** @@ -17,7 +18,7 @@ * than the supported will result in the content_width size or 500 if that is * not set. * - * Finally, there is a filter named, 'editor_max_image_size' that will be called + * Finally, there is a filter named 'editor_max_image_size', that will be called * on the calculated array for width and height, respectively. The second * parameter will be the value that was in the $size parameter. The returned * type for the hook is an array with the width as the first element and the @@ -29,11 +30,15 @@ * @param int $width Width of the image * @param int $height Height of the image * @param string|array $size Size of what the result image should be. + * @param context Could be 'display' (like in a theme) or 'edit' (like inserting into an editor) * @return array Width and height of what the result image should resize to. */ -function image_constrain_size_for_editor($width, $height, $size = 'medium') { +function image_constrain_size_for_editor($width, $height, $size = 'medium', $context = null ) { global $content_width, $_wp_additional_image_sizes; + if ( ! $context ) + $context = is_admin() ? 'edit' : 'display'; + if ( is_array($size) ) { $max_width = $size[0]; $max_height = $size[1]; @@ -53,9 +58,9 @@ function image_constrain_size_for_editor($width, $height, $size = 'medium') { // if no width is set, default to the theme content width if available } elseif ( $size == 'large' ) { - // we're inserting a large size image into the editor. if it's a really + // We're inserting a large 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 + // itself, and within the theme's content width if it's known. The user // can resize it in the editor if they wish. $max_width = intval(get_option('large_size_w')); $max_height = intval(get_option('large_size_h')); @@ -64,7 +69,7 @@ function image_constrain_size_for_editor($width, $height, $size = 'medium') { } 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 && is_admin() ) // Only in admin. Assume that theme authors know what they're doing. + if ( intval($content_width) > 0 && 'edit' == $context ) // Only in admin. Assume that theme authors know what they're doing. $max_width = min( intval($content_width), $max_width ); } // $size == 'full' has no constraint @@ -73,7 +78,7 @@ function image_constrain_size_for_editor($width, $height, $size = 'medium') { $max_height = $height; } - list( $max_width, $max_height ) = apply_filters( 'editor_max_image_size', array( $max_width, $max_height ), $size ); + list( $max_width, $max_height ) = apply_filters( 'editor_max_image_size', array( $max_width, $max_height ), $size, $context ); return wp_constrain_dimensions( $width, $height, $max_width, $max_height ); } @@ -125,7 +130,7 @@ function image_hwstring($width, $height) { * resize services. * * @param int $id Attachment ID for image. - * @param string $size Optional, default is 'medium'. Size of image, can be 'thumbnail'. + * @param array|string $size Optional, default is 'medium'. Size of image, either array or string. * @return bool|array False on failure, array on success. */ function image_downsize($id, $size = 'medium') { @@ -133,18 +138,19 @@ function image_downsize($id, $size = 'medium') { if ( !wp_attachment_is_image($id) ) return false; + // plugins can use this to provide resize services + if ( $out = apply_filters( 'image_downsize', false, $id, $size ) ) + return $out; + $img_url = wp_get_attachment_url($id); $meta = wp_get_attachment_metadata($id); $width = $height = 0; $is_intermediate = false; - - // plugins can use this to provide resize services - if ( $out = apply_filters('image_downsize', false, $id, $size) ) - return $out; + $img_url_basename = wp_basename($img_url); // try for a new style intermediate size if ( $intermediate = image_get_intermediate_size($id, $size) ) { - $img_url = str_replace(basename($img_url), $intermediate['file'], $img_url); + $img_url = str_replace($img_url_basename, $intermediate['file'], $img_url); $width = $intermediate['width']; $height = $intermediate['height']; $is_intermediate = true; @@ -152,13 +158,13 @@ function image_downsize($id, $size = 'medium') { elseif ( $size == 'thumbnail' ) { // fall back to the old thumbnail if ( ($thumb_file = wp_get_attachment_thumb_file($id)) && $info = getimagesize($thumb_file) ) { - $img_url = str_replace(basename($img_url), basename($thumb_file), $img_url); + $img_url = str_replace($img_url_basename, wp_basename($thumb_file), $img_url); $width = $info[0]; $height = $info[1]; $is_intermediate = true; } } - if ( !$width && !$height && isset($meta['width'], $meta['height']) ) { + if ( !$width && !$height && isset( $meta['width'], $meta['height'] ) ) { // any other type: use the real image $width = $meta['width']; $height = $meta['height']; @@ -176,16 +182,20 @@ function image_downsize($id, $size = 'medium') { /** * Registers a new image size + * + * @since 2.9.0 */ -function add_image_size( $name, $width = 0, $height = 0, $crop = FALSE ) { +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 ); + $_wp_additional_image_sizes[$name] = array( 'width' => absint( $width ), 'height' => absint( $height ), 'crop' => (bool) $crop ); } /** * Registers an image size for the post thumbnail + * + * @since 2.9.0 */ -function set_post_thumbnail_size( $width = 0, $height = 0, $crop = FALSE ) { +function set_post_thumbnail_size( $width = 0, $height = 0, $crop = false ) { add_image_size( 'post-thumbnail', $width, $height, $crop ); } @@ -220,10 +230,12 @@ function get_image_tag($id, $alt, $title, $align, $size='medium') { list( $img_src, $width, $height ) = image_downsize($id, $size); $hwstring = image_hwstring($width, $height); + $title = $title ? 'title="' . esc_attr( $title ) . '" ' : ''; + $class = 'align' . esc_attr($align) .' size-' . esc_attr($size) . ' wp-image-' . $id; $class = apply_filters('get_image_tag_class', $class, $id, $align, $size); - $html = '' . esc_attr($alt) . ''; + $html = '' . esc_attr($alt) . ''; $html = apply_filters( 'get_image_tag', $html, $id, $alt, $title, $align, $size ); @@ -231,35 +243,7 @@ function get_image_tag($id, $alt, $title, $align, $size='medium') { } /** - * Load an image from a string, if PHP supports it. - * - * @since 2.1.0 - * - * @param string $file Filename of the image to load. - * @return resource The resulting image resource on success, Error string on failure. - */ -function wp_load_image( $file ) { - if ( is_numeric( $file ) ) - $file = get_attached_file( $file ); - - if ( ! file_exists( $file ) ) - return sprintf(__('File “%s” doesn’t exist?'), $file); - - if ( ! function_exists('imagecreatefromstring') ) - return __('The GD image library is not installed.'); - - // Set artificially high because GD uses uncompressed images in memory - @ini_set('memory_limit', '256M'); - $image = imagecreatefromstring( file_get_contents( $file ) ); - - if ( !is_resource( $image ) ) - return sprintf(__('File “%s” is not an image.'), $file); - - return $image; -} - -/** - * Calculates the new dimentions for a downsampled image. + * Calculates the new dimensions for a downsampled image. * * If either width or height are empty, no constraint is applied on * that dimension. @@ -304,7 +288,7 @@ function wp_constrain_dimensions( $current_width, $current_height, $max_width=0, $h = intval( $current_height * $ratio ); // Sometimes, due to rounding, we'll end up with a result like this: 465x700 in a 177x177 box is 117x176... a pixel short - // We also have issues with recursive calls resulting in an ever-changing result. Contraining to the result of a constraint should yield the original result. + // We also have issues with recursive calls resulting in an ever-changing result. Constraining to the result of a constraint should yield the original result. // Thus we look for dimensions that are one pixel shy of the max value and bump them up if ( $did_width && $w == $max_width - 1 ) $w = $max_width; // Round it up @@ -315,20 +299,22 @@ function wp_constrain_dimensions( $current_width, $current_height, $max_width=0, } /** - * Retrieve calculated resized dimensions for use in imagecopyresampled(). + * Retrieve calculated resized dimensions for use in WP_Image_Editor. * * Calculate dimensions and coordinates for a resized image that fits within a * specified width and height. If $crop is true, the largest matching central * portion of the image will be cropped out and resized to the required size. * * @since 2.5.0 + * @uses apply_filters() Calls 'image_resize_dimensions' on $orig_w, $orig_h, $dest_w, $dest_h and + * $crop to provide custom resize dimensions. * * @param int $orig_w Original width. * @param int $orig_h Original height. * @param int $dest_w New width. * @param int $dest_h New height. * @param bool $crop Optional, default is false. Whether to crop image or resize. - * @return bool|array False, on failure. Returned array matches parameters for imagecopyresampled() PHP function. + * @return bool|array False on failure. Returned array matches parameters for imagecopyresampled() PHP function. */ function image_resize_dimensions($orig_w, $orig_h, $dest_w, $dest_h, $crop = false) { @@ -338,6 +324,11 @@ function image_resize_dimensions($orig_w, $orig_h, $dest_w, $dest_h, $crop = fal if ($dest_w <= 0 && $dest_h <= 0) return false; + // plugins can use this to provide custom resize dimensions + $output = apply_filters( 'image_resize_dimensions', null, $orig_w, $orig_h, $dest_w, $dest_h, $crop ); + if ( null !== $output ) + return $output; + if ( $crop ) { // crop the largest possible portion of the original image that we can size to $dest_w x $dest_h $aspect_ratio = $orig_w / $orig_h; @@ -380,90 +371,6 @@ function image_resize_dimensions($orig_w, $orig_h, $dest_w, $dest_h, $crop = fal } -/** - * Scale down an image to fit a particular size and save a new copy of the image. - * - * The PNG transparency will be preserved using the function, as well as the - * image type. If the file going in is PNG, then the resized image is going to - * be PNG. The only supported image types are PNG, GIF, and JPEG. - * - * Some functionality requires API to exist, so some PHP version may lose out - * support. This is not the fault of WordPress (where functionality is - * downgraded, not actual defects), but of your PHP version. - * - * @since 2.5.0 - * - * @param string $file Image file path. - * @param int $max_w Maximum width to resize to. - * @param int $max_h Maximum height to resize to. - * @param bool $crop Optional. Whether to crop image or resize. - * @param string $suffix Optional. File Suffix. - * @param string $dest_path Optional. New image file path. - * @param int $jpeg_quality Optional, default is 90. Image quality percentage. - * @return mixed WP_Error on failure. String with new destination path. - */ -function image_resize( $file, $max_w, $max_h, $crop = false, $suffix = null, $dest_path = null, $jpeg_quality = 90 ) { - - $image = wp_load_image( $file ); - if ( !is_resource( $image ) ) - return new WP_Error( 'error_loading_image', $image, $file ); - - $size = @getimagesize( $file ); - if ( !$size ) - return new WP_Error('invalid_image', __('Could not read image size'), $file); - list($orig_w, $orig_h, $orig_type) = $size; - - $dims = image_resize_dimensions($orig_w, $orig_h, $max_w, $max_h, $crop); - if ( !$dims ) - return new WP_Error( 'error_getting_dimensions', __('Could not calculate resized image dimensions') ); - list($dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h) = $dims; - - $newimage = wp_imagecreatetruecolor( $dst_w, $dst_h ); - - imagecopyresampled( $newimage, $image, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h); - - // convert from full colors to index colors, like original PNG. - if ( IMAGETYPE_PNG == $orig_type && function_exists('imageistruecolor') && !imageistruecolor( $image ) ) - imagetruecolortopalette( $newimage, false, imagecolorstotal( $image ) ); - - // we don't need the original in memory anymore - imagedestroy( $image ); - - // $suffix will be appended to the destination filename, just before the extension - if ( !$suffix ) - $suffix = "{$dst_w}x{$dst_h}"; - - $info = pathinfo($file); - $dir = $info['dirname']; - $ext = $info['extension']; - $name = basename($file, ".{$ext}"); - if ( !is_null($dest_path) and $_dest_path = realpath($dest_path) ) - $dir = $_dest_path; - $destfilename = "{$dir}/{$name}-{$suffix}.{$ext}"; - - if ( IMAGETYPE_GIF == $orig_type ) { - if ( !imagegif( $newimage, $destfilename ) ) - return new WP_Error('resize_path_invalid', __( 'Resize path invalid' )); - } elseif ( IMAGETYPE_PNG == $orig_type ) { - if ( !imagepng( $newimage, $destfilename ) ) - return new WP_Error('resize_path_invalid', __( 'Resize path invalid' )); - } else { - // all other formats are converted to jpg - $destfilename = "{$dir}/{$name}-{$suffix}.jpg"; - if ( !imagejpeg( $newimage, $destfilename, apply_filters( 'jpeg_quality', $jpeg_quality, 'image_resize' ) ) ) - return new WP_Error('resize_path_invalid', __( 'Resize path invalid' )); - } - - imagedestroy( $newimage ); - - // Set correct file permissions - $stat = stat( dirname( $destfilename )); - $perms = $stat['mode'] & 0000666; //same permissions as parent folder, strip off the executable bits - @ chmod( $destfilename, $perms ); - - return $destfilename; -} - /** * Resize an image to make a thumbnail or intermediate size. * @@ -479,16 +386,18 @@ function image_resize( $file, $max_w, $max_h, $crop = false, $suffix = null, $de * @param bool $crop Optional, default is false. Whether to crop image to specified height and width or resize. * @return bool|array False, if no image was created. Metadata array on success. */ -function image_make_intermediate_size($file, $width, $height, $crop=false) { +function image_make_intermediate_size( $file, $width, $height, $crop = false ) { if ( $width || $height ) { - $resized_file = image_resize($file, $width, $height, $crop); - if ( !is_wp_error($resized_file) && $resized_file && $info = getimagesize($resized_file) ) { - $resized_file = apply_filters('image_make_intermediate_size', $resized_file); - return array( - 'file' => basename( $resized_file ), - 'width' => $info[0], - 'height' => $info[1], - ); + $editor = wp_get_image_editor( $file ); + + if ( is_wp_error( $editor ) || is_wp_error( $editor->resize( $width, $height, $crop ) ) ) + return false; + + $resized_file = $editor->save(); + + if ( ! is_wp_error( $resized_file ) && $resized_file ) { + unset( $resized_file['path'] ); + return $resized_file; } } return false; @@ -606,7 +515,7 @@ function wp_get_attachment_image_src($attachment_id, $size='thumbnail', $icon = if ( $icon && $src = wp_mime_type_icon($attachment_id) ) { $icon_dir = apply_filters( 'icon_dir', ABSPATH . WPINC . '/images/crystal' ); - $src_file = $icon_dir . '/' . basename($src); + $src_file = $icon_dir . '/' . wp_basename($src); @list($width, $height) = getimagesize($src_file); } if ( $src && $width && $height ) @@ -630,6 +539,7 @@ function wp_get_attachment_image_src($attachment_id, $size='thumbnail', $icon = * @param int $attachment_id Image attachment ID. * @param string $size Optional, default is 'thumbnail'. * @param bool $icon Optional, default is false. Whether it is an icon. + * @param mixed $attr Optional, attributes for the image markup. * @return string HTML img element or empty string on failure. */ function wp_get_attachment_image($attachment_id, $size = 'thumbnail', $icon = false, $attr = '') { @@ -641,12 +551,11 @@ function wp_get_attachment_image($attachment_id, $size = 'thumbnail', $icon = fa $hwstring = image_hwstring($width, $height); if ( is_array($size) ) $size = join('x', $size); - $attachment =& get_post($attachment_id); + $attachment = get_post($attachment_id); $default_attr = array( 'src' => $src, 'class' => "attachment-$size", 'alt' => trim(strip_tags( get_post_meta($attachment_id, '_wp_attachment_image_alt', true) )), // Use Alt field first - 'title' => trim(strip_tags( $attachment->post_title )), ); if ( empty($default_attr['alt']) ) $default_attr['alt'] = trim(strip_tags( $attachment->post_excerpt )); // If not, Use the Caption @@ -667,9 +576,9 @@ function wp_get_attachment_image($attachment_id, $size = 'thumbnail', $icon = fa } /** - * Adds a 'wp-post-image' class to post thumbnail thumbnails + * Adds a 'wp-post-image' class to post thumbnails * Uses the begin_fetch_post_thumbnail_html and end_fetch_post_thumbnail_html action hooks to - * dynamically add/remove itself so as to only filter post thumbnail thumbnails + * dynamically add/remove itself so as to only filter post thumbnails * * @since 2.9.0 * @param array $attr Attributes including src, class, alt, title @@ -718,6 +627,13 @@ add_shortcode('caption', 'img_caption_shortcode'); * @return string */ function img_caption_shortcode($attr, $content = null) { + // New-style shortcode with the caption inside the shortcode with the link and image tags. + if ( ! isset( $attr['caption'] ) ) { + if ( preg_match( '#((?:]+>\s*)?]+>(?:\s*)?)(.*)#is', $content, $matches ) ) { + $content = $matches[1]; + $attr['caption'] = trim( $matches[2] ); + } + } // Allow plugins/themes to override the default caption template. $output = apply_filters('img_caption_shortcode', '', $attr, $content); @@ -729,7 +645,7 @@ function img_caption_shortcode($attr, $content = null) { 'align' => 'alignnone', 'width' => '', 'caption' => '' - ), $attr)); + ), $attr, 'caption')); if ( 1 > (int) $width || empty($caption) ) return $content; @@ -750,15 +666,22 @@ add_shortcode('gallery', 'gallery_shortcode'); * * @since 2.5.0 * - * @param array $attr Attributes attributed to the shortcode. + * @param array $attr Attributes of the shortcode. * @return string HTML content to display gallery. */ function gallery_shortcode($attr) { - global $post, $wp_locale; + $post = get_post(); static $instance = 0; $instance++; + if ( ! empty( $attr['ids'] ) ) { + // 'ids' is explicitly ordered, unless you specify otherwise. + if ( empty( $attr['orderby'] ) ) + $attr['orderby'] = 'post__in'; + $attr['include'] = $attr['ids']; + } + // Allow plugins/themes to override the default gallery template. $output = apply_filters('post_gallery', '', $attr); if ( $output != '' ) @@ -774,7 +697,7 @@ function gallery_shortcode($attr) { extract(shortcode_atts(array( 'order' => 'ASC', 'orderby' => 'menu_order ID', - 'id' => $post->ID, + 'id' => $post ? $post->ID : 0, 'itemtag' => 'dl', 'icontag' => 'dt', 'captiontag' => 'dd', @@ -782,14 +705,13 @@ function gallery_shortcode($attr) { 'size' => 'thumbnail', 'include' => '', 'exclude' => '' - ), $attr)); + ), $attr, 'gallery')); $id = intval($id); if ( 'RAND' == $order ) $orderby = 'none'; if ( !empty($include) ) { - $include = preg_replace( '/[^0-9,]+/', '', $include ); $_attachments = get_posts( array('include' => $include, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby) ); $attachments = array(); @@ -797,7 +719,6 @@ function gallery_shortcode($attr) { $attachments[$val->ID] = $_attachments[$key]; } } elseif ( !empty($exclude) ) { - $exclude = preg_replace( '/[^0-9,]+/', '', $exclude ); $attachments = get_children( array('post_parent' => $id, 'exclude' => $exclude, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby) ); } else { $attachments = get_children( array('post_parent' => $id, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby) ); @@ -815,13 +736,24 @@ function gallery_shortcode($attr) { $itemtag = tag_escape($itemtag); $captiontag = tag_escape($captiontag); + $icontag = tag_escape($icontag); + $valid_tags = wp_kses_allowed_html( 'post' ); + if ( ! isset( $valid_tags[ $itemtag ] ) ) + $itemtag = 'dl'; + if ( ! isset( $valid_tags[ $captiontag ] ) ) + $captiontag = 'dd'; + if ( ! isset( $valid_tags[ $icontag ] ) ) + $icontag = 'dt'; + $columns = intval($columns); $itemwidth = $columns > 0 ? floor(100/$columns) : 100; $float = is_rtl() ? 'right' : 'left'; $selector = "gallery-{$instance}"; - $output = apply_filters('gallery_style', " + $gallery_style = $gallery_div = ''; + if ( apply_filters( 'use_default_gallery_style', true ) ) + $gallery_style = " - -