函数文档

wp_get_avif_info()

💡 云策文档标注

概述

wp_get_avif_info() 函数用于提取 AVIF 文件的元信息,包括宽度、高度、位深度和通道数。它通过解析文件并返回一个包含这些信息的数组,若失败则返回 false 值。

关键要点

  • 函数接受一个必需参数 $filename,表示 AVIF 文件的路径。
  • 返回一个数组,包含 width、height、bit_depth 和 num_channels 键,成功时为整数值,失败时为 false。
  • 内部首先检查文件是否为 AVIF 类型,使用 wp_get_image_mime() 验证。
  • 通过 libavifinfo 的 PHP 实现解析文件,使用 AvifinfoParser 类。
  • 在 WordPress 6.5.0 版本中引入。

代码示例

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 AvifinfoParser( $handle );
		$success = $parser->parse_ftyp() && $parser->parse_file();
		fclose( $handle );
		if ( $success ) {
			$results = $parser->features->primary_item_features;
		}
	}
	return $results;
}

📄 原文内容

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 AvifinfoParser( $handle );
		$success = $parser->parse_ftyp() && $parser->parse_file();
		fclose( $handle );
		if ( $success ) {
			$results = $parser->features->primary_item_features;
		}
	}
	return $results;
}

Changelog

Version Description
6.5.0 Introduced.