Snippet Name: Gradient Image Generator
Description: The default gradient is set at black->white. To change it, just add the start and end variables.
Example usage:
Comment: (none)
Language: PHP
Highlight Mode: PHP
Last Modified: March 01st, 2009
|
<?PHP
HEADER("Content-type: image/png");
$height = 100;
$width = 50;
$start = '000000';
$end = 'FFFFFF';
IF ($_GET["start"]) {
$start = $_GET["start"];
}
IF ($_GET["end"]) {
$end = $_GET["end"];
}
$start_r = HEXDEC(SUBSTR($start, 0, 2));
$start_g = HEXDEC(SUBSTR($start, 2, 2));
$start_b = HEXDEC(SUBSTR($start, 4, 2));
$end_r = HEXDEC(SUBSTR($end, 0, 2));
$end_g = HEXDEC(SUBSTR($end, 2, 2));
$end_b = HEXDEC(SUBSTR($end, 4, 2));
$image = @imagecreate($width, $height);
FOR($y=0;$y<$height;$y++) {
FOR($x=0;$x<$width;$x++) {
IF ($start_r == $end_r) {
$new_r = $start_r;
}
$difference = $start_r - $end_r;
$new_r = $start_r - INTVAL(($difference / $height) * $y);
IF ($start_g == $end_g) {
$new_g = $start_g;
}
$difference = $start_g - $end_g;
$new_g = $start_g - INTVAL(($difference / $height) * $y);
IF ($start_b == $end_b) {
$new_b = $start_b;
}
$difference = $start_b - $end_b;
$new_b = $start_b - INTVAL(($difference / $height) * $y);
$row_color = imagecolorresolve($image, $new_r, $new_g, $new_b);
imagesetpixel($image, $x, $y, $row_color);
}
}
imagepng($image);
imagedestroy($image);
// Example usage:
// <img src="gradient.php?start=START_HEX_COLOR&end=END_HEX_COLOR" />
?> |