Allocate a color for an image
Syntax
imagecolorallocatealpha( GdImage $image, int $red, int $green, int $blue, int $alpha ): int|false
Parameters
image
A GdImage object, returned by one of the image creation functions, such as imagecreatetruecolor().
red
Value of red component.
green
Value of green component.
blue
Value of blue component.
alpha
A value between 0 and 127. 0 is completely opaque and 127 is completely transparent.
Return
A color identifier or false if the allocation failed.
Warning: This function may return Boolean false, but may also return a non-Boolean value which evaluates to false. Use the === operator for testing the return value of this function.
Examples
1 · red green blue · decimal
<? $width = 100; $height = 100; $image = imagecreate($width, $height); $red = 255; $green = 255; $blue = 255; $alpha = 64; $return = imagecolorallocatealpha($image, $red, $green, $blue, $alpha); echo $return; ?>
0
2 · red green blue · hexadecimal
<? $width = 100; $height = 100; $image = imagecreate($width, $height); $red = 0xff; $green = 0xff; $blue = 0xff; $alpha = 64; $return = imagecolorallocatealpha($image, $red, $green, $blue, $alpha); echo $return; ?>
0
3 · return
<? $width = 100; $height = 100; $image = imagecreate($width, $height); $red = 255; $green = 255; $blue = 255; $alpha = 64; $background = imagecolorallocatealpha($image, $red, $green, $blue, $alpha); $red = 255; $green = 0; $blue = 0; $alpha = 64; $r = imagecolorallocatealpha($image, $red, $green, $blue, $alpha); $red = 0; $green = 255; $blue = 0; $alpha = 64; $g = imagecolorallocatealpha($image, $red, $green, $blue, $alpha); $red = 0; $green = 0; $blue = 255; $alpha = 64; $b = imagecolorallocatealpha($image, $red, $green, $blue, $alpha); echo $background . $r . $g . $b; ?>
0123
4 · base64
<? $width = 100; $height = 100; $image = imagecreatetruecolor($width, $height); $enable = true; imagesavealpha($image, $enable); $red = 255; $green = 255; $blue = 255; $alpha = 64; $background = imagecolorallocatealpha($image, $red, $green, $blue, $alpha); $x = 0; $y = 0; imagefill($image, $x, $y, $background); $red = 255; $green = 0; $blue = 0; $alpha = 64; $r = imagecolorallocatealpha($image, $red, $green, $blue, $alpha); $x1 = 0; $y1 = 0; $x2 = 50; $y2 = 50; imagefilledrectangle($image, $x1, $y1, $x2, $y2, $r); $red = 0; $green = 255; $blue = 0; $alpha = 64; $g = imagecolorallocatealpha($image, $red, $green, $blue, $alpha); $x1 = 25; $y1 = 25; $x2 = 75; $y2 = 75; imagefilledrectangle($image, $x1, $y1, $x2, $y2, $g); $red = 0; $green = 0; $blue = 255; $alpha = 64; $b = imagecolorallocatealpha($image, $red, $green, $blue, $alpha); $x1 = 50; $y1 = 50; $x2 = 100; $y2 = 100; imagefilledrectangle($image, $x1, $y1, $x2, $y2, $b); ob_start(); imagepng($image); $output = ob_get_clean(); echo '<!doctype html> <html> <body> <img src="data:image/png;base64,' . base64_encode($output) . '"> </body> </html>'; ?>
<!doctype html> <html> <body> <img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAGQAAABkCAYAAABw4pVUAAAACXBIWXMAAA7EAAAOxAGVKw4bAAABX0lEQVR4nO3csU0DQRBA0TNyATSA5FbowVcABRCS0goFXAGEZKS04ZQKjpicXX+s9xqYPX3tRaM97Ofzx3Irtu3ff8vdtQ/Ab4LECBIjSIwgMYLECBIjSIwgMYLECBIjSIwgMYLECBIjSIwgMYLECBIjSIwgMYLECBIjSMxxxpDnp6/TjDmXZX0cPWNbxi7juSExgsQIEiNIjCAxgsQIEiNIjCAxgsQIEiNIjCAxgsQIEiNIjCAxgsQIEiNIjCAxgsQIEiNIzJRFuRk+317uv98fTqPnrJdl6DLeccazeDM2CmfEmMEvK0aQGEFiBIkRJEaQGEFiBIkRJEaQGEFiBIkRJEaQGEFiBIkRJEaQGEFiBIkRJEaQGEFiBIk57Pv+eu1D/IV1HbtROIsbEiNIjCAxgsQIEiNIjCAxgsQIEiNIjCAxgsQIEiNIjCAxgsQIEiNIjCAxgsQIEiNIjCAxP77GGcNMNCzSAAAAAElFTkSuQmCC"> </body> </html>