wppaste
WordPress

wp_get_webp_info( string $filename ): array

Since
5.8.0
Source
wp-includes/media.php:5904
Extracts meta information about a WebP file: width, height, and type.

Compatibility

WordPress
since 5.8.0
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.

Parameters

$filenamestring
Path to a WebP file.

Return value

array
An array of WebP image information.
  • $widthint|false

    Image width on success, false on failure.
  • $heightint|false

    Image height on success, false on failure.
  • $typestring|false

    The WebP type: one of 'lossy', 'lossless' or 'animated-alpha'. False on failure.

Performance profile

How much work a call to wp_get_webp_info() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.

Cost class
Heavy

Reads or writes the filesystem via file_get_contents().

Scaling
Constant

No loop in the body: the same number of instructions runs whatever you pass in.

Instructions
15–66

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 131.

Plugin surface
None

Nothing here hands control to plugin code.

Called by
3

3 places in core call this, so the cost is paid more often than your own code shows.

What it touches

  • filesystemfilesystem accessfile_get_contents()called directly

What one call costs · 4 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost wp_get_webp_info() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
always15wp_get_image_mime(), compact()
always25–39wp_get_image_mime(), file_get_contents(), compact()
$magic !== false49–66wp_get_image_mime(), file_get_contents(), unpack(), compact()
$magic !== false59–65wp_get_image_mime(), file_get_contents(), unpack(), unpack(), compact()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev13115–667
8.513115–667
8.413115–66715 fewer instructions than PHP 8.3
8.314615–747
8.214615–7471 more instruction than PHP 8.1
8.114515–747
7.414515–747

An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.

Uses · 1

Used by · 3

Source code

function wp_get_webp_info( $filename ) {	$width  = false;	$height = false;	$type   = false; 	if ( 'image/webp' !== wp_get_image_mime( $filename ) ) {		return compact( 'width', 'height', 'type' );	} 	$magic = file_get_contents( $filename, false, null, 0, 40 ); 	if ( false === $magic ) {		return compact( 'width', 'height', 'type' );	} 	// Make sure we got enough bytes.	if ( strlen( $magic ) < 40 ) {		return compact( 'width', 'height', 'type' );	} 	/*	 * The headers are a little different for each of the three formats.	 * Header values based on WebP docs, see https://developers.google.com/speed/webp/docs/riff_container.	 */	switch ( substr( $magic, 12, 4 ) ) {		// Lossy WebP.		case 'VP8 ':			$parts  = unpack( 'v2', substr( $magic, 26, 4 ) );			$width  = (int) ( $parts[1] & 0x3FFF );			$height = (int) ( $parts[2] & 0x3FFF );			$type   = 'lossy';			break;		// Lossless WebP.		case 'VP8L':			$parts  = unpack( 'C4', substr( $magic, 21, 4 ) );			$width  = (int) ( $parts[1] | ( ( $parts[2] & 0x3F ) << 8 ) ) + 1;			$height = (int) ( ( ( $parts[2] & 0xC0 ) >> 6 ) | ( $parts[3] << 2 ) | ( ( $parts[4] & 0x03 ) << 10 ) ) + 1;			$type   = 'lossless';			break;		// Animated/alpha WebP.		case 'VP8X':			// Pad 24-bit int.			$width = unpack( 'V', substr( $magic, 24, 3 ) . "\x00" );			$width = (int) ( $width[1] & 0xFFFFFF ) + 1;			// Pad 24-bit int.			$height = unpack( 'V', substr( $magic, 27, 3 ) . "\x00" );			$height = (int) ( $height[1] & 0xFFFFFF ) + 1;			$type   = 'animated-alpha';			break;	} 	return compact( 'width', 'height', 'type' );}

Changelog

Introduced in 5.8.0. Unchanged from 6.7.7 through 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

About this page

Parsed data
Generated from the wordpress-develop 6.9.7 tag, from src/wp-includes/media.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.