X-Git-Url: https://scripts.mit.edu/gitweb/autoinstalls/wordpress.git/blobdiff_plain/449d082fcc4873c1f7d363a0d9f7409be7f6e77d..1c09677af04c9e37714e09b73eb9dbc5b2e3eb13:/wp-includes/media.php diff --git a/wp-includes/media.php b/wp-includes/media.php index 58feaf3a..8f0bf3aa 100644 --- a/wp-includes/media.php +++ b/wp-includes/media.php @@ -32,7 +32,7 @@ * @return array Width and height of what the result image should resize to. */ function image_constrain_size_for_editor($width, $height, $size = 'medium') { - global $content_width; + global $content_width, $_wp_additional_image_sizes; if ( is_array($size) ) { $max_width = $size[0]; @@ -61,6 +61,11 @@ function image_constrain_size_for_editor($width, $height, $size = 'medium') { $max_height = intval(get_option('large_size_h')); if ( intval($content_width) > 0 ) $max_width = min( intval($content_width), $max_width ); + } 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 ); } // $size == 'full' has no constraint else { @@ -169,6 +174,21 @@ function image_downsize($id, $size = 'medium') { } +/** + * 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 thumbnail + */ +function set_post_thumbnail_size( $width = 0, $height = 0, $crop = FALSE ) { + add_image_size( 'post-thumbnail', $width, $height, $crop ); +} + /** * An tag for an image attachment, scaling it down if requested. * @@ -200,10 +220,10 @@ function get_image_tag($id, $alt, $title, $align, $size='medium') { list( $img_src, $width, $height ) = image_downsize($id, $size); $hwstring = image_hwstring($width, $height); - $class = 'align'.attribute_escape($align).' size-'.attribute_escape($size).' wp-image-'.$id; + $class = 'align' . esc_attr($align) .' size-' . esc_attr($size) . ' wp-image-' . $id; $class = apply_filters('get_image_tag_class', $class, $id, $align, $size); - $html = ''.attribute_escape($alt).''; + $html = '' . esc_attr($alt) . ''; $html = apply_filters( 'get_image_tag', $html, $id, $alt, $title, $align, $size ); @@ -231,10 +251,10 @@ function wp_constrain_dimensions( $current_width, $current_height, $max_width=0, $width_ratio = $height_ratio = 1.0; - if ( $max_width > 0 && $current_width > $max_width ) + if ( $max_width > 0 && $current_width > 0 && $current_width > $max_width ) $width_ratio = $max_width / $current_width; - if ( $max_height > 0 && $current_height > $max_height ) + if ( $max_height > 0 && $current_height > 0 && $current_height > $max_height ) $height_ratio = $max_height / $current_height; // the smaller ratio is the one we need to fit it to the constraining box @@ -259,7 +279,7 @@ function wp_constrain_dimensions( $current_width, $current_height, $max_width=0, * @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. */ -function image_resize_dimensions($orig_w, $orig_h, $dest_w, $dest_h, $crop=false) { +function image_resize_dimensions($orig_w, $orig_h, $dest_w, $dest_h, $crop = false) { if ($orig_w <= 0 || $orig_h <= 0) return false; @@ -272,22 +292,23 @@ function image_resize_dimensions($orig_w, $orig_h, $dest_w, $dest_h, $crop=false $aspect_ratio = $orig_w / $orig_h; $new_w = min($dest_w, $orig_w); $new_h = min($dest_h, $orig_h); - if (!$new_w) { + + if ( !$new_w ) { $new_w = intval($new_h * $aspect_ratio); } - if (!$new_h) { + + if ( !$new_h ) { $new_h = intval($new_w / $aspect_ratio); } $size_ratio = max($new_w / $orig_w, $new_h / $orig_h); - $crop_w = ceil($new_w / $size_ratio); - $crop_h = ceil($new_h / $size_ratio); + $crop_w = round($new_w / $size_ratio); + $crop_h = round($new_h / $size_ratio); - $s_x = floor(($orig_w - $crop_w)/2); - $s_y = floor(($orig_h - $crop_h)/2); - } - else { + $s_x = floor( ($orig_w - $crop_w) / 2 ); + $s_y = floor( ($orig_h - $crop_h) / 2 ); + } else { // don't crop, just resize using $dest_w x $dest_h as a maximum bounding box $crop_w = $orig_w; $crop_h = $orig_h; @@ -299,12 +320,12 @@ function image_resize_dimensions($orig_w, $orig_h, $dest_w, $dest_h, $crop=false } // if the resulting image would be the same size or larger we don't want to resize it - if ($new_w >= $orig_w && $new_h >= $orig_h) + if ( $new_w >= $orig_w && $new_h >= $orig_h ) return false; // the return array matches the parameters to imagecopyresampled() // int dst_x, int dst_y, int src_x, int src_y, int dst_w, int dst_h, int src_w, int src_h - return array(0, 0, $s_x, $s_y, $new_w, $new_h, $crop_w, $crop_h); + return array( 0, 0, (int) $s_x, (int) $s_y, (int) $new_w, (int) $new_h, (int) $crop_w, (int) $crop_h ); } @@ -330,28 +351,30 @@ function image_resize_dimensions($orig_w, $orig_h, $dest_w, $dest_h, $crop=false * @param int $jpeg_quality Optional, default is 90. Image quality percentage. * @return mixed WP_Error on failure. String with new destination path. Array of dimensions from {@link image_resize_dimensions()} */ -function image_resize( $file, $max_w, $max_h, $crop=false, $suffix=null, $dest_path=null, $jpeg_quality=90) { +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); - list($orig_w, $orig_h, $orig_type) = getimagesize( $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) + if ( !$dims ) return $dims; list($dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h) = $dims; - $newimage = imagecreatetruecolor( $dst_w, $dst_h); - - // preserve PNG transparency - if ( IMAGETYPE_PNG == $orig_type && function_exists( 'imagealphablending' ) && function_exists( 'imagesavealpha' ) ) { - imagealphablending( $newimage, false); - imagesavealpha( $newimage, true); - } + $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 && !imageistruecolor( $image ) ) + imagetruecolortopalette( $newimage, false, imagecolorstotal( $image ) ); + // we don't need the original in memory anymore imagedestroy( $image ); @@ -367,18 +390,16 @@ function image_resize( $file, $max_w, $max_h, $crop=false, $suffix=null, $dest_p $dir = $_dest_path; $destfilename = "{$dir}/{$name}-{$suffix}.{$ext}"; - if ( $orig_type == IMAGETYPE_GIF ) { - if (!imagegif( $newimage, $destfilename ) ) + if ( IMAGETYPE_GIF == $orig_type ) { + if ( !imagegif( $newimage, $destfilename ) ) return new WP_Error('resize_path_invalid', __( 'Resize path invalid' )); - } - elseif ( $orig_type == IMAGETYPE_PNG ) { - if (!imagepng( $newimage, $destfilename ) ) + } elseif ( IMAGETYPE_PNG == $orig_type ) { + if ( !imagepng( $newimage, $destfilename ) ) return new WP_Error('resize_path_invalid', __( 'Resize path invalid' )); - } - else { + } else { // all other formats are converted to jpg $destfilename = "{$dir}/{$name}-{$suffix}.jpg"; - if (!imagejpeg( $newimage, $destfilename, apply_filters( 'jpeg_quality', $jpeg_quality ) ) ) + if ( !imagejpeg( $newimage, $destfilename, apply_filters( 'jpeg_quality', $jpeg_quality, 'image_resize' ) ) ) return new WP_Error('resize_path_invalid', __( 'Resize path invalid' )); } @@ -503,6 +524,8 @@ function wp_get_attachment_image_src($attachment_id, $size='thumbnail', $icon = if ( $image = image_downsize($attachment_id, $size) ) return $image; + $src = false; + 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); @@ -514,9 +537,10 @@ function wp_get_attachment_image_src($attachment_id, $size='thumbnail', $icon = } /** - * Retrieve img HTML content for an image to represent an attachment. + * Get an HTML img element representing an image attachment * - * @see wp_get_attachment_image_src() Returns img HTML element based on array. + * @uses apply_filters() Calls 'wp_get_attachment_image_attributes' hook on attributes array + * @uses wp_get_attachment_image_src() Gets attachment file URL and dimensions * @since 2.5.0 * * @param int $attachment_id Image attachment ID. @@ -524,7 +548,7 @@ function wp_get_attachment_image_src($attachment_id, $size='thumbnail', $icon = * @param bool $icon Optional, default is false. Whether it is an icon. * @return string HTML img element or empty string on failure. */ -function wp_get_attachment_image($attachment_id, $size = 'thumbnail', $icon = false) { +function wp_get_attachment_image($attachment_id, $size = 'thumbnail', $icon = false, $attr = '') { $html = ''; $image = wp_get_attachment_image_src($attachment_id, $size, $icon); @@ -533,12 +557,61 @@ function wp_get_attachment_image($attachment_id, $size = 'thumbnail', $icon = fa $hwstring = image_hwstring($width, $height); if ( is_array($size) ) $size = join('x', $size); - $html = ''; + $attachment =& get_post($attachment_id); + $default_attr = array( + 'src' => $src, + 'class' => "attachment-$size", + 'alt' => trim(strip_tags( $attachment->post_excerpt )), + 'title' => trim(strip_tags( $attachment->post_title )), + ); + $attr = wp_parse_args($attr, $default_attr); + $attr = apply_filters( 'wp_get_attachment_image_attributes', $attr, $attachment ); + $attr = array_map( 'esc_attr', $attr ); + $html = rtrim(" $value ) { + $html .= " $name=" . '"' . $value . '"'; + } + $html .= ' />'; } return $html; } +/** + * Adds a 'wp-post-image' class to post thumbnail 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 + * + * @author Mark Jaquith + * @since 2.9.0 + * @param array $attr Attributes including src, class, alt, title + * @return array + */ +function _wp_post_thumbnail_class_filter( $attr ) { + $attr['class'] .= ' wp-post-image'; + return $attr; +} + +/** + * Adds _wp_post_thumbnail_class_filter to the wp_get_attachment_image_attributes filter + * + * @author Mark Jaquith + * @since 2.9.0 + */ +function _wp_post_thumbnail_class_filter_add( $attr ) { + add_filter( 'wp_get_attachment_image_attributes', '_wp_post_thumbnail_class_filter' ); +} + +/** + * Removes _wp_post_thumbnail_class_filter from the wp_get_attachment_image_attributes filter + * + * @author Mark Jaquith + * @since 2.9.0 + */ +function _wp_post_thumbnail_class_filter_remove( $attr ) { + remove_filter( 'wp_get_attachment_image_attributes', '_wp_post_thumbnail_class_filter' ); +} + add_shortcode('wp_caption', 'img_caption_shortcode'); add_shortcode('caption', 'img_caption_shortcode'); @@ -575,10 +648,10 @@ function img_caption_shortcode($attr, $content = null) { if ( 1 > (int) $width || empty($caption) ) return $content; - if ( $id ) $id = 'id="' . $id . '" '; + if ( $id ) $id = 'id="' . esc_attr($id) . '" '; - return '
' - . $content . '

' . $caption . '

'; + return '
' + . do_shortcode( $content ) . '

' . $caption . '

'; } add_shortcode('gallery', 'gallery_shortcode'); @@ -595,7 +668,10 @@ add_shortcode('gallery', 'gallery_shortcode'); * @return string HTML content to display gallery. */ function gallery_shortcode($attr) { - global $post; + global $post, $wp_locale; + + static $instance = 0; + $instance++; // Allow plugins/themes to override the default gallery template. $output = apply_filters('post_gallery', '', $attr); @@ -617,19 +693,37 @@ function gallery_shortcode($attr) { 'icontag' => 'dt', 'captiontag' => 'dd', 'columns' => 3, - 'size' => 'thumbnail' + 'size' => 'thumbnail', + 'include' => '', + 'exclude' => '' ), $attr)); $id = intval($id); - $attachments = get_children( array('post_parent' => $id, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby) ); + 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(); + foreach ( $_attachments as $key => $val ) { + $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) ); + } if ( empty($attachments) ) return ''; if ( is_feed() ) { $output = "\n"; - foreach ( $attachments as $id => $attachment ) - $output .= wp_get_attachment_link($id, $size, true) . "\n"; + foreach ( $attachments as $att_id => $attachment ) + $output .= wp_get_attachment_link($att_id, $size, true) . "\n"; return $output; } @@ -637,26 +731,29 @@ function gallery_shortcode($attr) { $captiontag = tag_escape($captiontag); $columns = intval($columns); $itemwidth = $columns > 0 ? floor(100/$columns) : 100; + $float = $wp_locale->text_direction == 'rtl' ? 'right' : 'left'; + + $selector = "gallery-{$instance}"; $output = apply_filters('gallery_style', " -