Directory

wp_get_avif_info() – Function | Developer.WordPress.org

wp_get_avif_info( string $filename ): array

In this article

Extracts meta information about an AVIF file: width, height, bit depth, and number of channels.

Parameters

$filenamestringrequired
Path to an AVIF file.

Return

array An array of AVIF image information.
  • width int|false
    Image width on success, false on failure.
  • height int|false
    Image height on success, false on failure.
  • bit_depth int|false
    Image bit depth on success, false on failure.
  • num_channels int|false
    Image number of channels on success, false on failure.

Source

function wp_get_avif_info( $filename ) {
	$results = array(
		'width'        => false,
		'height'       => false,
		'bit_depth'    => false,
		'num_channels' => false,
	);

	if ( 'image/avif' !== wp_get_image_mime( $filename ) ) {
		return $results;
	}

	// Parse the file using libavifinfo's PHP implementation.
	require_once ABSPATH . WPINC . '/class-avif-info.php';

	$handle = fopen( $filename, 'rb' );
	if ( $handle ) {
		$parser  = new Avifinfo\Parser( $handle );
		$success = $parser->parse_ftyp() && $parser->parse_file();
		fclose( $handle );
		if ( $success ) {
			$results = $parser->features->primary_item_features;
		}
	}
	return $results;
}

Changelog

VersionDescription
6.5.0Introduced.

User Contributed Notes

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