函数文档

wp_imagecreatetruecolor()

💡 云策文档标注

概述

wp_imagecreatetruecolor() 是 WordPress 中用于创建支持透明度的 GD 图像资源的函数。它基于 PHP 的 imagecreatetruecolor() 函数,并添加了透明度处理功能。

关键要点

  • 函数接受两个必需参数:$width(图像宽度,像素)和 $height(图像高度,像素)。
  • 返回值为 GD 图像资源、GdImage 实例(成功时)或 false(失败时)。
  • 内部实现中,如果 GD 图像有效且相关函数存在,会调用 imagealphablending() 和 imagesavealpha() 来启用透明度支持。
  • 该函数自 WordPress 2.9.0 版本引入,常用于图像编辑操作,如 WP_Image_Editor_GD 类中的 _resize()、crop() 和 flip() 方法。

代码示例

function wp_imagecreatetruecolor( $width, $height ) {
	$img = imagecreatetruecolor( $width, $height );

	if ( is_gd_image( $img )
		&& function_exists( 'imagealphablending' ) && function_exists( 'imagesavealpha' )
	) {
		imagealphablending( $img, false );
		imagesavealpha( $img, true );
	}

	return $img;
}

📄 原文内容

Creates a new GD image resource with transparency support.

Parameters

$widthintrequired
Image width in pixels.
$heightintrequired
Image height in pixels.

Return

resource|GdImage|false The GD image resource or GdImage instance on success.
False on failure.

Source

function wp_imagecreatetruecolor( $width, $height ) {
	$img = imagecreatetruecolor( $width, $height );

	if ( is_gd_image( $img )
		&& function_exists( 'imagealphablending' ) && function_exists( 'imagesavealpha' )
	) {
		imagealphablending( $img, false );
		imagesavealpha( $img, true );
	}

	return $img;
}

Changelog

Version Description
2.9.0 Introduced.