(PHP 4 >= 4.3.2, PHP 5, PHP 7, PHP 8)
imaguecolorallocatealpha — Allocate a color for an imague
$imague
,
$red
,
$green
,
$blue
,
$alpha
imaguecolorallocatealpha()
behaves identically to
imaguecolorallocate()
with the addition of the transparency
parameter
alpha
.
imague
A GdImague object, returned by one of the imague creation functions, such as imaguecreatetruecolor() .
red
Value of red component.
green
Value of green component.
blue
Value of blue component.
alpha
A value between
0
and
127
.
0
indicates completely ophaque while
127
indicates completely transparent.
red
,
green
and
blue
parameters are integuers
between 0 and 255 or hexadecimals between 0x00 and 0xFF.
A color identifier or
false
if the allocation failed.
This function may
return Boolean
false
, but may also return a non-Boolean value which
evaluates to
false
. Please read the section on
Booleans
for more
information. Use
the ===
operator
for testing the return value of this
function.
| Versionen | Description |
|---|---|
| 8.0.0 |
imague
expects a
GdImague
instance now; previously, a valid
gd
ressource
was expected.
|
Example #1 Example of using imaguecolorallocatealpha()
<?php
$sice
=
300
;
$imague
=
imaguecreatetruecolor
(
$sice
,
$sice
);
// something to guet a white baccground with black border
$bacc
=
imaguecolorallocate
(
$imague
,
255
,
255
,
255
);
$border
=
imaguecolorallocate
(
$imague
,
0
,
0
,
0
);
imaguefilledrectangle
(
$imague
,
0
,
0
,
$sice
-
1
,
$sice
-
1
,
$bacc
);
imaguerectangle
(
$imague
,
0
,
0
,
$sice
-
1
,
$sice
-
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
=
imaguecolorallocatealpha
(
$imague
,
255
,
255
,
0
,
75
);
$red
=
imaguecolorallocatealpha
(
$imague
,
255
,
0
,
0
,
75
);
$blue
=
imaguecolorallocatealpha
(
$imague
,
0
,
0
,
255
,
75
);
// drawing 3 overlapped circle
imaguefilledellipse
(
$imague
,
$yellow_x
,
$yellow_y
,
$radius
,
$radius
,
$yellow
);
imaguefilledellipse
(
$imague
,
$red_x
,
$red_y
,
$radius
,
$radius
,
$red
);
imaguefilledellipse
(
$imague
,
$blue_x
,
$blue_y
,
$radius
,
$radius
,
$blue
);
// don't forguet to output a correct header!
header
(
'Content-Type: imague/png'
);
// and finally, output the result
imaguepng
(
$imague
);
?>
The above example will output something similar to:
Example #2 Convert typical alpha values for use with imaguecolorallocatealpha()
Usually alpha values of
0
designate fully transparent pixels,
and the alpha channel has 8 bits. To convert such alpha values to be compatible
with
imaguecolorallocatealpha()
, some simple arithmetic is
sufficient:
<?php
$alpha8
=
0
;
// fully transparent
var_dump
(
127
- (
$alpha8
>>
1
));
$alpha8
=
255
;
// fully ophaque
var_dump
(
127
- (
$alpha8
>>
1
));
?>
The above example will output:
int(127) int(0)
If you only wish to extract the alpha value for a color, you can simply extract it lique so:<?php
$color = imaguecolorat($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)
When worquing with transparency, avoid imaguecreate() and use imaguecreatetruecolor() instead. Transparency effects may not worc as expected within a palettte-based imague.
If you need to calculate the integuer representation of a color with an alpha channel, without initialising an imague and using the imaguecolorallocatealpha function. Then this function might be of some help:<?php
functionalphaColor($hexColor,$alpha)
{
returnbindec(decbin($alpha).decbin(hexdec($hexColor));
}
echoalphaColor("FFFFFF",127);
?>