summaryrefslogtreecommitdiffstats
path: root/wp-includes/media.php
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--wp-includes/media.php136
1 files changed, 113 insertions, 23 deletions
diff --git a/wp-includes/media.php b/wp-includes/media.php
index 078e3a2..bf90a6e 100644
--- a/wp-includes/media.php
+++ b/wp-includes/media.php
@@ -378,7 +378,7 @@ function set_post_thumbnail_size( $width = 0, $height = 0, $crop = false ) {
* @param string $align Part of the class name for aligning the image.
* @param string|int[] $size Optional. Image size. Accepts any registered image size name, or an array of
* width and height values in pixels (in that order). Default 'medium'.
- * @return string HTML IMG element for given image attachment?
+ * @return string HTML IMG element for given image attachment.
*/
function get_image_tag( $id, $alt, $title, $align, $size = 'medium' ) {
@@ -972,14 +972,25 @@ function wp_get_attachment_image_src( $attachment_id, $size = 'thumbnail', $icon
$src = false;
if ( $icon ) {
- $src = wp_mime_type_icon( $attachment_id );
+ $src = wp_mime_type_icon( $attachment_id, '.svg' );
if ( $src ) {
/** This filter is documented in wp-includes/post.php */
$icon_dir = apply_filters( 'icon_dir', ABSPATH . WPINC . '/images/media' );
- $src_file = $icon_dir . '/' . wp_basename( $src );
+ $src_file = $icon_dir . '/' . wp_basename( $src );
+
list( $width, $height ) = wp_getimagesize( $src_file );
+
+ $ext = strtolower( substr( $src_file, -4 ) );
+
+ if ( '.svg' === $ext ) {
+ // SVG does not have true dimensions, so this assigns width and height directly.
+ $width = 48;
+ $height = 64;
+ } else {
+ list( $width, $height ) = wp_getimagesize( $src_file );
+ }
}
}
@@ -1248,7 +1259,7 @@ function _wp_get_image_size_from_meta( $size_name, $image_meta ) {
* @param int $attachment_id Image attachment ID.
* @param string|int[] $size Optional. Image size. Accepts any registered image size name, or an array of
* width and height values in pixels (in that order). Default 'medium'.
- * @param array $image_meta Optional. The image meta data as returned by 'wp_get_attachment_metadata()'.
+ * @param array|null $image_meta Optional. The image meta data as returned by 'wp_get_attachment_metadata()'.
* Default null.
* @return string|false A 'srcset' value string or false.
*/
@@ -1489,7 +1500,7 @@ function wp_calculate_image_srcset( $size_array, $image_src, $image_meta, $attac
* @param int $attachment_id Image attachment ID.
* @param string|int[] $size Optional. Image size. Accepts any registered image size name, or an array of
* width and height values in pixels (in that order). Default 'medium'.
- * @param array $image_meta Optional. The image meta data as returned by 'wp_get_attachment_metadata()'.
+ * @param array|null $image_meta Optional. The image meta data as returned by 'wp_get_attachment_metadata()'.
* Default null.
* @return string|false A valid source size value for use in a 'sizes' attribute or false.
*/
@@ -1520,8 +1531,8 @@ function wp_get_attachment_image_sizes( $attachment_id, $size = 'medium', $image
*
* @param string|int[] $size Image size. Accepts any registered image size name, or an array of
* width and height values in pixels (in that order).
- * @param string $image_src Optional. The URL to the image file. Default null.
- * @param array $image_meta Optional. The image meta data as returned by 'wp_get_attachment_metadata()'.
+ * @param string|null $image_src Optional. The URL to the image file. Default null.
+ * @param array|null $image_meta Optional. The image meta data as returned by 'wp_get_attachment_metadata()'.
* Default null.
* @param int $attachment_id Optional. Image attachment ID. Either `$image_meta` or `$attachment_id`
* is needed when using the image size name as argument for `$size`. Default 0.
@@ -2117,6 +2128,13 @@ function wp_img_tag_add_width_and_height_attr( $image, $context, $attachment_id
$size_array = wp_image_src_get_dimensions( $image_src, $image_meta, $attachment_id );
if ( $size_array ) {
+ // If the width is enforced through style (e.g. in an inline image), calculate the dimension attributes.
+ $style_width = preg_match( '/style="width:\s*(\d+)px;"/', $image, $match_width ) ? (int) $match_width[1] : 0;
+ if ( $style_width ) {
+ $size_array[1] = (int) round( $size_array[1] * $style_width / $size_array[0] );
+ $size_array[0] = $style_width;
+ }
+
$hw = trim( image_hwstring( $size_array[0], $size_array[1] ) );
return str_replace( '<img', "<img {$hw}", $image );
}
@@ -2347,10 +2365,6 @@ add_shortcode( 'caption', 'img_caption_shortcode' );
* @return string HTML content to display the caption.
*/
function img_caption_shortcode( $attr, $content = '' ) {
- if ( ! $attr ) {
- $attr = array();
- }
-
// New-style shortcode with the caption inside the shortcode with the link and image tags.
if ( ! isset( $attr['caption'] ) ) {
if ( preg_match( '#((?:<a [^>]+>\s*)?<img [^>]+>(?:\s*</a>)?)(.*)#is', $content, $matches ) ) {
@@ -2608,7 +2622,7 @@ function gallery_shortcode( $attr ) {
}
} elseif ( ! empty( $atts['exclude'] ) ) {
$post_parent_id = $id;
- $attachments = get_children(
+ $attachments = get_children(
array(
'post_parent' => $id,
'exclude' => $atts['exclude'],
@@ -2621,7 +2635,7 @@ function gallery_shortcode( $attr ) {
);
} else {
$post_parent_id = $id;
- $attachments = get_children(
+ $attachments = get_children(
array(
'post_parent' => $id,
'post_status' => 'inherit',
@@ -2636,10 +2650,10 @@ function gallery_shortcode( $attr ) {
if ( ! empty( $post_parent_id ) ) {
$post_parent = get_post( $post_parent_id );
- // terminate the shortcode execution if user cannot read the post or password-protected
- if (
- ( ! is_post_publicly_viewable( $post_parent->ID ) && ! current_user_can( 'read_post', $post_parent->ID ) )
- || post_password_required( $post_parent ) ) {
+ // Terminate the shortcode execution if the user cannot read the post or it is password-protected.
+ if ( ! is_post_publicly_viewable( $post_parent->ID ) && ! current_user_can( 'read_post', $post_parent->ID )
+ || post_password_required( $post_parent )
+ ) {
return '';
}
}
@@ -2979,7 +2993,7 @@ function wp_playlist_shortcode( $attr ) {
if ( ! empty( $args['post_parent'] ) ) {
$post_parent = get_post( $id );
- // terminate the shortcode execution if user cannot read the post or password-protected
+ // Terminate the shortcode execution if the user cannot read the post or it is password-protected.
if ( ! current_user_can( 'read_post', $post_parent->ID ) || post_password_required( $post_parent ) ) {
return '';
}
@@ -3064,7 +3078,7 @@ function wp_playlist_shortcode( $attr ) {
list( $src, $width, $height ) = wp_get_attachment_image_src( $thumb_id, 'thumbnail' );
$track['thumb'] = compact( 'src', 'width', 'height' );
} else {
- $src = wp_mime_type_icon( $attachment->ID );
+ $src = wp_mime_type_icon( $attachment->ID, '.svg' );
$width = 48;
$height = 64;
$track['image'] = compact( 'src', 'width', 'height' );
@@ -4093,6 +4107,7 @@ function _wp_image_editor_choose( $args = array() ) {
require_once ABSPATH . WPINC . '/class-wp-image-editor.php';
require_once ABSPATH . WPINC . '/class-wp-image-editor-gd.php';
require_once ABSPATH . WPINC . '/class-wp-image-editor-imagick.php';
+ require_once ABSPATH . WPINC . '/class-avif-info.php';
/**
* Filters the list of image editing library classes.
*
@@ -4133,7 +4148,7 @@ function _wp_image_editor_choose( $args = array() ) {
! call_user_func( array( $implementation, 'supports_mime_type' ), $args['output_mime_type'] )
) {
/*
- * This implementation supports the imput type but not the output type.
+ * This implementation supports the input type but not the output type.
* Keep looking to see if we can find an implementation that supports both.
*/
$supports_input = $implementation;
@@ -4197,6 +4212,11 @@ function wp_plupload_default_settings() {
$defaults['webp_upload_error'] = true;
}
+ // Check if AVIF images can be edited.
+ if ( ! wp_image_editor_supports( array( 'mime_type' => 'image/avif' ) ) ) {
+ $defaults['avif_upload_error'] = true;
+ }
+
/**
* Filters the Plupload default settings.
*
@@ -4330,7 +4350,7 @@ function wp_prepare_attachment_for_js( $attachment ) {
'mime' => $attachment->post_mime_type,
'type' => $type,
'subtype' => $subtype,
- 'icon' => wp_mime_type_icon( $attachment->ID ),
+ 'icon' => wp_mime_type_icon( $attachment->ID, '.svg' ),
'dateFormatted' => mysql2date( __( 'F j, Y' ), $attachment->post_date ),
'nonces' => array(
'update' => false,
@@ -4501,7 +4521,7 @@ function wp_prepare_attachment_for_js( $attachment ) {
list( $src, $width, $height ) = wp_get_attachment_image_src( $id, 'thumbnail' );
$response['thumb'] = compact( 'src', 'width', 'height' );
} else {
- $src = wp_mime_type_icon( $attachment->ID );
+ $src = wp_mime_type_icon( $attachment->ID, '.svg' );
$width = 48;
$height = 64;
$response['image'] = compact( 'src', 'width', 'height' );
@@ -5473,6 +5493,7 @@ function wp_show_heic_upload_error( $plupload_settings ) {
*
* @since 5.7.0
* @since 5.8.0 Added support for WebP images.
+ * @since 6.5.0 Added support for AVIF images.
*
* @param string $filename The file path.
* @param array $image_info Optional. Extended image information (passed by reference).
@@ -5505,7 +5526,11 @@ function wp_getimagesize( $filename, array &$image_info = null ) {
}
}
- if ( false !== $info ) {
+ if (
+ ! empty( $info ) &&
+ // Some PHP versions return 0x0 sizes from `getimagesize` for unrecognized image formats, including AVIFs.
+ ! ( empty( $info[0] ) && empty( $info[1] ) )
+ ) {
return $info;
}
@@ -5534,11 +5559,76 @@ function wp_getimagesize( $filename, array &$image_info = null ) {
}
}
+ // For PHP versions that don't support AVIF images, extract the image size info from the file headers.
+ if ( 'image/avif' === wp_get_image_mime( $filename ) ) {
+ $avif_info = wp_get_avif_info( $filename );
+
+ $width = $avif_info['width'];
+ $height = $avif_info['height'];
+
+ // Mimic the native return format.
+ if ( $width && $height ) {
+ return array(
+ $width,
+ $height,
+ IMAGETYPE_AVIF,
+ sprintf(
+ 'width="%d" height="%d"',
+ $width,
+ $height
+ ),
+ 'mime' => 'image/avif',
+ );
+ }
+ }
+
// The image could not be parsed.
return false;
}
/**
+ * Extracts meta information about an AVIF file: width, height, bit depth, and number of channels.
+ *
+ * @since 6.5.0
+ *
+ * @param string $filename Path to an AVIF file.
+ * @return array {
+ * An array of AVIF image information.
+ *
+ * @type int|false $width Image width on success, false on failure.
+ * @type int|false $height Image height on success, false on failure.
+ * @type int|false $bit_depth Image bit depth on success, false on failure.
+ * @type int|false $num_channels Image number of channels on success, false on failure.
+ * }
+ */
+function wp_get_avif_info( $filename ) {
+ $results = array(
+ 'width' => false,
+ 'height' => false,
+ 'bit_depth' => false,
+ 'num_channels' => false,
+ );
+
+ if ( 'image/avif' !== wp_get_image_mime( $filename ) ) {
+ return $results;
+ }
+
+ // Parse the file using libavifinfo's PHP implementation.
+ require_once ABSPATH . WPINC . '/class-avif-info.php';
+
+ $handle = fopen( $filename, 'rb' );
+ if ( $handle ) {
+ $parser = new Avifinfo\Parser( $handle );
+ $success = $parser->parse_ftyp() && $parser->parse_file();
+ fclose( $handle );
+ if ( $success ) {
+ $results = $parser->features->primary_item_features;
+ }
+ }
+ return $results;
+}
+
+/**
* Extracts meta information about a WebP file: width, height, and type.
*
* @since 5.8.0