Wizzard~Of~Ozz
04-11-2005, 4:48 PM
I looked around for a nice way to make a gradient Image in PHP, but could only find messy things that I couldn't follow, so I wrote my own, it requires PHP and GD and I'm just putting it here for reference, it's a function that will draw a rectangle of X width and X height and fill it's with a blend of 2 colours (on my stats page it's used for the bar graphs), the source image must be created with imagecreatetruecolor since this can use a fair number of colours, I would recommend outputting the end file to PNG since it's size is quite good, and quality is excellent.
<?php
Function grad($im,$height,$width,$step,$baseheight,$basewid th,$base,$end)
{
// $im is passed as @$im for the image to draw it on
// $height is the Height of the box to draw
// $width is the width of the box to draw
// $step is the steps to take, a step of 10 would change the colour every 10 pixels
// $baseheight is where to place the bottom of the rectangle
// $basewidth is the left side of the location for the rectangle
// $base is the start colour in hex (ffffff)
// $end is the colour to end at in hex (000000)
// Break colours into RGB components
sscanf($base, "%2x%2x%2x", $rbase, $gbase, $bbase);
sscanf($end, "%2x%2x%2x", $rend, $gend, $bend);
// Set the Variable to step to use height
$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 for the height at a rate equal to the steps.
for($i=1;$i<$varstep;$i=$i+$step+1)
{
//Adjust the colours
$clour1 = ($i*$rmod)+$rbase;
$clour2 = ($i*$gmod)+$gbase;
$clour3 = ($i*$bmod)+$bbase;
$col=imagecolorallocate($im,$clour1,$clour2,$clour 3);
//Paint the rectangle at current colour.
imagefilledrectangle($im,$basewidth-$width+1,$baseheight-$i,$basewidth,$baseheight-$i+$step,$col);
}
}
// Return the colour
return(@$im);
?>
I also have it as a different php file that will allow for horizontal gradients aswell, I just removed it from this to abbreviate it, to change it, you'ld use the width for varstep instead of height and adjust the imagefilledrectangle to adjust width instead.
Hopefully this will help someone, for 22 lines of code, it works very well, not too sure about the passing image and return since $im is the same variable in both the function and the main page.. I use an include to add this so it can work with any program I choose.
The finished product (this function is called 28 times for this image using random base and end colours)
http://woo.gotdns.com/GraphWeek.php.png
<?php
Function grad($im,$height,$width,$step,$baseheight,$basewid th,$base,$end)
{
// $im is passed as @$im for the image to draw it on
// $height is the Height of the box to draw
// $width is the width of the box to draw
// $step is the steps to take, a step of 10 would change the colour every 10 pixels
// $baseheight is where to place the bottom of the rectangle
// $basewidth is the left side of the location for the rectangle
// $base is the start colour in hex (ffffff)
// $end is the colour to end at in hex (000000)
// Break colours into RGB components
sscanf($base, "%2x%2x%2x", $rbase, $gbase, $bbase);
sscanf($end, "%2x%2x%2x", $rend, $gend, $bend);
// Set the Variable to step to use height
$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 for the height at a rate equal to the steps.
for($i=1;$i<$varstep;$i=$i+$step+1)
{
//Adjust the colours
$clour1 = ($i*$rmod)+$rbase;
$clour2 = ($i*$gmod)+$gbase;
$clour3 = ($i*$bmod)+$bbase;
$col=imagecolorallocate($im,$clour1,$clour2,$clour 3);
//Paint the rectangle at current colour.
imagefilledrectangle($im,$basewidth-$width+1,$baseheight-$i,$basewidth,$baseheight-$i+$step,$col);
}
}
// Return the colour
return(@$im);
?>
I also have it as a different php file that will allow for horizontal gradients aswell, I just removed it from this to abbreviate it, to change it, you'ld use the width for varstep instead of height and adjust the imagefilledrectangle to adjust width instead.
Hopefully this will help someone, for 22 lines of code, it works very well, not too sure about the passing image and return since $im is the same variable in both the function and the main page.. I use an include to add this so it can work with any program I choose.
The finished product (this function is called 28 times for this image using random base and end colours)
http://woo.gotdns.com/GraphWeek.php.png