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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
| <?php
class AnalogClock
{
// These are for the clock system
var $im, $background, $timezone;
// This is called whenever a new clock is made
function AnalogClock($width, $height=0, $fill='FFFFFF')
{
$this->clockwidth = $width; // Clock width
$this->clockheight = ($height == 0)?$width:$height; // Clock height
$this->timezone = substr(date('O'), 0, 3); // Timezone
$this->im = imagecreatetruecolor($this->clockwidth, $this->clockheight);
imagefill($this->im, 0, 0, $this->AllocateColor($fill));
}
/******************************************************************************
* Alias (on) *
*******************************************************************************
* Add anti-aliasing to the clock. Only the PHP-bundled version of gd has *
* anti-aliasing support, so this becomes optional. There is only one *
* parameter, which defaults to TRUE. *
********************************** *
* Revision History * *
********************************** *
* v1.1 - This is a new function *
******************************************************************************/
function Alias ($on=TRUE)
{
imageantialias($this->im, $on); // Antialiasing
}
/******************************************************************************
* Background (file, [stretch]) *
*******************************************************************************
* Draws a background onto the clock. There are two parameters: the image *
* to draw, and whether or not to stretch that image across the clock image. *
********************************** *
* Revision History * *
********************************** *
* v1.1 - No longer requires DrawFace(), as it draws immediately *
******************************************************************************/
function Background($file, $stretch=FALSE)
{
$this->background = imagecreatefromstring(file_get_contents($file));
if ($stretch == FALSE)
{
imagecopy($this->im, $this->background, 0, 0, 0, 0, imagesx($this->background), imagesy($this->background));
}
else
{
imagecopyresampled($this->im, $this->background, 0, 0, 0, 0, $this->clockwidth, $this->clockheight, imagesx($this->background), imagesy($this->background));
}
}
/******************************************************************************
* NewHand (type, style, color, length, width, [filled]) *
*******************************************************************************
* Create a new hand to be drawn. The type is which hand (hour, min, or *
* sec). Color is the color of the hand. Length is the pixel length, from *
* the center (certain hand styles will draw further back). Width is the *
* pixel width of the current hand. Make filled TRUE if you want the hand to *
* be filled with the color. The style can be one of several: *
* - line - A line drawn from behind the center point by 1/3 of the length. *
* - triangle - Three points out from the center. *
* - diamond - Four points from the center. *
********************************** *
* Revision History * *
********************************** *
* v1.1 - Completely redone *
* No longer waits for DrawHands() *
* Made it a little easier to read through *
******************************************************************************/
function NewHand($type, $style, $color, $length, $width, $filled=FALSE)
{
switch ($type)
{
case 'hour':
$point = (gmdate('g') + $this->timezone);
if (date('I') == 1)
{
$point += 1;
}
$point = ($point + date('i') / 60) * 2 * M_PI / 12;
break;
case 'min':
$point = (date('i') + date('s') / 60) * 2 * M_PI / 60;
break;
case 'sec':
$point = date('s') * 2 * M_PI / 60;
break;
}
switch ($style)
{
case 'line':
$startx = $this->AngleToPixelX(-($length / 3), $point);
$starty = $this->AngleToPixelY(-($length / 3), $point);
$endx = $this->AngleToPixelX($length, $point);
$endy = $this->AngleToPixelY($length, $point);
if ($width == 1)
{
imageline($this->im, $startx, $starty, $endx, $endy, $this->AllocateColor($color));
return;
}
// This segment of code was borrowed from the PHP manual
// (I felt no need to rewrite it, as it does an excellent job.)
if ($startx != $endx)
$k = ($starty - $endy) / ($startx - $endx);
else
$k = $starty - $endy;
$a = (($width / 2) - 0.5) / sqrt(1 + pow($k, 2));
$points = array(
round($startx - (1 + $k) * $a), round($starty + (1 - $k) * $a),
round($startx - (1 - $k) * $a), round($starty - (1 + $k) * $a),
round($endx + (1 + $k) * $a), round($endy - (1 - $k) * $a),
round($endx + (1 - $k) * $a), round($endy + (1 + $k) * $a),
);
// End segment
$numpoints = 4;
break;
case 'triangle':
$x1 = $this->AngleToPixelX($width / 2, $point - (M_PI / 2));
$y1 = $this->AngleToPixelY($width / 2, $point - (M_PI / 2));
$x2 = $this->AngleToPixelX($width / 2, $point + (M_PI / 2));
$y2 = $this->AngleToPixelY($width / 2, $point + (M_PI / 2));
$x3 = $this->AngleToPixelX($length, $point);
$y3 = $this->AngleToPixelY($length, $point);
$points = array(
$x1, $y1,
$x2, $y2,
$x3, $y3
);
$numpoints = 3;
break;
case 'diamond':
$x1 = $this->AngleToPixelX(-$width, $point);
$y1 = $this->AngleToPixelY(-$width, $point);
$x2 = $this->AngleToPixelX($width / 2, $point - (M_PI / 2));
$y2 = $this->AngleToPixelY($width / 2, $point - (M_PI / 2));
$x3 = $this->AngleToPixelX($length, $point);
$y3 = $this->AngleToPixelY($length, $point);
$x4 = $this->AngleToPixelX($width / 2, $point + (M_PI / 2));
$y4 = $this->AngleToPixelY($width / 2, $point + (M_PI / 2));
$points = array(
$x1, $y1,
$x2, $y2,
$x3, $y3,
$x4, $y4
);
$numpoints = 4;
break;
}
if ($filled == TRUE)
{
imagefilledpolygon($this->im, $points, $numpoints, $this->AllocateColor($color));
}
imagepolygon($this->im, $points, $numpoints, $this->AllocateColor($color));
}
/******************************************************************************
* DrawFace (fill, border) *
*******************************************************************************
* Draws a basic clock face. Takes the face and border color. Only use *
* this if you don't want to use a background image. *
********************************** *
* Revision History * *
********************************** *
* v1.1 - No longer draws the background image *
* No longer required to be called *
******************************************************************************/
function DrawFace($fill, $border)
{
imagefilledarc($this->im, $this->clockwidth/2, $this->clockheight/2, $this->clockwidth-1, $this->clockheight-1, 0, 360, $this->AllocateColor($fill), 4);
imagearc($this->im, $this->clockwidth/2, $this->clockheight/2, $this->clockwidth-1, $this->clockheight-1, 0, 360, $this->AllocateColor($border));
}
/******************************************************************************
* DrawBullets (color, distance, [width, [height]]) *
*******************************************************************************
* Draws bullets for each hour. Rather limited, so it's best to use an *
* image for the background with the bullets already drawn on. *
********************************** *
* Revision History * *
********************************** *
* v1.1 - This is a new function *
******************************************************************************/
function DrawBullets($color, $distance, $width=4, $height=4)
{
for ($i = 0; $i < 12; $i++)
{
$num = $i * 2 * M_PI / 12;
$centerx = $this->AngleToPixelX($distance, $num);
$centery = $this->AngleToPixelY($distance, $num);
imagefilledellipse($this->im, $centerx, $centery, $width, $height, $this->AllocateColor($color));
}
}
/******************************************************************************
* DrawOverlay (file, [x, [y]]) *
*******************************************************************************
* Draw an overlay onto the clock. This preserves alpha transparency, so *
* using a PNG file will look the best. Additionally, you can specify the X *
* and Y coordinates. *
********************************** *
* Revision History * *
********************************** *
* None. *
******************************************************************************/
function DrawOverlay($file, $x=0, $y=0)
{
$overlay = imagecreatefromstring(file_get_contents($file));
imagecopy($this->im, $overlay, $x, $y, 0, 0, imagesx($overlay), imagesy($overlay));
imagedestroy($overlay);
}
/******************************************************************************
* AngleToPixelX (distance, angle) *
*******************************************************************************
* Calculate a pixel point from the center of the clock given a distance *
* and angle. Returns the X part of the point. *
********************************** *
* Revision History * *
********************************** *
* v1.1 - Calculates from the correct center *
******************************************************************************/
function AngleToPixelX($distance, $angle)
{
return intval(($this->clockwidth / 2) + ($distance * sin($angle)));
}
/******************************************************************************
* AngleToPixelY (distance, angle) *
*******************************************************************************
* Calculate a pixel point from the center of the clock given a distance *
* and an angle. Returns the Y part of the point. *
********************************** *
* Revision History * *
********************************** *
* v1.1 - Calculates from the correct center *
******************************************************************************/
function AngleToPixelY($distance, $angle)
{
return intval(($this->clockheight / 2) - ($distance * cos($angle)));
}
/******************************************************************************
* DrawLabel (font, X, Y, size, angle, string, color) *
*******************************************************************************
* Draw a TrueType Font label on the clock image. You must specify the *
* font file, the X and Y coordinates, font point size, angle, and the text to *
* write on the label. *
********************************** *
* Revision History * *
********************************** *
* v1.1 - Added color as a new parameter. *
******************************************************************************/
function DrawLabel($font, $x, $y, $size, $angle, $string, $color)
{
imagettftext($this->im, $size, $angle, $x, $y, $this->AllocateColor($color), $font, $string);
}
/******************************************************************************
* DrawPNG () *
*******************************************************************************
* Output a PNG image of the clock. *
********************************** *
* Revision History * *
********************************** *
* None. *
******************************************************************************/
function DrawPNG()
{
header('Content-type: image/png');
imagepng($this->im);
imagedestroy($this->im);
}
/******************************************************************************
* DrawJPEG ([quality]) *
*******************************************************************************
* Output a JPEG image of the clock. Quality of the JPEG, which defaults *
* at 75%, will make the image look better or worse. *
********************************** *
* Revision History * *
********************************** *
* None. *
******************************************************************************/
function DrawJPEG($quality=75)
{
header('Content-type: image/jpeg');
ob_start();
imagejpeg($this->im, '', $quality);
$imagedata = ob_get_contents();
ob_end_clean();
echo str_replace('CREATOR:','CREATOR: Analog Clock using',$imagedata);
imagedestroy($this->im);
}
/******************************************************************************
* DrawGIF () *
*******************************************************************************
* Output a GIF image of the clock. Note that if you do not have version *
* 2.0.28 or higher of gd, you will probably not have GIF support. *
********************************** *
* Revision History * *
********************************** *
* None. *
******************************************************************************/
function DrawGIF()
{
header('Content-type: image/gif');
imagegif($this->im);
imagedestroy($this->im);
}
/******************************************************************************
* FetchImage () *
*******************************************************************************
* Use this function if you plan on using the clock image as a gd image *
* resource. You can use any of gd's functions on this resource. *
********************************** *
* Revision History * *
********************************** *
* None. *
******************************************************************************/
function FetchImage()
{
return $this->im;
}
/******************************************************************************
* AllocateColor (color) *
*******************************************************************************
* Color is a hex representation of an RGB color. Since the clock uses *
* a truecolor image, any hex color will work. *
********************************** *
* Revision History * *
********************************** *
* None. *
******************************************************************************/
function AllocateColor($col)
{
return imagecolorclosest($this->im, hexdec(substr($col,0,2)), hexdec(substr($col,2,2)), hexdec(substr($col,4,2)));
}
}
?> |