wp_get_webp_info( string $filename ): array

In this article

Extracts meta information about a WebP file: width, height, and type.

Parameters

$filenamestringrequired
Path to a WebP file.

Return

array An array of WebP image information.
  • width int|false
    Image width on success, false on failure.
  • height int|false
    Image height on success, false on failure.
  • type string|false
    The WebP type: one of 'lossy', 'lossless' or 'animated-alpha'.
    False on failure.

Source

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

$image_mime_type = wp_get_image_mime( $filename );

// Not an image?
if ( false === $image_mime_type ) {
	return false;
}

/*
 * For PHP versions that don't support WebP images,
 * extract the image size info from the file headers.
 */
if ( 'image/webp' === $image_mime_type ) {
	$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' === $image_mime_type ) {
	$avif_info = wp_get_avif_info( $filename );

	$width  = $avif_info['width'];
	$height = $avif_info['height'];

Changelog

VersionDescription
5.8.0Introduced.

User Contributed Notes

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