On this page
wp_image_src_get_dimensions( string $image_src, array $image_meta, int $attachment_id ): array|false
Determines an image’s width and height dimensions based on the source file.
Parameters
$image_srcstring Required-
The image source file.
$image_metaarray Required-
The image meta data as returned by 'wp_get_attachment_metadata() '.
$attachment_idint 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.
Source
File: wp-includes/media.php. View all references
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 );
}
Hooks
- apply_filters( 'wp_image_src_get_dimensions',
array|false $dimensions ,string $image_src ,array $image_meta ,int $attachment_id ) -
Filters the ‘wp_image_src_get_dimensions’ value.
Related
Uses
| Uses | Description |
|---|---|
| wp_basename() wp-includes/formatting.php | i18n-friendly version of basename(). |
| apply_filters() wp-includes/plugin.php | Calls the callback functions that have been added to a filter hook. |
Used By
| Used By | Description |
|---|---|
| wp_img_tag_add_width_and_height_attr() wp-includes/media.php | Adds |
| wp_image_add_srcset_and_sizes() wp-includes/media.php | Adds ‘srcset’ and ‘sizes’ attributes to an existing ‘img’ element. |
Changelog
| Version | Description |
|---|---|
| 5.5.0 | Introduced. |
© 2003–2022 WordPress Foundation
Licensed under the GNU GPLv2+ License.
https://developer.wordpress.org/reference/functions/wp_image_src_get_dimensions