wp_get_webp_info( string $filename ): array
- Since
- 5.8.0
- Source
wp-includes/media.php:6073
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|falseImage width on success, false on failure.$heightint|falseImage height on success, false on failure.$typestring|falseThe 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
- Scaling
- Constant
- Instructions
- 15–66
- Plugin surface
- None
- Called by
- 3
Reads or writes the filesystem via file_get_contents().
No loop in the body: the same number of instructions runs whatever you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 131.
Nothing here hands control to plugin code.
3 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- filesystemfilesystem access
file_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.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 15 | wp_get_image_mime(), compact() |
| always | 25–39 | wp_get_image_mime(), file_get_contents(), compact() |
$magic !== false | 49–66 | wp_get_image_mime(), file_get_contents(), unpack(), compact() |
$magic !== false | 59–65 | wp_get_image_mime(), file_get_contents(), unpack(), unpack(), compact() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 131 | 15–66 | 7 | |
| 8.5 | 131 | 15–66 | 7 | |
| 8.4 | 131 | 15–66 | 7 | 15 fewer instructions than PHP 8.3 |
| 8.3 | 146 | 15–74 | 7 | |
| 8.2 | 146 | 15–74 | 7 | 1 more instruction than PHP 8.1 |
| 8.1 | 145 | 15–74 | 7 | |
| 7.4 | 145 | 15–74 | 7 |
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
- wp_get_image_mime()Returns the real mime type of an image file.
Used by · 3
- WP_Image_Editor_GD::set_quality()Sets Image Compression quality on a 1-100% scale. Handles WebP lossless images.
- WP_Image_Editor_Imagick::set_quality()Sets Image Compression quality on a 1-100% scale.
- wp_getimagesize()Allows PHP's getimagesize() to be debuggable when necessary.
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.
Signature, return type and hooks compared across 5 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 7.1.0 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.