Friday, October 17, 2025
HomeLanguagesPHP | imagecolorset() Function

PHP | imagecolorset() Function

The imagecolorset() function is an inbuilt function in PHP which is used to set the color for the specified palette index. It is used to specify the index in the palette to the specified color. To perform the actual flood-fill, it is useful to create the flood-fill-like effects in palleted images without the overhead.
Syntax: 
 

void imagecolorset ( $image, $index, $red, $green, $blue, $alpha )

Parameters: This function accepts six parameters as mentioned above and described below: 
 

  • $image: It is returned by one of the image creation functions, such as imagecreatetruecolor(). It is used to create size of image.
  • $index: This parameter is the index value in the palette image.
  • $red: This parameter is used to set value of red color component.
  • $green: This parameter is used to set value of green color component.
  • $blue: This parameter is used to set value of blue color component.
  • $alpha: This parameter is used to set the transparency of image. The value of $alpha lies between 0 to 127 where 0 represents completely opaque while 127 represents completely transparent.

Return Value: This function does not return any value.
Below programs illustrate the imagecolorset() function in PHP:
Program 1: 
 

php




<?php
  
// Create an image of given size
$image = imagecreate(500, 300);
  
// Set the background
imagecolorallocate($image, 0, 0, 0);
  
// Get the color index for the background
$bg = imagecolorat($image, 150, 100);
  
// Change the background color
imagecolorset($image, $bg, 0, 153, 0);
  
// Output of the image
header('Content-Type: image/png');
  
imagepng($image);
imagedestroy($image);
?>


Output: 
 

image

Program 2: 
 

php




<?php
  
// Create an image of given size
$image = imagecreate(500, 300);
  
// Set the background
imagecolorallocate($image, 0, 0, 0);
 
// set the colors of image
$white_color = imagecolorallocate($image, 255, 255, 255);
 
// draw the head
imagearc($image, 200, 150, 200, 200,  0, 360, $white_color);
  
// Get the color index for the background
$bg = imagecolorat($image, 150, 100);
  
// Set the background
imagecolorset($image, $bg, 0, 153, 0);
  
// Output the image to the browser
header('Content-Type: image/png');
  
imagepng($image);
imagedestroy($image);
?>


Output: 
 

image

Related Articles: 
 

Reference: http://php.net/manual/en/function.imagecolorset.php
 

RELATED ARTICLES

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS