wp_getimagesize( string $filename, array $image_info = null ): array|false

Allows PHP’s getimagesize() to be debuggable when necessary.

Parameters

$filenamestringrequired
The file path.
$image_infoarrayoptional
Extended image information (passed by reference).

Default:null

Return

array|false Array of image information or false on failure.

Source

 * @param string $filename   The file path.
 * @param array  $image_info Optional. Extended image information (passed by reference).
 * @return array|false Array of image information or false on failure.
 */
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() ) {
			$info = @getimagesize( $filename, $image_info );
		} else {
			$info = @getimagesize( $filename );
		}
	}

	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;
	}

	/*
	 * 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',
			);
		}
	}

	// 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',
			);
		}
	}

Changelog

VersionDescription
6.5.0Added support for AVIF images.
5.8.0Added support for WebP images.
5.7.0Introduced.

User Contributed Notes

  1. Skip to note 2 content

    Usage (if you only have image URL):

    <?php
    $src          = 'https://example.com/path/to/image.jpg';
    $upload_dir   = wp_upload_dir();
    $image_path   = str_replace( $upload_dir[ 'baseurl' ], $upload_dir[ 'basedir' ], $src );
    $getimagesize = wp_getimagesize( $image_path );
    ?>
    
    <img <?php echo $getimagesize[3]; ?> src="<?php echo esc_url( $src ); ?>" />

    Returns array of data:

    $getimagesize[0]          = The width of the image.
    $getimagesize[1]          = The height of the image.
    $getimagesize[2]          = One of the IMAGETYPE_ constants indicating the type of the image.
    $getimagesize[3]          = A text string with the correct height="yyy" width="xxx" string that can be used directly in an IMG tag.
    $getimagesize['mime']     = The correspondent MIME type of the image. This information can be used to deliver images with the correct HTTP Content-type header.
    $getimagesize['channels'] = 3 for RGB pictures and 4 for CMYK pictures.
    $getimagesize['bits']     = The number of bits for each color.
    
    Array
    (
        [0] => 440
        [1] => 95
        [2] => 3
        [3] => width="440" height="95"
        [bits] => 8
        [mime] => image/png
    )

You must log in before being able to contribute a note or feedback.