Saturday, May 23, 2026
HomeLanguagesPHP | imagecolordeallocate() Function

PHP | imagecolordeallocate() Function

The imagecolordeallocate() function is an inbuilt function in PHP which is used to de-allocate a color created by imagecolorallocate() or imagecolorallocatealpha() function for an image.

Syntax:

bool imagecolordeallocate( $image, $color )

Parameters: This function accepts two 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 the size of image.
  • $color: This parameter holds the color identifier.

Return Value: This function returns True on success or False on failure.

Below programs illustrate the imagecolordeallocate() function in PHP:

Program 1:




<?php 
  
// It create the size of image or blank image. 
$image_size = imagecreatetruecolor(500, 300); 
  
// Set the background color of image using 
// imagecolorallocate() function. 
$bg = imagecolorallocate($image_size, 0, 103, 0); 
  
// Use imagecolordeallocate() function to
// deallocate the color
$bg = imagecolordeallocate($image_size, $bg);
  
// Fill background with above selected color. 
imagefill($image_size, 0, 0, $bg); 
  
// Output image in the browser 
header("Content-type: image/png"); 
imagepng($image_size); 
  
// free memory 
imagedestroy($image_size); 
  
?> 


Output:

Program 2:




<?php 
  
// It create the size of image or blank image. 
$image_size = imagecreatetruecolor(500, 300); 
  
// Set the background color of image. 
$bg = imagecolorallocate($image_size, 0, 103, 0); 
  
// Use imagecolordeallocate() function to
// deallocate the color
$bg = imagecolordeallocate($image_size, $bg); 
  
// Fill background with above selected color. 
imagefill($image_size, 0, 0, $bg); 
  
// Set the colors of image 
$white_color = imagecolorallocate($image_size, 255, 255, 255); 
  
// Draw a circle 
imagearc($image_size, 200, 150, 200, 200, 0, 360, $white_color); 
  
// Output image in the browser 
header("Content-type: image/png"); 
imagepng($image_size); 
  
?>


Output:

Reference: https://www.php.net/manual/en/function.imagecolordeallocate.php

RELATED ARTICLES

1 COMMENT

Most Popular

Dominic
32514 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
6892 POSTS0 COMMENTS
Nicole Veronica
12012 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12107 POSTS0 COMMENTS
Shaida Kate Naidoo
7016 POSTS0 COMMENTS
Ted Musemwa
7262 POSTS0 COMMENTS
Thapelo Manthata
6975 POSTS0 COMMENTS
Umr Jansen
6963 POSTS0 COMMENTS