1 | <?php |
---|
2 | /*******************************************************************\ |
---|
3 | * eGroupWare - GD Image * |
---|
4 | * http://www.egroupware.org * |
---|
5 | * * |
---|
6 | * Written by by Bettina Gille [ceb@phpgroupware.org] * |
---|
7 | * * |
---|
8 | * Creates images using GD graphics library * |
---|
9 | * Copyright (C) 2003 Free Software Foundation, Inc * |
---|
10 | * ----------------------------------------------------------------- * |
---|
11 | * This class based on htmlGD.php3 * |
---|
12 | * Double Choco Latte - Source Configuration Management System * |
---|
13 | * Copyright (C) 1999 Michael L. Dean & Tim R. Norman * |
---|
14 | * ----------------------------------------------------------------- * |
---|
15 | * This library is part of the eGroupWare API * |
---|
16 | * ----------------------------------------------------------------- * |
---|
17 | * This library is free software; you can redistribute it and/or * |
---|
18 | * modify it under the terms of the GNU General Public License as * |
---|
19 | * published by the Free Software Foundation; either version 2 of * |
---|
20 | * the License, or (at your option) any later version. * |
---|
21 | * * |
---|
22 | * This program is distributed in the hope that it will be useful, * |
---|
23 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
---|
24 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * |
---|
25 | * General Public License for more details. * |
---|
26 | * * |
---|
27 | * You should have received a copy of the GNU General Public License * |
---|
28 | * along with this program; if not, write to the Free Software * |
---|
29 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. * |
---|
30 | \*******************************************************************/ |
---|
31 | |
---|
32 | class gdimage |
---|
33 | { |
---|
34 | var $filename; |
---|
35 | var $type; |
---|
36 | var $cur_x; |
---|
37 | var $cur_y; |
---|
38 | var $width; |
---|
39 | var $height; |
---|
40 | var $hImage; |
---|
41 | var $colormap; |
---|
42 | var $hColor; |
---|
43 | var $font; |
---|
44 | |
---|
45 | function gdimage() |
---|
46 | { |
---|
47 | $this->gd = $this->check_gd(); |
---|
48 | |
---|
49 | if ($this->gd == 0) |
---|
50 | { |
---|
51 | echo 'Your PHP installation does not seem to have the required GD library. |
---|
52 | Please see the PHP documentation on how to install and enable the GD library.'; |
---|
53 | exit; |
---|
54 | } |
---|
55 | |
---|
56 | $this->cur_x = 0; |
---|
57 | $this->cur_y = 0; |
---|
58 | $this->width = 0; |
---|
59 | $this->height = 0; |
---|
60 | $this->hImage = 0; |
---|
61 | $this->colormap = array(); |
---|
62 | $this->hColor = 0; |
---|
63 | $this->font = 0; |
---|
64 | $this->type = 'png'; |
---|
65 | $this->temp_file = PHPGW_SERVER_ROOT . SEP . 'phpgwapi' . SEP . 'images' . SEP . 'draw_tmp.png'; |
---|
66 | } |
---|
67 | |
---|
68 | function check_gd() |
---|
69 | { |
---|
70 | ob_start(); |
---|
71 | phpinfo(8); // Just get the modules loaded |
---|
72 | $a = ob_get_contents(); |
---|
73 | ob_end_clean(); |
---|
74 | |
---|
75 | if(preg_match('/.*GD Version.*(1[0-9|\.]+).*/',$a,$m)) |
---|
76 | { |
---|
77 | $r=1; //$v=$m[1]; |
---|
78 | } |
---|
79 | elseif(preg_match('/.*GD Version.*(2[0-9|\.]+).*/',$a,$m)) |
---|
80 | { |
---|
81 | $r=2; //$v=$m[1]; |
---|
82 | } |
---|
83 | else |
---|
84 | { |
---|
85 | $r=0; //$v=$m[1]; |
---|
86 | } |
---|
87 | return $r; |
---|
88 | } |
---|
89 | |
---|
90 | function Init() |
---|
91 | { |
---|
92 | $this->hImage = ImageCreate($this->width, $this->height) or die; |
---|
93 | return True; |
---|
94 | } |
---|
95 | |
---|
96 | function Done() |
---|
97 | { |
---|
98 | ImageDestroy($this->hImage); |
---|
99 | } |
---|
100 | |
---|
101 | function MoveTo($x, $y) |
---|
102 | { |
---|
103 | if ($x >= 0 && $x <= $this->width && $y >= 0 && $y <= $this->height) |
---|
104 | { |
---|
105 | $this->cur_x = $x; |
---|
106 | $this->cur_y = $y; |
---|
107 | |
---|
108 | return true; |
---|
109 | } |
---|
110 | return false; |
---|
111 | } |
---|
112 | |
---|
113 | function LineTo($x, $y, $linestyle = 'solid') |
---|
114 | { |
---|
115 | if ($x >= 0 && $x <= $this->width && $y >= 0 && $y <= $this->height) |
---|
116 | { |
---|
117 | if ($linestyle == 'dashed') |
---|
118 | { |
---|
119 | ImageDashedLine($this->hImage, $this->cur_x, $this->cur_y, $x, $y, $this->hColor); |
---|
120 | } |
---|
121 | else |
---|
122 | { |
---|
123 | ImageLine($this->hImage, $this->cur_x, $this->cur_y, $x, $y, $this->hColor); |
---|
124 | } |
---|
125 | |
---|
126 | $this->cur_x = $x; |
---|
127 | $this->cur_y = $y; |
---|
128 | |
---|
129 | return true; |
---|
130 | } |
---|
131 | |
---|
132 | return false; |
---|
133 | } |
---|
134 | |
---|
135 | function Line($x1, $y1, $x2, $y2, $linestyle = 'solid') |
---|
136 | { |
---|
137 | if ($x1 >= 0 && $x1 <= $this->width && $y1 >= 0 && $y1 <= $this->height && $x2 >= 0 && $x2 <= $this->width && $y2 >= 0 && $y2 <= $this->height) |
---|
138 | { |
---|
139 | if ($linestyle == 'solid') |
---|
140 | { |
---|
141 | ImageLine($this->hImage, $x1, $y1, $x2, $y2, $this->hColor); |
---|
142 | } |
---|
143 | else |
---|
144 | { |
---|
145 | ImageDashedLine($this->hImage, $x1, $y1, $x2, $y2, $this->hColor); |
---|
146 | } |
---|
147 | |
---|
148 | $this->cur_x = $x2; |
---|
149 | $this->cur_y = $y2; |
---|
150 | |
---|
151 | return true; |
---|
152 | } |
---|
153 | |
---|
154 | return false; |
---|
155 | } |
---|
156 | |
---|
157 | function SetColor($r, $g, $b, $set_transparent=False) |
---|
158 | { |
---|
159 | $key = "$r,$g,$b"; |
---|
160 | if (!IsSet($this->colormap[$key])) |
---|
161 | { |
---|
162 | $this->hColor = ImageColorAllocate($this->hImage, $r, $g, $b); |
---|
163 | $this->colormap[$key] = $this->hColor; |
---|
164 | } |
---|
165 | else |
---|
166 | { |
---|
167 | $this->hColor = $this->colormap[$key]; |
---|
168 | } |
---|
169 | if ($set_transparent) |
---|
170 | { |
---|
171 | ImageColorTransparent($this->hImage,$this->hColor); |
---|
172 | } |
---|
173 | |
---|
174 | return true; |
---|
175 | } |
---|
176 | |
---|
177 | function SetColorByName($name) |
---|
178 | { |
---|
179 | $r = 0; |
---|
180 | $g = 0; |
---|
181 | $b = 0; |
---|
182 | switch ($name) |
---|
183 | { |
---|
184 | case 'red': |
---|
185 | $r = 180; |
---|
186 | break; |
---|
187 | case 'green': |
---|
188 | $g = 180; |
---|
189 | break; |
---|
190 | case 'blue': |
---|
191 | $b = 180; |
---|
192 | break; |
---|
193 | case 'bright red': |
---|
194 | $r = 255; |
---|
195 | break; |
---|
196 | case 'bright green': |
---|
197 | $g = 255; |
---|
198 | break; |
---|
199 | case 'bright blue': |
---|
200 | $b = 255; |
---|
201 | break; |
---|
202 | case 'dark red': |
---|
203 | $r = 80; |
---|
204 | break; |
---|
205 | case 'dark green': |
---|
206 | $g = 80; |
---|
207 | break; |
---|
208 | case 'dark blue': |
---|
209 | $b = 80; |
---|
210 | break; |
---|
211 | case 'yellow': |
---|
212 | $r = 255; |
---|
213 | $g = 215; |
---|
214 | break; |
---|
215 | } |
---|
216 | |
---|
217 | return $this->SetColor($r, $g, $b); |
---|
218 | } |
---|
219 | |
---|
220 | function SetFont($font) |
---|
221 | { |
---|
222 | if ($font < 1 || $font > 5) |
---|
223 | { |
---|
224 | return false; |
---|
225 | } |
---|
226 | |
---|
227 | $this->font = $font; |
---|
228 | |
---|
229 | return true; |
---|
230 | } |
---|
231 | |
---|
232 | function GetFontHeight() |
---|
233 | { |
---|
234 | return ImageFontHeight($this->font); |
---|
235 | } |
---|
236 | |
---|
237 | function GetFontWidth() |
---|
238 | { |
---|
239 | return ImageFontWidth($this->font); |
---|
240 | } |
---|
241 | |
---|
242 | function DrawText($params) |
---|
243 | { |
---|
244 | $text = $params['text']; |
---|
245 | $direction = (isset($params['direction'])?$params['direction']:''); |
---|
246 | $justification = (isset($params['justification'])?$params['justification']:'center'); |
---|
247 | $margin_left = (isset($params['margin_left'])?$params['margin_left']:''); |
---|
248 | |
---|
249 | $textwidth = ImageFontWidth($this->font) * strlen($text); |
---|
250 | |
---|
251 | /*if (isset($margin_left) && $textwidth >= $margin_left) |
---|
252 | { |
---|
253 | $text = strlen($text) - 1 . '.'; |
---|
254 | }*/ |
---|
255 | |
---|
256 | if ($justification == 'center') |
---|
257 | { |
---|
258 | if ($direction == 'up') |
---|
259 | { |
---|
260 | $this->cur_y += $textwidth / 2; |
---|
261 | if ($this->cur_y > $this->height) |
---|
262 | { |
---|
263 | $this->cur_y = $this->height; |
---|
264 | } |
---|
265 | } |
---|
266 | else |
---|
267 | { |
---|
268 | $this->cur_x -= $textwidth / 2; |
---|
269 | if ($this->cur_x < 0) |
---|
270 | { |
---|
271 | $this->cur_x = 0; |
---|
272 | } |
---|
273 | } |
---|
274 | } |
---|
275 | elseif ($justification == 'right') |
---|
276 | { |
---|
277 | if ($direction == 'up') |
---|
278 | { |
---|
279 | $this->cur_y += $textwidth; |
---|
280 | if ($this->cur_y > $this->height) |
---|
281 | { |
---|
282 | $this->cur_y = $this->height; |
---|
283 | } |
---|
284 | } |
---|
285 | else |
---|
286 | { |
---|
287 | $this->cur_x -= $textwidth; |
---|
288 | if ($this->cur_x < 0) |
---|
289 | { |
---|
290 | $this->cur_x = 0; |
---|
291 | } |
---|
292 | } |
---|
293 | } |
---|
294 | |
---|
295 | if ($direction == 'up') |
---|
296 | { |
---|
297 | ImageStringUp($this->hImage, $this->font, $this->cur_x, $this->cur_y, $text, $this->hColor); |
---|
298 | } |
---|
299 | else |
---|
300 | { |
---|
301 | ImageString($this->hImage, $this->font, $this->cur_x, $this->cur_y, $text, $this->hColor); |
---|
302 | } |
---|
303 | |
---|
304 | return true; |
---|
305 | } |
---|
306 | |
---|
307 | function ToBrowser() |
---|
308 | { |
---|
309 | //header('Content-type: image/' . $this->type); |
---|
310 | switch ($this->type) |
---|
311 | { |
---|
312 | case 'png': |
---|
313 | ImagePNG($this->hImage,$this->temp_file); |
---|
314 | break; |
---|
315 | case 'gif': |
---|
316 | ImageGIF($this->hImage); |
---|
317 | break; |
---|
318 | case 'jpeg': |
---|
319 | ImageJPEG($this->hImage); |
---|
320 | break; |
---|
321 | } |
---|
322 | } |
---|
323 | } |
---|
324 | ?> |
---|