imagecolortransparent

(PHP 4, PHP 5, PHP 7, PHP 8)

imagecolortransparent将颜色定义为透明

说明

imagecolortransparent(GdImage $image, ?int $color = null): int

获取或设置指定 image 中的透明色。

参数

image

由图象创建函数(例如imagecreatetruecolor())返回的 GdImage 对象。

color

颜色标识符使用 imagecolorallocate() 创建。

返回值

返回新(或当前,如果未指定)透明色的标识符。如果 colornull,并且图像没有透明色,则返回的标识符为 -1

更新日志

版本 说明
8.0.0 image 现在需要 GdImage 实例;之前需要有效的 gd resource
8.0.0 color 现在允许为 null。

范例

示例 #1 imagecolortransparent() 示例

<?php
// Create a 55x30 image
$im = imagecreatetruecolor(55, 30);
$red = imagecolorallocate($im, 255, 0, 0);
$black = imagecolorallocate($im, 0, 0, 0);

// Make the background transparent
imagecolortransparent($im, $black);

// Draw a red rectangle
imagefilledrectangle($im, 4, 4, 50, 25, $red);

// Save the image
imagepng($im, './imagecolortransparent.png');
imagedestroy($im);
?>

以上例程的输出类似于:

示例输出:imagecolortransparent()

注释

注意:

透明度仅能使用 imagecopymerge() 和真彩色图像复制,而不使用 imagecopy() 或调色板图像。

注意:

透明色是图像的属性,透明度不是颜色的属性。一旦设定某个颜色为透明色,图像中之前绘制为该颜色的任何区域都成为透明的。

add a note

User Contributed Notes

There are no user contributed notes for this page.
To Top