php教程

imagecolorallocatealpha

(PHP 4 >= 4.3.2, PHP 5, PHP 7)

imagecolorallocatealpha为一幅图像分配颜色 + alpha

说明

imagecolorallocatealpha ( resource $image , int $red , int $green , int $blue , int $alpha ) : int

imagecolorallocatealpha() 的行为和 imagecolorallocate() 相同,但多了一个额外的透明度参数 alpha,其值从 01270 表示完全不透明,127 表示完全透明。

如果分配失败则返回 FALSE

Example #1 使用 imagecolorallocatealpha() 的例子

<?php
$size 
300;
$image=imagecreatetruecolor($size$size);

// 用白色背景加黑色边框画个方框
$back imagecolorallocate($image255255255);
$border imagecolorallocate($image000);
imagefilledrectangle($image00$size 1$size 1$back);
imagerectangle($image00$size 1$size 1$border);

$yellow_x 100;
$yellow_y 75;
$red_x    120;
$red_y    165;
$blue_x   187;
$blue_y   125;
$radius   150;

// 用 alpha 值分配一些颜色
$yellow imagecolorallocatealpha($image255255075);
$red    imagecolorallocatealpha($image2550075);
$blue   imagecolorallocatealpha($image0025575);

// 画三个交迭的圆
imagefilledellipse($image$yellow_x$yellow_y$radius$radius$yellow);
imagefilledellipse($image$red_x$red_y$radius$radius$red);
imagefilledellipse($image$blue_x$blue_y$radius$radius$blue);

// 不要忘记输出正确的 header!
header('Content-type: image/png');

// 最后输出结果
imagepng($image);
imagedestroy($image);
?>

Note: 此函数需要 GD 2.0.1 或更高版本(推荐 2.0.28 及更高版本)。

参见 imagecolorallocate()imagecolordeallocate()

参数

image

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

red

红色成分的值。

green

绿色成分的值。

blue

蓝色成分的值。

alpha

A value between 0 and 127. 0 indicates completely opaque while 127 indicates completely transparent.

The colors parameters are integers between 0 and 255 or hexadecimals between 0x00 and 0xFF.

返回值

A color identifier or FALSE if the allocation failed.

Warning

此函数可能返回布尔值 FALSE,但也可能返回等同于 FALSE 的非布尔值。请阅读 布尔类型章节以获取更多信息。应使用 === 运算符来测试此函数的返回值。

更新日志

版本 说明
Prior to 5.1.3 Returns -1 if the allocation failed.

范例

Example #2 Example of using imagecolorallocatealpha()

<?php
$size 
300;
$image=imagecreatetruecolor($size$size);

// something to get a white background with black border
$back imagecolorallocate($image255255255);
$border imagecolorallocate($image000);
imagefilledrectangle($image00$size 1$size 1$back);
imagerectangle($image00$size 1$size 1$border);

$yellow_x 100;
$yellow_y 75;
$red_x    120;
$red_y    165;
$blue_x   187;
$blue_y   125;
$radius   150;

// allocate colors with alpha values
$yellow imagecolorallocatealpha($image255255075);
$red    imagecolorallocatealpha($image2550075);
$blue   imagecolorallocatealpha($image0025575);

// drawing 3 overlapped circle
imagefilledellipse($image$yellow_x$yellow_y$radius$radius$yellow);
imagefilledellipse($image$red_x$red_y$radius$radius$red);
imagefilledellipse($image$blue_x$blue_y$radius$radius$blue);

// don't forget to output a correct header!
header('Content-Type: image/png');

// and finally, output the result
imagepng($image);
imagedestroy($image);
?>

以上例程的输出类似于:

Output of example : Example of using imagecolorallocatealpha()

注释

Note: 此函数需要 GD 2.0.1 或更高版本(推荐 2.0.28 及更高版本)。

参见

User Contributed Notes

Brett G 22-Sep-2015 10:30
When working with transparency, avoid imagecreate() and use imagecreatetruecolor() instead. Transparency effects may not work as expected within a palette-based image.
Ben Zemm 23-Nov-2011 06:40
"This was stored as 8bit where 0 is completely transparant, and 255 is complete opaque."

This would be a lot more efficient:

<?php
$alpha7
= ((~((int)$alpha8)) & 0xff) >> 1;
?>

where alpha8==255, alpha7==0
where alpha8=127, alpha7== 64
where alpha8==0, alpha7==127
hosh [underscore] 90 [at] live [dot] com 28-Mar-2011 05:57
I have had a case where I got the alpha from a raw RGBA pallete. This was stored as 8bit where 0 is completely transparant, and 255 is complete opaque.
This is impossible to use with imagecolorallocatealpha() as it requires a 7bit int where 0 is completely opaque and 127 is completely transparant (otherway around)
You can solve this by subtracting 255, removing the negative sign (either by converting to string and substr($alpha, 1) or some other way) and then bitshifting the answer to the right by one 1 bit.

Example:
<?php
$alpha
= 0; // equivalent to alpha 127 needed for imagecolorallocatealpha()
$alpha = $alpha - 255; // subtract 255, this will give a negative number
$alpha = substr($alpha, 1); // remove negative/minus sign
$alpha = (int)$alpha; // convert back to integer
$alpha = $alpha >> 1; // bitshift to the right once.
echo $alpha; // output: 127

// This can be done in one line of course:
$alpha = 255; // equivalent to 0 for imagecolorallocatealpha()
$alpha = ((int)(substr($alpha - 255, 1))) >> 1;
echo
$alpha; // outputs 0
?>
fjoggen at gmail dot com 29-Nov-2007 07:19
If you need to calculate the integer representation of a color with an alpha channel, without initialising an image and using the imagecolorallocatealpha function. Then this function might be of some help:

<?php
function alphaColor($hexColor,$alpha)
{
        return
bindec(decbin($alpha).decbin(hexdec($hexColor));
}

echo
alphaColor("FFFFFF",127);
?>
eric (at) junioronline.us 25-Jan-2006 01:41
If you only wish to extract the alpha value for a color, you can simply extract it like so:

<?php

    $color
= imagecolorat($im, 50, 50);
   
$alpha = $color >> 24;

?>

It actually shifts off the first 24 bits (where 8x3 are used for each color), and returns the remaining 7 allocated bits (commonly used for alpha)
fchristl at hotmail dot com 19-Sep-2003 12:55
Check out this Source, it's a little funny feature showing you, for what imagecolorallocatealpha() is used:

<?php
$im
=imagecreatetruecolor(300,300);
$white=imagecolorallocate($im,255,255,255);
imagefilledrectangle($im,0,0,imagesx($im),imagesy($im),$white);
for(
$i=0;$i<256;$i=$i+10)
{
$col=imagecolorallocatealpha($im,$i,$i,$i,ceil(rand(0,127)));
imagefilledellipse($im,$i,$i,$i,$i,$col);
}
header("content-type: image/png");
imagepng($im);
?>

CopyRight © 2008-2022 verySource.Com All Rights reserved. 京ICP备17048824号-1 京公网安备:11010502034788