wp_get_avif_info()
Extracts metadata of the specified AVIF image. Such as: width, height, color depth and number of channels.
Analyzes the specified AVIF file and returns its technical characteristics as an array.
Part of the internal WordPress mechanism for working with modern image formats.
Mainly used as a helper for wp_getimagesize() in cases where the PHP version on the server does not have built-in AVIF support (before PHP 8.1).
To obtain image information it is recommended to use wp_getimagesize(), which will automatically determine how to handle the file and, if necessary, will use this function.
No Hooks.
Returns
Array.
array- Associative array with information about the AVIF image containing the following keys:
width(int|false): Image width in pixels.height(int|false): Image height in pixels.bit_depth(int|false): Color depth.num_channels(int|false): Number of channels.
false- when the file is not an AVIF image or an error occurred.
- Associative array with information about the AVIF image containing the following keys:
Usage
wp_get_avif_info( $filename );
- $filename(string) (required)
- Full path to the AVIF image file.
Examples
#1 Getting information about an AVIF file
This example shows how to use the function to get metadata from the example.avif file.
// Suppose there is a file 'image.avif' in the 'uploads' folder
$upload_dir = wp_upload_dir();
$file_path = $upload_dir['basedir'] . '/image.avif';
// Check that the file exists before getting information
if ( file_exists( $file_path ) ) {
$avif_info = wp_get_avif_info( $file_path );
if ( $avif_info['width'] ) {
printf(
'Image information: Width: %dpx, Height: %dpx, Bit depth: %d, Channels: %d',
$avif_info['width'],
$avif_info['height'],
$avif_info['bit_depth'],
$avif_info['num_channels']
);
} else {
echo 'Failed to get information. It may not be an AVIF file.';
}
} else {
echo 'File not found.';
}
Changelog
| Since 6.5.0 | Introduced. |