| |
HOWTO: Horizontal color gradient in PHP using GD library
< Previous | Back to List
digitela at December 30, 2005
A very important function missing in GD library is the color gradient. Here is simple function that fills image with horizontal color gradient:
<? function imagecolorgradient($im, $x1, $y1, $height, $width, $left_color, $right_color) { $color0=($left_color[0]-$right_color[0])/$width; $color1=($left_color[1]-$right_color[1])/$width; $color2=($left_color[2]-$right_color[2])/$width; for ($i=0;$i<=$width;$i++) { $red=$left_color[0]-floor($i*$color0); $green=$left_color[1]-floor($i*$color1); $blue=$left_color[2]-floor($i*$color2); $col= imagecolorallocate($im, $red, $green, $blue); imageline($im, $x1+$i, $y1, $x1+$i, $y1+$height, $col); } } ?>
Is not a complicated one, but anyway here is simple example of its usage:
<? $height = 20; $width = 200; $left_color = array(255, 255, 0); $right_color = array(255, 0, 0); $im = imagecreatetruecolor($width, $height); imagecolorgradient($im, 0, 0, $height, $width, $left_color, $right_color); header('Content-type: image/png'); imagepng($im); imagedestroy($im); ?>
As a result you should see in your browser next image. Could be very usefull in bar graphs and design.
< Previous | Back to List
|
|
 |
 |
| Get started |
| You may think that Digitela services are expencive for you due to only two reasons: 1) You don't know our prices yet; 2) You don't know yet that you will get highly competitive and appreciable result. Get to know it right now! |
| |
| Latest work |
 |
 |

See all works in portfolio. |
|