wp_getimagesize() WordPress Function
The wp_getimagesize() function is used to retrieve the dimensions of an image. This is useful for displaying images in a grid or for sizing images on a page. The function takes the image URL as an argument and returns an array with the width, height, and type of image.
wp_getimagesize( string $filename, array $image_info = null ) #
Allows PHP’s getimagesize() to be debuggable when necessary.
Parameters
- $filename
(string)(Required)The file path.
- $image_info
(array)(Optional) Extended image information (passed by reference).
Default value: null
Return
(array|false) Array of image information or false on failure.
Source
File: wp-includes/media.php
function wp_getimagesize( $filename, array &$image_info = null ) {
// Don't silence errors when in debug mode, unless running unit tests.
if ( defined( 'WP_DEBUG' ) && WP_DEBUG
&& ! defined( 'WP_RUN_CORE_TESTS' )
) {
if ( 2 === func_num_args() ) {
$info = getimagesize( $filename, $image_info );
} else {
$info = getimagesize( $filename );
}
} else {
/*
* Silencing notice and warning is intentional.
*
* getimagesize() has a tendency to generate errors, such as
* "corrupt JPEG data: 7191 extraneous bytes before marker",
* even when it's able to provide image size information.
*
* See https://core.trac.wordpress.org/ticket/42480
*/
if ( 2 === func_num_args() ) {
// phpcs:ignore WordPress.PHP.NoSilencedErrors
$info = @getimagesize( $filename, $image_info );
} else {
// phpcs:ignore WordPress.PHP.NoSilencedErrors
$info = @getimagesize( $filename );
}
}
if ( false !== $info ) {
return $info;
}
// For PHP versions that don't support WebP images,
// extract the image size info from the file headers.
if ( 'image/webp' === wp_get_image_mime( $filename ) ) {
$webp_info = wp_get_webp_info( $filename );
$width = $webp_info['width'];
$height = $webp_info['height'];
// Mimic the native return format.
if ( $width && $height ) {
return array(
$width,
$height,
IMAGETYPE_WEBP,
sprintf(
'width="%d" height="%d"',
$width,
$height
),
'mime' => 'image/webp',
);
}
}
// The image could not be parsed.
return false;
}
Expand full source codeCollapse full source codeView on TracView on GitHub
Changelog
| Version | Description |
|---|---|
| 5.8.0 | Added support for WebP images. |
| 5.7.0 | Introduced. |