4 * Generate a gradient image for use as a background image.
8 * height Height of output image (ignored for horizontal gradient)
9 * width Width of output image (ignored for vertical gradient)
10 * colors Number of colors to generate
11 * direction Direction gradient should go
12 * Currently limited to multiples of 90 degrees
13 * 0 means left-to-right, 90 means bottom-to-top,
14 * 180 means right-to-left, 270 means top-to-bottom
17 * One of the following two pairs of input parameters should be used
18 * to specify the colors for the gradient:
19 * 1. color1, color2 - End colors of gradient specified
20 * as "color1=RRGGBB" or "color1=RGB"
21 * 2. base, percent - `base' is specified the same way as
22 * `color1' and `color2'
23 * `percent' is the amount the components of `base'
25 * For example, given "base=445566&percent=50", the starting and
26 * ending colors of the gradient will be:
28 * Red: 44 (hex) | 68 (dec)
29 * Green: 55 (hex) | 85 (dec)
30 * Blue: 66 (hex) | 102 (dec)
32 * Red: 44 + (50% of FF) =
33 * CC (hex) | 68 + (50% of 255) = 196 (dec)
34 * Green: 55 + (50% of FF) =
35 * DD (hex) | 85 + (50% of 255) = 203 (dec)
36 * Blue: 66 + (50% of FF) =
37 * EE (hex) | 102 + (50% of 255) = 230 (dec)
39 * So it is entirely equivalent to say "base=445566&percent=50" OR
40 * "color1=445566&color2=CCDDEE"
42 * Since this file does not use any other WebCalendar file, it could
43 * be used by other PHP apps.
46 * Allow directions which are not multiples of 90 degrees so that
47 * we can have diagonal gradients.
50 * No security restrictions by user.
51 * Limit height and width parameters to 600 so a malicious user cannot
52 * request a 10Gb image 8-)
69 // Get a value from a GET URL
70 function getGetValue ( $name ) {
71 if ( isset ( $_GET[$name] ) ) {
75 if ( ! isset ( $HTTP_GET_VARS ) ) {
79 if ( ! isset ( $HTTP_GET_VARS[$name] ) ) {
83 return ( $HTTP_GET_VARS[$name] );
87 * Turn an HTML color (like 'AABBCC') into an array of decimal RGB values
90 * $color - HTML color specification in 'RRGGBB' or 'RGB' format
93 * array('red' => $red_val, 'green' => $green_val, 'blue' => $blue_val)
95 function colorToRGB ( $color ) {
96 if ( strlen ( $color ) == 6 ) {
97 $red = hexdec ( substr ( $color, 0, 2 ) );
98 $green = hexdec ( substr ( $color, 2, 2 ) );
99 $blue = hexdec ( substr ( $color, 4, 2 ) );
100 } else if ( strlen ( $color ) == 3 ) {
101 $red_hex = substr ( $color, 0, 1 );
102 $red_hex .= $red_hex;
104 $green_hex = substr ( $color, 1, 1 );
105 $green_hex .= $green_hex;
107 $blue_hex = substr ( $color, 2, 1 );
108 $blue_hex .= $blue_hex;
110 $red = hexdec ( $red_hex );
111 $green = hexdec ( $green_hex );
112 $blue = hexdec ( $blue_hex );
114 // Invalid color specification
118 return array('red' => $red, 'green' => $green, 'blue' => $blue);
121 $direction = getGetValue ( 'direction' );
122 if ( $direction === null || ( $direction % 90 ) != 0 ) {
123 $direction = $DEFAULTS['direction'];
125 while ( $direction > 360 ) {
130 if ( $direction == 90 || $direction == 270 ) {
133 $height = getGetValue ( 'height' );
134 if ( empty ( $height ) ) {
135 $height = $DEFAULTS['height'];
138 if ( $height > $MAX_HEIGHT ) {
139 $height = $MAX_HEIGHT;
144 // Horizontal gradient
146 $width = getGetValue ( 'width' );
147 if ( empty ( $width ) ) {
148 $width = $DEFAULTS['width'];
151 if ( $width > $MAX_WIDTH ) {
158 $numcolors = getGetValue ( 'colors' );
159 if ( empty ( $numcolors ) ) {
160 $numcolors = $DEFAULTS['colors'];
162 if ( preg_match ( '/^\d+$/', $numcolors ) ) {
163 if ( $numcolors < $MIN_COLORS ) {
164 $numcolors = $MIN_COLORS;
165 } else if ( $numcolors > $MAX_COLORS ) {
166 $numcolors = $MAX_COLORS;
169 $numcolors = $DEFAULTS['colors'];
174 $base = getGetValue ( 'base' );
175 if ( $base === null ) {
176 $color1 = getGetValue ( 'color1' );
181 if ( $color1 === null ) {
182 $color1 = colorToRGB ( $DEFAULTS['color1'] );
184 if ( preg_match ( "/^#?([0-9a-fA-F]{3,6})/", $color1, $matches ) ) {
185 $color1 = colorToRGB ( $matches[1] );
187 $color1 = colorToRGB ( $DEFAULTS['color1'] );
191 if ( $base === null ) {
192 $color2 = getGetValue ( 'color2' );
193 if ( $color2 === null ) {
194 $color2 = colorToRGB ( $DEFAULTS['color2'] );
196 if ( preg_match ( "/^#?([0-9a-fA-F]{3,6})/", $color2, $matches ) ) {
197 $color2 = colorToRGB ( $matches[1] );
199 $color2 = colorToRGB ( $DEFAULTS['color2'] );
205 $percent = getGetValue ( 'percent' );
206 if ( $percent === null || $percent < 0 || $percent > 100 ) {
207 $percent = $DEFAULTS['percent'];
210 $color2['red'] = min ( $color2['red'] + $percent * 255 / 100 , 255 );
211 $color2['green'] = min ( $color2['green'] + $percent * 255 / 100, 255 );
212 $color2['blue'] = min ( $color2['blue'] + $percent * 255 / 100, 255 );
216 $image = imagecreate ( $width, $height );
218 // Allocate array of colors
221 $deltared = $color2['red'] - $color1['red'];
222 $deltagreen = $color2['green'] - $color1['green'];
223 $deltablue = $color2['blue'] - $color1['blue'];
225 for ( $i = 0; $i < $numcolors; $i++ ) {
227 min ( $color1['red'] + ( $deltared * $i / ( $numcolors - 1 ) ), 255 );
230 min ( $color1['green'] + ( $deltagreen * $i / ( $numcolors - 1 ) ), 255 );
233 min ( $color1['blue'] + ( $deltablue * $i / ( $numcolors - 1 ) ), 255 );
235 $thisred = floor ( $thisred );
236 $thisgreen = floor ( $thisgreen );
237 $thisblue = floor ( $thisblue );
239 $colors[$i] = imagecolorallocate ( $image, $thisred, $thisgreen, $thisblue );
242 switch ( $direction ) {
294 while ( $x1 >= 0 && $x1 < $width
295 && $x2 >= 0 && $x2 < $width
296 && $y1 >= 0 && $y1 < $height
297 && $y2 >= 0 && $y2 < $height ) {
299 // Which color for this line?
300 $ind = floor ( $numcolors * $i / $dim );
301 if ( $ind >= $numcolors ) {
305 imageline ( $image, $x1, $y1, $x2, $y2, $colors[$ind] );
317 if ( function_exists ( "imagepng" ) ) {
318 Header ( "Content-type: image/png" );
320 } else if ( function_exists ( "imagegif" ) ) {
321 Header ( "Content-type: image/gif" );
324 echo "No image formats supported!<br />\n";
327 imagedestroy ( $image );