4 Script Name: GD Gradient Fill
5 Script URI: http://planetozh.com/blog/my-projects/images-php-gd-gradient-fill/
6 Description: Creates a gradient fill of any shape (rectangle, ellipse, vertical, horizontal, diamond)
7 Author: Ozh --> MODIFIED BY Martin Frano
9 Author URI: http://planetozh.com/
14 * - changed : more nicely packaged as a class
15 * - fixed : not displaying proper gradient colors with image dimension greater than 255 (because of a limitation in imagecolorallocate)
16 * - added : optional parameter 'step', more options for 'direction'
23 * require_once('/path/to/gd-gradient-fill.php');
24 * $image = new gd_gradient_fill($width,$height,$direction,$startcolor,$endcolor,$step);
27 * - width and height : integers, dimesions of your image.
28 * - direction : string, shape of the gradient.
29 * Can be : vertical, horizontal, rectangle (or square), ellipse, ellipse2, circle, circle2, diamond.
30 * - startcolor : string, start color in 3 or 6 digits hexadecimal.
31 * - endcolor : string, end color in 3 or 6 digits hexadecimal.
32 * - step : integer, optional, default to 0. Step that breaks the smooth blending effect.
33 * Returns a resource identifier.
38 * require_once('/home/ozh/www/includes/gd-gradient-fill.php');
39 * $image = new gd_gradient_fill(200,200,'horizontal','#fff','#f00');
42 * require_once('c:/iis/inet/include/gd-gradient-fill.php');
43 * $myimg = new gd_gradient_fill(80,20,'diamond','#ff0010','#303060');
49 public $image; // image with the gradient
51 // Constructor. Creates, fills and returns an image
52 function __construct($dest, $w, $h, $d, $s, $e, $step=0) {
55 $this->direction
= $d;
56 $this->startcolor
= $s;
58 $this->step
= intval(abs($step));
63 // Attempt to create a blank image in true colors, or a new palette based image if this fails
64 if (function_exists('imagecreatetruecolor')) {
65 $this->image
= imagecreatetruecolor($this->width
,$this->height
);
66 } elseif (function_exists('imagecreate')) {
67 $this->image
= imagecreate($this->width
,$this->height
);
69 die('Unable to create an image');
74 $this->fill($this->direction
,$this->startcolor
,$this->endcolor
);
77 //$this->display($this->image);
80 //return $this->image;
84 // // Displays the image with a portable function that works with any file type
85 // // depending on your server software configuration
86 // function display () {
87 // $im = $this->image;
89 // if (function_exists("imagepng")) {
90 // header("Content-type: image/png");
93 // elseif (function_exists("imagegif")) {
94 // header("Content-type: image/gif");
97 // elseif (function_exists("imagejpeg")) {
98 // header("Content-type: image/jpeg");
99 // imagejpeg($im, "", 0.5);
101 // elseif (function_exists("imagewbmp")) {
102 // header("Content-type: image/vnd.wap.wbmp");
105 // die("Doh ! No graphical functions on this server?");
111 // The main function that draws the gradient
112 function fill($direction,$start,$end) {
117 $line_numbers = imagesx($im);
118 $line_width = imagesy($im);
119 list($r1,$g1,$b1) = $this->hex2rgb($start);
120 list($r2,$g2,$b2) = $this->hex2rgb($end);
123 $line_numbers = imagesy($im);
124 $line_width = imagesx($im);
125 list($r1,$g1,$b1) = $this->hex2rgb($start);
126 list($r2,$g2,$b2) = $this->hex2rgb($end);
129 $width = imagesx($im);
130 $height = imagesy($im);
131 $rh=$height>$width?
1:$width/$height;
132 $rw=$width>$height?
1:$height/$width;
133 $line_numbers = min($width,$height);
134 $center_x = $width/2;
135 $center_y = $height/2;
136 list($r1,$g1,$b1) = $this->hex2rgb($end);
137 list($r2,$g2,$b2) = $this->hex2rgb($start);
138 imagefill($im, 0, 0, imagecolorallocate( $im, $r1, $g1, $b1 ));
141 $width = imagesx($im);
142 $height = imagesy($im);
143 $rh=$height>$width?
1:$width/$height;
144 $rw=$width>$height?
1:$height/$width;
145 $line_numbers = sqrt(pow($width,2)+
pow($height,2));
146 $center_x = $width/2;
147 $center_y = $height/2;
148 list($r1,$g1,$b1) = $this->hex2rgb($end);
149 list($r2,$g2,$b2) = $this->hex2rgb($start);
152 $width = imagesx($im);
153 $height = imagesy($im);
154 $line_numbers = sqrt(pow($width,2)+
pow($height,2));
155 $center_x = $width/2;
156 $center_y = $height/2;
158 list($r1,$g1,$b1) = $this->hex2rgb($end);
159 list($r2,$g2,$b2) = $this->hex2rgb($start);
162 $width = imagesx($im);
163 $height = imagesy($im);
164 $line_numbers = min($width,$height);
165 $center_x = $width/2;
166 $center_y = $height/2;
168 list($r1,$g1,$b1) = $this->hex2rgb($end);
169 list($r2,$g2,$b2) = $this->hex2rgb($start);
170 imagefill($im, 0, 0, imagecolorallocate( $im, $r1, $g1, $b1 ));
174 $width = imagesx($im);
175 $height = imagesy($im);
176 $line_numbers = max($width,$height)/2;
177 list($r1,$g1,$b1) = $this->hex2rgb($end);
178 list($r2,$g2,$b2) = $this->hex2rgb($start);
181 list($r1,$g1,$b1) = $this->hex2rgb($end);
182 list($r2,$g2,$b2) = $this->hex2rgb($start);
183 $width = imagesx($im);
184 $height = imagesy($im);
185 $rh=$height>$width?
1:$width/$height;
186 $rw=$width>$height?
1:$height/$width;
187 $line_numbers = min($width,$height);
196 for ( $i = 0; $i < $line_numbers; $i=$i+
1+
$this->step
) {
202 $r = ( $r2 - $r1 != 0 ) ?
intval( $r1 +
( $r2 - $r1 ) * ( $i / $line_numbers ) ): $r1;
203 $g = ( $g2 - $g1 != 0 ) ?
intval( $g1 +
( $g2 - $g1 ) * ( $i / $line_numbers ) ): $g1;
204 $b = ( $b2 - $b1 != 0 ) ?
intval( $b1 +
( $b2 - $b1 ) * ( $i / $line_numbers ) ): $b1;
205 // if new values are really new ones, allocate a new color, otherwise reuse previous color.
206 // There's a "feature" in imagecolorallocate that makes this function
207 // always returns '-1' after 255 colors have been allocated in an image that was created with
208 // imagecreate (everything works fine with imagecreatetruecolor)
209 if ( "$old_r,$old_g,$old_b" != "$r,$g,$b")
210 $fill = imagecolorallocate( $im, $r, $g, $b );
213 imagefilledrectangle($im, 0, $i, $line_width, $i+
$this->step
, $fill);
216 imagefilledrectangle( $im, $i, 0, $i+
$this->step
, $line_width, $fill );
222 imagefilledellipse ($im,$center_x, $center_y, ($line_numbers-$i)*$rh, ($line_numbers-$i)*$rw,$fill);
226 imagefilledrectangle ($im,$i*$width/$height,$i*$height/$width,$width-($i*$width/$height), $height-($i*$height/$width),$fill);
229 imagefilledpolygon($im, array (
230 $width/2, $i*$rw-0.5*$height,
231 $i*$rh-0.5*$width, $height/2,
232 $width/2,1.5*$height-$i*$rw,
233 1.5*$width-$i*$rh, $height/2 ), 4, $fill);
240 // #ff00ff -> array(255,0,255) or #f0f -> array(255,0,255)
241 function hex2rgb($color) {
242 $color = str_replace('#','',$color);
243 $s = strlen($color) / 3;
244 $rgb[]=hexdec(str_repeat(substr($color,0,$s),2/$s));
245 $rgb[]=hexdec(str_repeat(substr($color,$s,$s),2/$s));
246 $rgb[]=hexdec(str_repeat(substr($color,2*$s,$s),2/$s));