PHP imagecolorset β Set Color for a Given Palette Index
imagecolorset β Sets the color for a given palette index.
Syntax
void imagecolorset ( resource $image , int $index , int $red , int $green , int $blue )
This function sets the specified index in the palette to the given color. It is useful for creating flood-fill-like effects in palette-based images without the overhead of actually performing a flood fill.
Note: This function is only available with the GD library.
Parameters
- $image: An image resource, returned by one of the image creation functions, such as
imagecreatetruecolor(). - $index: The color index to set.
- $red: The red component (0-255).
- $green: The green component (0-255).
- $blue: The blue component (0-255).
Return Values
Does not return a value.
Example
<?php
// Create a 100x100 image
$im = imagecreatetruecolor(100, 100);
// Allocate colors
$white = imagecolorallocate($im, 255, 255, 255);
$red = imagecolorallocate($im, 255, 0, 0);
// Fill the image with white
imagefill($im, 0, 0, $white);
// Set the color at index 1 to red
imagecolorset($im, 1, 255, 0, 0);
// Output the image
header('Content-Type: image/png');
imagepng($im);
imagedestroy($im);
?>
// Create a 100x100 image
$im = imagecreatetruecolor(100, 100);
// Allocate colors
$white = imagecolorallocate($im, 255, 255, 255);
$red = imagecolorallocate($im, 255, 0, 0);
// Fill the image with white
imagefill($im, 0, 0, $white);
// Set the color at index 1 to red
imagecolorset($im, 1, 255, 0, 0);
// Output the image
header('Content-Type: image/png');
imagepng($im);
imagedestroy($im);
?>
ByteDance Coding Plan supports mainstream large models like Doubao, GLM, DeepSeek, Kimi, MiniMax. Official supply, stable and reliable. Β₯9.9/month
iFlytek Star Coding Plan includes free model call quotas, DeepSeek, GLM, Kimi, MiniMax. One-stop experience and deployment platform. Configuration Guide Β₯3.9/month Subscribe Now
YouTip