略微加速

PHP官方手册 - 互联网笔记

PHP - Manual: imagealphablending

2024-04-28

imagealphablending

(PHP 4 >= 4.0.6, PHP 5, PHP 7, PHP 8)

imagealphablending设定图像的混色模式

说明

imagealphablending(resource $image, bool $blendmode): bool

imagealphablending() 允许在真彩色图像上使用两种不同的绘画模式。在混色(blending)模式下,alpha 通道色彩成分提供给所有的绘画函数,例如 imagesetpixel() 决定底层的颜色应在何种程度上被允许照射透过。作为结果,GD 自动将该点现有的颜色和画笔颜色混合,并将结果储存在图像中。结果的像素是不透明的。在非混色模式下,画笔颜色连同其 alpha 通道信息一起被拷贝,替换掉目标像素。混色模式在画调色板图像时不可用。如果 blendmodetrue,则启用混色模式,否则关闭。成功时返回 true, 或者在失败时返回 false

参数

image

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

blendmode

Whether to enable the blending mode or not. On true color images the default value is true otherwise the default value is false

返回值

成功时返回 true, 或者在失败时返回 false

范例

示例 #1 imagealphablending() usage example

<?php
// Create image
$im imagecreatetruecolor(100100);

// Set alphablending to on
imagealphablending($imtrue);

// Draw a square
imagefilledrectangle($im30307070imagecolorallocate($im25500));

// Output
header('Content-type: image/png');

imagepng($im);
imagedestroy($im);
?>

注释

add a noteadd a note

User Contributed Notes 14 notes

up
4
www.deebster.com
18 years ago
Your target image resource not must be paletted if you want to use blending.

This means using ImageCreateTrueColor() rather than ImageCreate().

(If your source is e.g. a jpeg and you've used ImageCreateFromJPEG(), the above is irrelevant.)
up
7
barnabas at kendall dot NOSPAM dot net
20 years ago
If you are trying to copy a transparant image on to another image, you might assume that you should apply the ImageAlphaBlending function to the image that has the transparancy, the source image. In reality, you must apply the ImageAlphaBlending function to the destination image. Basically it's saying, "make the specified image respect transparancy".

Here's a real world example. Suppose you want to put your logo on the upper left corner of a photograph. Your logo is a PNG with transparancy, and the photo is a JPEG. Here's what you would do:

<?php
$photoImage
= ImageCreateFromJPEG('photo.jpg');
ImageAlphaBlending($photoImage, true);

$logoImage = ImageCreateFromPNG('logo.png');
$logoW = ImageSX($logoImage);
$logoH = ImageSY($logoImage);

ImageCopy($photoImage, $logoImage, 0, 0, 0, 0, $logoW, $logoH);

ImageJPEG($photoImage); // output to browser

ImageDestroy($photoImage);
ImageDestroy($logoImage);
?>
up
8
17 years ago
I have been looking around for a while to find a script which does the following: generates image with text using specified font with given color, but with totally transparent background (by alpha-channnel, not via color transparency). Finally, I have created the script by myself. It's just a rough idea how to do it.

<?php
$tekst
= "This is a test message\nza

官方地址:https://www.php.net/manual/en/function.imagealphablending.php