1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13:
14: class Horde_Image_Swf extends Horde_Image_Base {
15:
16: 17: 18: 19: 20:
21: var $_capabilities = array('canvas');
22:
23: 24: 25: 26: 27:
28: var $_movie;
29:
30: function Horde_Image_swf($params)
31: {
32: parent::Horde_Image($params);
33:
34: $this->_movie = new SWFMovie();
35: $this->_movie->setDimension($this->_width, $this->_height);
36:
37:
38: $this->_movie->setBackground(0xff, 0xff, 0xff);
39: $this->_movie->setRate(30);
40: }
41:
42: function getContentType()
43: {
44: return 'application/x-shockwave-flash';
45: }
46:
47: 48: 49:
50: function display()
51: {
52: $this->headers();
53: $this->_movie->output();
54: }
55:
56: 57: 58: 59: 60:
61: function raw()
62: {
63: ob_start();
64: $this->_movie->output();
65: $data = ob_get_contents();
66: ob_end_clean();
67:
68: return $data;
69: }
70:
71: 72: 73: 74: 75: 76: 77: 78:
79: function allocateColor($name)
80: {
81: list($r, $g, $b) = $this->getRGB($name);
82: return array('red' => $r,
83: 'green' => $g,
84: 'blue' => $b,
85: 'alpha' => 255);
86: }
87:
88: function getFont($font)
89: {
90: switch ($font) {
91: case 'sans-serif':
92: return '_sans';
93:
94: case 'serif':
95: return '_serif';
96:
97: case 'monospace':
98: return '_typewriter';
99:
100: default:
101: return $font;
102: }
103: }
104:
105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115:
116: function text($string, $x, $y, $font = 'monospace', $color = 'black', $direction = 0)
117: {
118: $color = $this->allocateColor($color);
119:
120: if (!strncasecmp(PHP_OS, 'WIN', 3)) {
121: $text = new SWFTextField(SWFTEXTFIELD_NOEDIT);
122: } else {
123: $text = new SWFText();
124: }
125: $text->setColor($color['red'], $color['green'], $color['blue'], $color['alpha']);
126: $text->addString($string);
127: $text->setFont(new SWFFont($this->getFont($font)));
128:
129: $t = $this->_movie->add($text);
130: $t->moveTo($x, $y);
131: $t->rotate($direction);
132:
133: return $t;
134: }
135:
136: 137: 138: 139: 140: 141: 142: 143: 144:
145: function circle($x, $y, $r, $color, $fill = 'none')
146: {
147: $s = new SWFShape();
148: $color = $this->allocateColor($color);
149: $s->setLine(1, $color['red'], $color['green'], $color['blue'], $color['alpha']);
150:
151: if ($fill != 'none') {
152: $fillColor = $this->allocateColor($fill);
153: $f = $s->addFill($fillColor['red'], $fillColor['green'], $fillColor['blue'], $fillColor['alpha']);
154: $s->setRightFill($f);
155: }
156:
157: $a = $r * 0.414213562;
158: $b = $r * 0.707106781;
159:
160: $s->movePenTo($x + $r, $y);
161:
162: $s->drawCurveTo($x + $r, $y - $a, $x + $b, $y - $b);
163: $s->drawCurveTo($x + $a, $y - $r, $x, $y - $r);
164: $s->drawCurveTo($x - $a, $y - $r, $x - $b, $y - $b);
165: $s->drawCurveTo($x - $r, $y - $a, $x - $r, $y);
166: $s->drawCurveTo($x - $r, $y + $a, $x - $b, $y + $b);
167: $s->drawCurveTo($x - $a, $y + $r, $x, $y + $r);
168: $s->drawCurveTo($x + $a, $y + $r, $x + $b, $y + $b);
169: $s->drawCurveTo($x + $r, $y + $a, $x + $r, $y);
170:
171: return $this->_movie->add($s);
172: }
173:
174: 175: 176: 177: 178: 179: 180: 181:
182: function polygon($verts, $color, $fill = 'none')
183: {
184: $color = $this->allocateColor($color);
185:
186: if (is_array($color) && is_array($verts) && (sizeof($verts) > 2)) {
187: $shape = new SWFShape();
188: $shape->setLine(1, $color['red'], $color['green'], $color['blue'], $color['alpha']);
189:
190: if ($fill != 'none') {
191: $fillColor = $this->allocateColor($fill);
192: $f = $shape->addFill($fillColor['red'], $fillColor['green'], $fillColor['blue'], $fillColor['alpha']);
193: $shape->setRightFill($f);
194: }
195:
196: $first_done = false;
197: foreach ($verts as $value) {
198: if (!$first_done) {
199: $shape->movePenTo($value['x'], $value['y']);
200: $first_done = true;
201: $first_x = $value['x'];
202: $first_y = $value['y'];
203: }
204: $shape->drawLineTo($value['x'], $value['y']);
205: }
206: $shape->drawLineTo($first_x, $first_y);
207:
208: return $this->_movie->add($shape);
209: } else {
210:
211:
212: return false;
213: }
214: }
215:
216: 217: 218: 219: 220: 221: 222: 223: 224: 225:
226: function rectangle($x, $y, $width, $height, $color, $fill = 'none')
227: {
228: $verts[0] = array('x' => $x, 'y' => $y);
229: $verts[1] = array('x' => $x + $width, 'y' => $y);
230: $verts[2] = array('x' => $x + $width, 'y' => $y + $height);
231: $verts[3] = array('x' => $x, 'y' => $y + $height);
232:
233: return $this->polygon($verts, $color, $fill);
234: }
235:
236: 237: 238: 239: 240: 241: 242: 243: 244: 245: 246:
247: function roundedRectangle($x, $y, $width, $height, $round, $color = 'black', $fill = 'none')
248: {
249: if ($round <= 0) {
250:
251: return $this->rectangle($x, $y, $width, $height, $color, $fill);
252: }
253:
254: $s = new SWFShape();
255: $color = $this->allocateColor($color);
256: $s->setLine(1, $color['red'], $color['green'], $color['blue'], $color['alpha']);
257:
258: if ($fill != 'none') {
259: $fillColor = $this->allocateColor($fill);
260: $f = $s->addFill($fillColor['red'], $fillColor['green'], $fillColor['blue'], $fillColor['alpha']);
261: $s->setRightFill($f);
262: }
263:
264:
265: $x1 = $x + $round;
266: $y1 = $y + $round;
267:
268: $x2 = $x + $width - $round;
269: $y2 = $y + $round;
270:
271: $x3 = $x + $width - $round;
272: $y3 = $y + $height - $round;
273:
274: $x4 = $x + $round;
275: $y4 = $y + $height - $round;
276:
277:
278: $p1 = Horde_Image::arcPoints($round, 180, 225);
279: $p2 = Horde_Image::arcPoints($round, 225, 270);
280:
281:
282: $s->movePenTo($x1 + $p1['x1'], $y1 + $p1['y1']);
283:
284:
285: $s->drawCurveTo($x1 + $p1['x3'], $y1 + $p1['y3'], $x1 + $p1['x2'], $y1 + $p1['y2']);
286: $s->drawCurveTo($x1 + $p2['x3'], $y1 + $p2['y3'], $x1 + $p2['x2'], $y1 + $p2['y2']);
287:
288:
289: $p3 = Horde_Image::arcPoints($round, 270, 315);
290: $p4 = Horde_Image::arcPoints($round, 315, 360);
291:
292:
293: $s->drawLineTo($x2 + $p3['x1'], $y2 + $p3['y1']);
294:
295:
296: $s->drawCurveTo($x2 + $p3['x3'], $y2 + $p3['y3'], $x2 + $p3['x2'], $y2 + $p3['y2']);
297: $s->drawCurveTo($x2 + $p4['x3'], $y2 + $p4['y3'], $x2 + $p4['x2'], $y2 + $p4['y2']);
298:
299:
300: $p5 = Horde_Image::arcPoints($round, 0, 45);
301: $p6 = Horde_Image::arcPoints($round, 45, 90);
302:
303:
304: $s->drawLineTo($x3 + $p5['x1'], $y3 + $p5['y1']);
305:
306:
307: $s->drawCurveTo($x3 + $p5['x3'], $y3 + $p5['y3'], $x3 + $p5['x2'], $y3 + $p5['y2']);
308: $s->drawCurveTo($x3 + $p6['x3'], $y3 + $p6['y3'], $x3 + $p6['x2'], $y3 + $p6['y2']);
309:
310:
311: $p7 = Horde_Image::arcPoints($round, 90, 135);
312: $p8 = Horde_Image::arcPoints($round, 135, 180);
313:
314:
315: $s->drawLineTo($x4 + $p7['x1'], $y4 + $p7['y1']);
316:
317:
318: $s->drawCurveTo($x4 + $p7['x3'], $y4 + $p7['y3'], $x4 + $p7['x2'], $y4 + $p7['y2']);
319: $s->drawCurveTo($x4 + $p8['x3'], $y4 + $p8['y3'], $x4 + $p8['x2'], $y4 + $p8['y2']);
320:
321:
322: $s->drawLineTo($x1 + $p1['x1'], $y1 + $p1['y1']);
323:
324: return $this->_movie->add($s);
325: }
326:
327: 328: 329: 330: 331: 332: 333: 334: 335: 336:
337: function line($x1, $y1, $x2, $y2, $color = 'black', $width = 1)
338: {
339: $color = $this->allocateColor($color);
340:
341: if (is_array($color)) {
342: $shape = new SWFShape();
343: $shape->setLine($width, $color['red'], $color['green'], $color['blue'], $color['alpha']);
344: $shape->movePenTo($x1, $y1);
345: $shape->drawLineTo($x2, $y2);
346:
347: return $this->_movie->add($shape);
348: } else {
349: return false;
350: }
351: }
352:
353: 354: 355: 356: 357: 358: 359: 360: 361: 362: 363: 364:
365: function dashedLine($x0, $y0, $x1, $y1, $color = 'black', $width = 1, $dash_length = 2, $dash_space = 2)
366: {
367:
368: $line_length = max(ceil(sqrt(pow(($x1 - $x0), 2) + pow(($y1 - $y0), 2))), 2);
369:
370: $cosTheta = ($x1 - $x0) / $line_length;
371: $sinTheta = ($y1 - $y0) / $line_length;
372: $lastx = $x0;
373: $lasty = $y0;
374:
375:
376: for ($i = 0; $i < $line_length; $i += ($dash_length + $dash_space)) {
377: $x = ($dash_length * $cosTheta) + $lastx;
378: $y = ($dash_length * $sinTheta) + $lasty;
379:
380: $this->line($lastx, $lasty, $x, $y, $color);
381:
382: $lastx = $x + ($dash_space * $cosTheta);
383: $lasty = $y + ($dash_space * $sinTheta);
384: }
385: }
386:
387: 388: 389: 390: 391: 392: 393: 394: 395:
396: function polyline($verts, $color, $width = 1)
397: {
398: $color = $this->allocateColor($color);
399:
400: $shape = new SWFShape();
401: $shape->setLine($width, $color['red'], $color['green'], $color['blue'], $color['alpha']);
402:
403: $first_done = false;
404: foreach ($verts as $value) {
405: if (!$first_done) {
406: $shape->movePenTo($value['x'], $value['y']);
407: $first_done = true;
408: }
409: $shape->drawLineTo($value['x'], $value['y']);
410: }
411:
412: return $this->_movie->add($shape);
413: }
414:
415: 416: 417: 418: 419: 420: 421: 422: 423: 424: 425:
426: function arc($x, $y, $r, $start, $end, $color = 'black', $fill = 'none')
427: {
428: $s = new SWFShape();
429: $color = $this->allocateColor($color);
430: $s->setLine(1, $color['red'], $color['green'], $color['blue'], $color['alpha']);
431:
432: if ($fill != 'none') {
433: $fillColor = $this->allocateColor($fill);
434: $f = $s->addFill($fillColor['red'], $fillColor['green'], $fillColor['blue'], $fillColor['alpha']);
435: $s->setRightFill($f);
436: }
437:
438: if ($end - $start <= 45) {
439: $pts = Horde_Image::arcPoints($r, $start, $end);
440: $s->movePenTo($x, $y);
441: $s->drawLineTo($pts['x1'] + $x, $pts['y1'] + $y);
442: $s->drawCurveTo($pts['x3'] + $x, $pts['y3'] + $y, $pts['x2'] + $x, $pts['y2'] + $y);
443: $s->drawLineTo($x, $y);
444: } else {
445: $sections = ceil(($end - $start) / 45);
446: for ($i = 0; $i < $sections; $i++) {
447: $pts = Horde_Image::arcPoints($r, $start + ($i * 45), ($start + (($i + 1) * 45) > $end)
448: ? $end
449: : ($start + (($i + 1) * 45)));
450:
451:
452:
453: if ($i == 0 && $fill != 'none') {
454: $s->movePenTo($x, $y);
455: $s->drawLineTo($pts['x1'] + $x, $pts['y1'] + $y);
456: } else {
457: $s->movePenTo($pts['x1'] + $x, $pts['y1'] + $y);
458: }
459:
460:
461: $s->drawCurveTo($pts['x3'] + $x, $pts['y3'] + $y, $pts['x2'] + $x, $pts['y2'] + $y);
462: }
463:
464: if ($fill != 'none') {
465:
466:
467: $s->drawLineTo($x, $y);
468: }
469: }
470:
471: return $this->_movie->add($s);
472: }
473:
474: 475: 476: 477: 478: 479: 480: 481: 482: 483: 484: 485:
486: function gradientRectangle($x, $y, $width, $height, $color = 'black', $fill1 = 'black', $fill2 = 'white')
487: {
488: $s = new SWFShape();
489:
490: if ($color != 'none') {
491: $color = $this->allocateColor($color);
492: $s->setLine(1, $color['red'], $color['green'], $color['blue'], $color['alpha']);
493: }
494:
495: $fill1 = $this->allocateColor($fill1);
496: $fill2 = $this->allocateColor($fill2);
497: $gradient = new SWFGradient();
498: $gradient->addEntry(0.0, $fill1['red'], $fill1['green'], $fill1['blue'], $fill1['alpha']);
499: $gradient->addEntry(1.0, $fill2['red'], $fill2['green'], $fill2['blue'], $fill2['alpha']);
500:
501: $f = $s->addFill($gradient, SWFFILL_LINEAR_GRADIENT);
502: $f->scaleTo($width / $this->_width);
503: $f->moveTo($x, $y);
504: $s->setRightFill($f);
505:
506: $verts[0] = array('x' => $x, 'y' => $y);
507: $verts[1] = array('x' => $x + $width, 'y' => $y);
508: $verts[2] = array('x' => $x + $width, 'y' => $y + $height);
509: $verts[3] = array('x' => $x, 'y' => $y + $height);
510:
511: $first_done = false;
512: foreach ($verts as $vert) {
513: if (!$first_done) {
514: $s->movePenTo($vert['x'], $vert['y']);
515: $first_done = true;
516: $first_x = $vert['x'];
517: $first_y = $vert['y'];
518: }
519: $s->drawLineTo($vert['x'], $vert['y']);
520: }
521: $s->drawLineTo($first_x, $first_y);
522:
523: return $this->_movie->add($s);
524: }
525:
526: }
527: