函数文档

is_gd_image()

💡 云策文档标注

概述

is_gd_image() 函数用于检查传入值是否为 GD 图像函数可接受的类型,兼容 PHP 8.0 中的 GdImage 对象和旧版资源类型。

关键要点

  • 检查 $image 是否为 GdImage 对象实例或资源类型为 'gd' 的资源
  • 返回布尔值:true 表示是有效 GD 图像,false 表示不是
  • 在 WordPress 5.6.0 中引入,主要用于图像编辑和处理相关函数

代码示例

function is_gd_image( $image ) {
	if ( $image instanceof GdImage
		|| is_resource( $image ) && 'gd' === get_resource_type( $image )
	) {
		return true;
	}

	return false;
}

📄 原文内容

Determines whether the value is an acceptable type for GD image functions.

Description

In PHP 8.0, the GD extension uses GdImage objects for its data structures.
This function checks if the passed value is either a GdImage object instance or a resource of type gd. Any other type will return false.

Parameters

$imageresource|GdImage|falserequired
A value to check the type for.

Return

bool True if $image is either a GD image resource or a GdImage instance, false otherwise.

Source

function is_gd_image( $image ) {
	if ( $image instanceof GdImage
		|| is_resource( $image ) && 'gd' === get_resource_type( $image )
	) {
		return true;
	}

	return false;
}

Changelog

Version Description
5.6.0 Introduced.