wp_image_src_get_dimensions() WordPress Function

The wp_image_src_get_dimensions() function allows you to get the width and height of an image. This is useful when you need to know the size of an image before you output it on a page.

wp_image_src_get_dimensions( string $image_src, array $image_meta, int $attachment_id ) #

Determines an image’s width and height dimensions based on the source file.


Parameters

$image_src

(string)(Required)The image source file.

$image_meta

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

$attachment_id

(int)(Optional) The image attachment ID. Default 0.


Top ↑

Return

(array|false) Array with first element being the width and second element being the height, or false if dimensions cannot be determined.


Top ↑

Source

File: wp-includes/media.php

function wp_image_src_get_dimensions( $image_src, $image_meta, $attachment_id = 0 ) {
	$dimensions = false;

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

	if ( ! $dimensions && ! 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'] ) {
				$dimensions = array(
					(int) $image_size_data['width'],
					(int) $image_size_data['height'],
				);

				break;
			}
		}
	}

	/**
	 * Filters the 'wp_image_src_get_dimensions' value.
	 *
	 * @since 5.7.0
	 *
	 * @param array|false $dimensions    Array with first element being the width
	 *                                   and second element being the height, or
	 *                                   false if dimensions could not be determined.
	 * @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 The image attachment ID. Default 0.
	 */
	return apply_filters( 'wp_image_src_get_dimensions', $dimensions, $image_src, $image_meta, $attachment_id );
}


Top ↑

Changelog

Changelog
VersionDescription
5.5.0Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by theĀ Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.

Show More
Show More