Image processing is a common requirement in web development, such as generating thumbnails, adding watermarks, and adjusting hues automatically. In certain scenarios, such as creating personalized avatars or artistic effects, we need to dynamically generate color palettes based on different inputs and apply them to images.
This article will teach you how to write a PHP script that supports image processing.
The most commonly used library for image processing in PHP is the GD library (GD extension). Before you start, please make sure that the GD extension is installed and enabled in your PHP environment:
php -m | grep gd
If it's not installed, you can install it with the following commands (example for Ubuntu):
sudo apt-get install php-gd
sudo service apache2 restart
Receive parameters (such as color, image size, and palette mode)
Generate a dynamic color palette based on the parameters
Create a canvas
Draw the image based on the color palette
Output or save the image
Below is a complete example that supports dynamic color palettes and simple rendering effects:
<?php
// Set response header
header('Content-Type: image/png');
<p>// Define color palette generation function<br>
function generatePalette($baseColor, $count = 5) {<br>
$palette = [];<br>
list($r, $g, $b) = sscanf($baseColor, "#%02x%02x%02x");</p>
$factor = 1 - ($i * 0.15);
$palette[] = sprintf("#%02x%02x%02x",
max(0, min(255, $r * $factor)),
max(0, min(255, $g * $factor)),
max(0, min(255, $b * $factor))
);
}
return $palette;
}
// Read base color from GET parameters
$baseColor = isset($_GET['color']) ? $_GET['color'] : '#3498db';
// Generate color palette
$palette = generatePalette($baseColor);
// Create canvas
$width = 500;
$height = 100;
$image = imagecreatetruecolor($width, $height);
// Fill background
$white = imagecolorallocate($image, 255, 255, 255);
imagefill($image, 0, 0, $white);
// Draw color bar
$segmentWidth = $width / count($palette);
foreach ($palette as $index => $hex) {
list($r, $g, $b) = sscanf($hex, "#%02x%02x%02x");
$color = imagecolorallocate($image, $r, $g, $b);
imagefilledrectangle(
$image,
$index * $segmentWidth,
0,
($index + 1) * $segmentWidth,
$height,
$color
);
}
// Optional: Label color codes on the image
foreach ($palette as $index => $hex) {
$textColor = imagecolorallocate($image, 0, 0, 0);
imagestring(
$image,
3,
$index * $segmentWidth + 10,
$height / 2 - 7,
$hex,
$textColor
);
}
// Output the image
imagepng($image);
imagedestroy($image);
?>
If you save the above code as palette.php, you can access it like this:
https://m66.net/palette.php?color=%23e74c3c
This will generate a color palette based on red (#e74c3c) and return the image.
Since the output is an image, there should be no HTML or spaces in the script, as it would corrupt the image.
The number of colors in the palette can be adjusted as needed.
More advanced features such as gradients, random colors, and different color schemes can be added for more complex use cases.
Through this article, we learned how to dynamically generate a color palette with PHP and render it as an image. This simple technique can be applied to various scenarios, such as avatar generators, game skin configurations, and theme previews.
In the future, you can further extend this by integrating color theory to generate more harmonious palettes or using AI to create personalized color schemes.