File: gradient.php

package info (click to toggle)
webcalendar 0.9.45-4sarge7
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 2,888 kB
  • ctags: 4,775
  • sloc: php: 16,990; sql: 1,272; perl: 777; sh: 120; makefile: 45
file content (171 lines) | stat: -rw-r--r-- 4,694 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
<?php
/*
 * $Id: gradient.php,v 1.3 2004/11/29 10:18:17 musus Exp $
 *
 * Description:
 *	Generate a gradient image for use as a background image.
 *	Required gd module.
 *
 * Input Parameters:
 *	height - Height of output image (width is 1)
 *	colors - Number of colors to generate
 *	base - Base color specified as "base=FFC0C0" or "base=#FFC0C0"
 *
 * Security:
 *	No security restrictions by user.
 *	Limit height parameter to 600.
 */

$MAX_HEIGHT = 600;
$MIN_COLORS = 4;
$MAX_COLORS = 256;
$DEFAULT_COLORS = 32;
$PERCENT = 15; // Percent to change brightness

$width = 5; // could be 1, doesn't really matter since browser will stretch it

// Get a value from a GET URL
function getGetValue ( $name ) {
  if ( ! empty ( $_GET[$name] ) )
    return $_GET[$name];
  if ( ! isset ( $HTTP_GET_VARS ) )
    return null;
  if ( ! isset ( $HTTP_GET_VARS[$name] ) )
    return null;
  return ( $HTTP_GET_VARS[$name] );
}

// Convert a hex value to an integer
function hextoint ( $val ) {
  if ( empty ( $val ) )
    return 0;
  switch ( strtoupper ( $val ) ) {
    case "0": return 0;
    case "1": return 1;
    case "2": return 2;
    case "3": return 3;
    case "4": return 4;
    case "5": return 5;
    case "6": return 6;
    case "7": return 7;
    case "8": return 8;
    case "9": return 9;
    case "A": return 10;
    case "B": return 11;
    case "C": return 12;
    case "D": return 13;
    case "E": return 14;
    case "F": return 15;
  }
  return 0;
}

$height = getGetValue ( "height" );
if ( empty ( $height ) )
  $height = 50;
if ( $height > $MAX_HEIGHT )
  $height = $MAX_HEIGHT;

$numcolors = getGetValue ( "colors" );
if ( empty ( $numcolors ) )
  $numcolors = $DEFAULT_COLORS;
else {
  if ( preg_match ( "/^\d+$/", $numcolors ) ) {
    if ( $numcolors < $MIN_COLORS )
      $numcolors = $MIN_COLORS;
    else if ( $numcolors > $MAX_COLORS )
      $numcolors = $MAX_COLORS;
  } else {
    $numcolors = $DEFAULT_COLORS;
  }
}

// Get base color
$red = $green = $blue = 192;
$base = getGetValue ( "base" );
if ( empty ( $base ) )
  $base = "C0C0C0";

if ( preg_match ( "/^#(\S+)/", $base, $matches ) ) {
  $base = $matches[1];
}
if ( preg_match ( "/^[0-9a-fA-F]+$/", $base ) ) {
  // valid color
} else {
  // Invalid color
  $base = "C0C0C0";
}

if ( strlen ( $base ) == 6 ) {
  $red = 16 * hextoint ( substr ( $base, 0, 1 ) ) +
    hextoint ( substr ( $base, 1, 1 ) );
  $green = 16 * hextoint ( substr ( $base, 2, 1 ) ) +
    hextoint ( substr ( $base, 3, 1 ) );
  $blue = 16 * hextoint ( substr ( $base, 4, 1 ) ) +
    hextoint ( substr ( $base, 5, 1 ) );
} else if ( strlen ( $base ) == 3 ) {
  $red = 16 * hextoint ( substr ( $base, 0, 1 ) );
  $green = 16 * hextoint ( substr ( $base, 1, 1 ) );
  $blue = 16 * hextoint ( substr ( $base, 2, 1 ) );
} else {
  // Invalid color specification
}

//echo "Base=$base<br />";
//echo "Red=$red<br />Green=$green<br />Blue=$blue<br />\n"; exit;
$image = imagecreate ( $width, $height );

// Allocate array of colors
$colors = array ();
$delta = 256 * $PERCENT / 100;
$deltared = $deltagreen = $deltablue = $delta;
if ( $red + $deltared > 255 )
  $deltared = 255 - $red;
if ( $green + $deltagreen > 255 )
  $deltagreen = 255 - $green;
if ( $blue + $deltablue > 255 )
  $deltablue = 255 - $blue;
//echo "deltared=$deltared<br />deltagreen=$deltagreen<br />deltablue=$deltablue<br />";
//echo "red=$red<br />green=$green<br />blue=$blue<br />";
for ( $i = 0; $i < $numcolors; $i++ ) {
  $thisdelta = ceil ($delta * $i / $numcolors);
  //echo "thisdelta=$thisdelta<br />";
  $thisred = $red + ( $deltared * $i / $numcolors );
  if ( $thisread > 255 )
    $thisread = 255;
  $thisgreen = $green + ( $deltagreen * $i / $numcolors );
  if ( $thisgreen > 255 )
    $thisgreen = 255;
  $thisblue = $blue + ( $deltablue * $i / $numcolors );
  if ( $thisblue > 255 )
    $thisblue = 255;
  $thisred = floor ( $thisred );
  $thisgreen = floor ( $thisgreen );
  $thisblue = floor ( $thisblue );
  $colors[$i] = imagecolorallocate ( $image, $thisred, $thisgreen, $thisblue );
  //echo "Color $i: $thisred $thisgreen $thisblue<br />";
}

for ( $i = 0; $i < $height; $i++ ) {
  // Which color for this line?
  $ind = floor ( $numcolors * $i / $height );
  if ( $ind >= $numcolors )
    $ind = $numcolors;
  $y = $height - $i - 1;
  imageline ( $image, 0, $y, $width - 1, $y,
    $colors[$ind] );
  //echo "Line $i, color $ind<br />";
}
//exit;

if ( function_exists ( "imagepng" ) ) {
  Header ( "Content-type: image/png" );
  imagepng ( $image );
} else if ( function_exists ( "imagegif" ) ) {
  Header ( "Content-type: image/gif" );
  imagegif ( $image );
} else {
  echo "No image formats supported!<br />\n";
}
imagedestroy ( $image );
?>