I have revamped the function given in the link above.
My changes include:
-
The rectangles were actually starting on the right, not the left as stated in the function. This was majorly confusing!
-
Added horizontal bar support
-
Accept rgb or hex color values
-
renamed variables to facilitate understanding, arguement order, etc.
The only problem I cannot correct is when filling an entire image, the topmost and/or leftmost line is left white. The white line looks nice in a bar graph, but unnecessary in an ordinary graphic.
Please give suggestions for further improvement.
function gradient($im,$xstart,$ystart,$width,$height,$pixels_per_step,$start_color,$end_color,$horizontal = false) {
// function to paint boxes with gradients having two endpoints
// based on the original function at: http://www.xtremepccentral.com/forums/showthread.php?t=17981
// $im is the image GD image to draw on
// $xstart is the left (vertical mode) or top (horizontal mode) position of the rectangle
// $ystart is the bottom (vertical mode) or left (horizontal mode) of the rectangle
// $width is the width of the box to draw (starting from $xstart)
// $height is the height of the box to draw (starting from $ystart)
// $pixels_per_step is the length of steps to take, a step of 10 would change the colour every 10 pixels
// $start_color is the colour to start at (bottom/left) in rgb (xxx,yyy,zzz) or hexadecimal (00ff00)
// $end_color is the end colour (top/right) in in rgb (xxx,yyy,zzz) or hexadecimal (ff00ff)
// $horizontal is true if the bars should be drawn horizontally. Defaults to vertical bars.
// Extract RGB components of the color
if (strpos($start_color,",") !== false) {
// color is in RGB
list($rend , $gend , $bend) = explode(",", $start_color);
list($rbase, $gbase, $bbase) = explode(",", $end_color);
} else {
// color is in hexadecimal
sscanf($base, "%2x%2x%2x", $rbase, $gbase, $bbase);
sscanf($end, "%2x%2x%2x", $rend, $gend, $bend);
}
// Set the Variable to step
if ($horizontal) {
// horizontal gradients
$varstep = $width;
} else {
// vertical gradients
$varstep = $height;
}
// Remove potential divide by 0 errors.
if ($rbase == $rend) $rend = $rend -1;
if ($gbase == $gend) $gend = $gend -1;
if ($bbase == $bend) $bend = $bend -1;
// Make sure the height is at least 1 pixel
if ($varstep == 0) $varstep = 1;
// Set up step modifiers for each colour
$rmod = ($rend - $rbase) / $varstep;
$gmod = ($gend - $gbase) / $varstep;
$bmod = ($bend - $bbase) / $varstep;
// Serves no real purpose.
$white=imagecolorallocate($im,255,255,255);
// Loop through the various colored steps
for($i = 1; $i < $varstep; $i = $i+$pixels_per_step+1) {
//Adjust the color for this step
$clour1 = ($i*$rmod)+$rbase;
$clour2 = ($i*$gmod)+$gbase;
$clour3 = ($i*$bmod)+$bbase;
$col=imagecolorallocate($im,$clour1,$clour2,$clour3);
// Paint the current step color
if ($horizontal) {
// horizontal gradient
imagefilledrectangle($im, $xstart+$i-1, $ystart, $xstart+$i-1+$pixels_per_step, $ystart+$height, $col);
} else {
// vertical gradient
imagefilledrectangle($im, $xstart, $ystart-$i, $xstart+$width, $ystart-$i+$pixels_per_step, $col);
}
}
// Return the image
return($im);
}