When working with images in PHP, dynamically modifying color maps is a very practical skill. Especially when we want to quickly change certain colors in a palette-based image without redrawing the entire image, imagecolorset() and imagecolorresolve() become particularly important.
This article will teach you how to use these two functions in conjunction with the effect of dynamically modifying the image color map.
imagecolorset(resource $image, int $index, int $red, int $green, int $blue, int $alpha = 0): bool
This function can directly modify the color value of a color index in the image palette. That is to say, if a pixel in the figure uses this color index, its color will also change immediately.
imagecolorresolve(resource $image, int $red, int $green, int $blue): int
This function finds the index closest to the specified RGB color in the image palette. If the exact matching color is not found, it tries to assign a new index.
These two functions can be used together to enable us to locate the color we want to modify first, and then dynamically set the new color.
Suppose we have a simple palette image and we want to change the original blue from the image to red.
<?php
// Create a simple palette image
$image = imagecreate(100, 100);
// Define the initial color
$white = imagecolorallocate($image, 255, 255, 255); // Background color:White
$blue = imagecolorallocate($image, 0, 0, 255); // Brush color:blue
// 用blue画一个矩形
imagefilledrectangle($image, 10, 10, 90, 90, $blue);
// 动态找到blue的Color index
$targetIndex = imagecolorresolve($image, 0, 0, 255);
// use imagecolorset 修改blue为red色
// The parameters are:Image Resources、Color index、red、green、blue、transparency
imagecolorset($image, $targetIndex, 255, 0, 0);
// Output image to browser
header('Content-Type: image/png');
imagepng($image);
// Free memory
imagedestroy($image);
?>
After this code is executed, you will see that the original blue rectangle turns red, which is very silky.
This method only applies to palette images (i.e. palette-based image, such as GIF or small images created with imagecreate() ).
For true color images (created by imagecreatetruecolor() ), imagecolorset() is invalid.
If there are multiple similar but not exactly the same colors in the image, the colors found using imagecolorresolve() may not be what you expected, so sometimes you need to standardize the palette of the image in advance.
For example, if you want to develop a web page function, users can click the button to dynamically change the color of the picture, and you can use the above solution to combine it with a simple form to submit. For example:
<form method="post" action="https://m66.net/change_color.php">
<input type="submit" name="change" value="Change the color!">
</form>
Then use the script just now to process the image to achieve the effect of dynamic color change.
By combining image color resolve() to find the target color index, and then using imagecolorset() to modify the corresponding color value, it is very convenient to realize dynamic image color replacement. This technology is especially suitable for use when it is necessary to quickly change the fixed color area, such as game maps, dynamic icon customization and other scenarios.
If your project needs to process a large number of dynamic images, it is recommended to use it with more advanced image processing libraries such as GD or Imagick for more powerful features.