Avail feature updated
[ninja.git] / application / vendor / mfchart / Gradient.php
blobaee921b43def476221c7d71e80d0e4d9c2cb8fef
1 <?php
3 /*
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
8 Version: 1.1
9 Author URI: http://planetozh.com/
12 /* Release history :
13 * 1.1
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'
17 * 1.0
18 * - initial release
21 /* Usage :
23 * require_once('/path/to/gd-gradient-fill.php');
24 * $image = new gd_gradient_fill($width,$height,$direction,$startcolor,$endcolor,$step);
26 * Parameters :
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.
35 * Examples :
37 * 1.
38 * require_once('/home/ozh/www/includes/gd-gradient-fill.php');
39 * $image = new gd_gradient_fill(200,200,'horizontal','#fff','#f00');
41 * 2.
42 * require_once('c:/iis/inet/include/gd-gradient-fill.php');
43 * $myimg = new gd_gradient_fill(80,20,'diamond','#ff0010','#303060');
47 class Gradient {
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) {
53 $this->width = $w;
54 $this->height = $h;
55 $this->direction = $d;
56 $this->startcolor = $s;
57 $this->endcolor = $e;
58 $this->step = intval(abs($step));
60 if ($dest) {
61 $this->image = $dest;
62 } else {
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);
68 } else {
69 die('Unable to create an image');
73 // Fill it
74 $this->fill($this->direction,$this->startcolor,$this->endcolor);
76 // Show it
77 //$this->display($this->image);
79 // Return it
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;
88 //
89 // if (function_exists("imagepng")) {
90 // header("Content-type: image/png");
91 // imagepng($im);
92 // }
93 // elseif (function_exists("imagegif")) {
94 // header("Content-type: image/gif");
95 // imagegif($im);
96 // }
97 // elseif (function_exists("imagejpeg")) {
98 // header("Content-type: image/jpeg");
99 // imagejpeg($im, "", 0.5);
100 // }
101 // elseif (function_exists("imagewbmp")) {
102 // header("Content-type: image/vnd.wap.wbmp");
103 // imagewbmp($im);
104 // } else {
105 // die("Doh ! No graphical functions on this server?");
106 // }
107 // return true;
108 // }
111 // The main function that draws the gradient
112 function fill($direction,$start,$end) {
113 $im = $this->image;
115 switch($direction) {
116 case 'horizontal':
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);
121 break;
122 case 'vertical':
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);
127 break;
128 case 'ellipse':
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 ));
139 break;
140 case 'ellipse2':
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);
150 break;
151 case 'circle':
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;
157 $rh = $rw = 1;
158 list($r1,$g1,$b1) = $this->hex2rgb($end);
159 list($r2,$g2,$b2) = $this->hex2rgb($start);
160 break;
161 case 'circle2':
162 $width = imagesx($im);
163 $height = imagesy($im);
164 $line_numbers = min($width,$height);
165 $center_x = $width/2;
166 $center_y = $height/2;
167 $rh = $rw = 1;
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 ));
171 break;
172 case 'square':
173 case 'rectangle':
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);
179 break;
180 case 'diamond':
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);
188 break;
189 default:
192 $r = NULL;
193 $g = NULL;
194 $b = NULL;
196 for ( $i = 0; $i < $line_numbers; $i=$i+1+$this->step ) {
197 // old values :
198 $old_r=$r;
199 $old_g=$g;
200 $old_b=$b;
201 // new values :
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 );
211 switch($direction) {
212 case 'vertical':
213 imagefilledrectangle($im, 0, $i, $line_width, $i+$this->step, $fill);
214 break;
215 case 'horizontal':
216 imagefilledrectangle( $im, $i, 0, $i+$this->step, $line_width, $fill );
217 break;
218 case 'ellipse':
219 case 'ellipse2':
220 case 'circle':
221 case 'circle2':
222 imagefilledellipse ($im,$center_x, $center_y, ($line_numbers-$i)*$rh, ($line_numbers-$i)*$rw,$fill);
223 break;
224 case 'square':
225 case 'rectangle':
226 imagefilledrectangle ($im,$i*$width/$height,$i*$height/$width,$width-($i*$width/$height), $height-($i*$height/$width),$fill);
227 break;
228 case 'diamond':
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);
234 break;
235 default:
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));
247 return $rgb;