wp_image_add_srcset_and_sizes

Advertisement

Summery Summery

Adds ‘srcset’ and ‘sizes’ attributes to an existing ‘img’ element.

Syntax Syntax

wp_image_add_srcset_and_sizes( string $image, array $image_meta, int $attachment_id )

Parameters Parameters

$image

(string) (Required) An HTML 'img' element to be filtered.

$image_meta

(array) (Required) The image meta data as returned by 'wp_get_attachment_metadata()'.

$attachment_id

(int) (Required) Image attachment ID.

Return Return

(string) Converted 'img' element with 'srcset' and 'sizes' attributes added.

Source Source

File: wp-includes/media.php

					$match = true;
				}
			}

			if ( ! $match && ! empty( $image_meta['sizes'] ) ) {
				foreach ( $image_meta['sizes'] as $image_size_data ) {
					$relative_path = $dirname . $image_size_data['file'];

					if ( strrpos( $image_location, $relative_path ) === strlen( $image_location ) - strlen( $relative_path ) ) {
						$match = true;
						break;
					}
				}
			}
		}
	}

	/**
	 * Filter whether an image path or URI matches image meta.
	 *
	 * @since 5.5.0
	 *
	 * @param bool   $match          Whether the image relative path from the image meta
	 *                               matches the end of the URI or path to the image file.
	 * @param string $image_location Full path or URI to the tested image file.
	 * @param array  $image_meta     The image meta data as returned by 'wp_get_attachment_metadata()'.
	 * @param int    $attachment_id  The image attachment ID or 0 if not supplied.
	 */
	return apply_filters( 'wp_image_file_matches_image_meta', $match, $image_location, $image_meta, $attachment_id );
}

/**
 * Determines an image's width and height dimensions based on the source file.
 *
 * @since 5.5.0
 *
 * @param string $image_src     The image source file.
 * @param array  $image_meta    The image meta data as returned by 'wp_get_attachment_metadata()'.
 * @param int    $attachment_id Optional. The image attachment ID. Default 0.
 * @return array|false Array with first element being the width and second element being the height,
 *                     or false if dimensions cannot be determined.
 */
function wp_image_src_get_dimensions( $image_src, $image_meta, $attachment_id = 0 ) {
	if ( ! wp_image_file_matches_image_meta( $image_src, $image_meta, $attachment_id ) ) {
		return false;
	}

	// Is it a full size image?
	if ( strpos( $image_src, $image_meta['file'] ) !== false ) {
		return array(
			(int) $image_meta['width'],
			(int) $image_meta['height'],
		);
	}

	if ( ! empty( $image_meta['sizes'] ) ) {
		$src_filename = wp_basename( $image_src );

		foreach ( $image_meta['sizes'] as $image_size_data ) {
			if ( $src_filename === $image_size_data['file'] ) {
				return array(
					(int) $image_size_data['width'],
					(int) $image_size_data['height'],
				);
			}
		}
	}

	return false;
}

/**
 * Adds 'srcset' and 'sizes' attributes to an existing 'img' element.
 *
 * @since 4.4.0

Advertisement

Changelog Changelog

Changelog
Version Description
4.4.0 Introduced.

See also See also

Advertisement

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.