imagecolorallocate
Description
The imagecolorallocate of Image for PHP allocate a color for an image.
Syntax
imagecolorallocate(
GdImage $image,
int $red,
int $green,
int $blue
): int|falseParameters
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.
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; $return = imagecolorallocate($image, $red, $green, $blue); echo $return;
0
2 · red green blue · hexadecimal
<? $width = 100; $height = 100; $image = imagecreate($width, $height); $red = 0xff; $green = 0xff; $blue = 0xff; $return = imagecolorallocate($image, $red, $green, $blue); echo $return;
0
3 · return
<? $width = 100; $height = 100; $image = imagecreate($width, $height); $red = 255; $green = 255; $blue = 255; $background = imagecolorallocate($image, $red, $green, $blue); $red = 255; $green = 0; $blue = 0; $r = imagecolorallocate($image, $red, $green, $blue); $red = 0; $green = 255; $blue = 0; $g = imagecolorallocate($image, $red, $green, $blue); $red = 0; $green = 0; $blue = 255; $b = imagecolorallocate($image, $red, $green, $blue); echo $background . $r . $g . $b;
0123
4 · base64
<?
$width = 100;
$height = 100;
$image = imagecreate($width, $height);
$red = 255;
$green = 255;
$blue = 255;
$background = imagecolorallocate($image, $red, $green, $blue);
$x = 0;
$y = 0;
imagefill($image, $x, $y, $background);
$red = 255;
$green = 0;
$blue = 0;
$r = imagecolorallocate($image, $red, $green, $blue);
$x1 = 0;
$y1 = 0;
$x2 = 50;
$y2 = 50;
imagefilledrectangle($image, $x1, $y1, $x2, $y2, $r);
$red = 0;
$green = 255;
$blue = 0;
$g = imagecolorallocate($image, $red, $green, $blue);
$x1 = 25;
$y1 = 25;
$x2 = 75;
$y2 = 75;
imagefilledrectangle($image, $x1, $y1, $x2, $y2, $g);
$red = 0;
$green = 0;
$blue = 255;
$b = imagecolorallocate($image, $red, $green, $blue);
$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,iVBORw0KGgoAAAANSUhEUgAAAGQAAABkAgMAAAANjH3HAAAADFBMVEX/////AAAA/wAAAP9WpSYHAAAACXBIWXMAAA7EAAAOxAGVKw4bAAAANklEQVRIiWMIRQIhDMhgVGZUhv4yWauQwKjMqMwAy6z/jwSg8lqjMqMyg0IGAvhHZUZlaCsDAIspNRnrII7dAAAAAElFTkSuQmCC">
</body>
</html>Links
Image
- gd_info
- getimagesize
- getimagesizefromstring
- image_type_to_extension
- image_type_to_mime_type
- imageaffine
- imageaffinematrixconcat
- imageaffinematrixget
- imagealphablending
- imageantialias
- imagearc
- imageavif
- imagebmp
- imagechar
- imagecharup
- imagecolorallocatealpha
- imagecolorat
- imagecolorclosest
- imagecolorclosestalpha
- imagecolorclosesthwb
- imagecolordeallocate
- imagecolorexact
- imagecolorexactalpha
- imagecolormatch
- imagecolorresolve
- imagecolorresolvealpha
- imagecolorset
- imagecolorsforindex
- imagecolorstotal
- imagecolortransparent
- imageconvolution
- imagecopy
- imagecopymerge
- imagecopymergegray
- imagecopyresampled
- imagecopyresized
- imagecreate
- imagecreatefromavif
- imagecreatefrombmp
- imagecreatefromgif
- imagecreatefromjpeg
- imagecreatefrompng
- imagecreatefromstring
- imagecreatefromtga
- imagecreatefromwbmp
- imagecreatefromwebp
- imagecreatefromxbm
- imagecreatefromxpm
- imagecreatetruecolor
- imagecrop
- imagecropauto
- imagedashedline
- imagedestroy
- imageellipse
- imagefill
- imagefilledarc
- imagefilledellipse
- imagefilledpolygon
- imagefilledrectangle
- imagefilltoborder
- imagefilter
- imageflip
- imagefontheight
- imagefontwidth
- imageftbbox
- imagefttext
- imagegammacorrect
- imagegetclip
- imagegetinterpolation
- imagegif
- imageinterlace
- imageistruecolor
- imagejpeg
- imagelayereffect
- imageline
- imageloadfont
- imageopenpolygon
- imagepalettecopy
- imagepalettetotruecolor
- imagepng
- imagepolygon
- imagerectangle
- imageresolution
- imagerotate
- imagesavealpha
- imagescale
- imagesetbrush
- imagesetclip
- imagesetinterpolation
- imagesetpixel
- imagesetstyle
- imagesetthickness
- imagesettile
- imagestring
- imagestringup
- imagesx
- imagesy
- imagetruecolortopalette
- imagettfbbox
- imagettftext
- imagetypes
- imagewbmp
- imagewebp
- imagexbm
- iptcembed
- iptcparse