Overview

Packages

  • Image
  • None

Classes

  • Horde_Image
  • Horde_Image_Base
  • Horde_Image_Effect
  • Horde_Image_Effect_Border
  • Horde_Image_Effect_Gd_DropShadow
  • Horde_Image_Effect_Gd_RoundCorners
  • Horde_Image_Effect_Gd_TextWatermark
  • Horde_Image_Effect_Gd_Unsharpmask
  • Horde_Image_Effect_Im_Border
  • Horde_Image_Effect_Im_CenterCrop
  • Horde_Image_Effect_Im_Composite
  • Horde_Image_Effect_Im_DropShadow
  • Horde_Image_Effect_Im_LiquidResize
  • Horde_Image_Effect_Im_PhotoStack
  • Horde_Image_Effect_Im_PolaroidImage
  • Horde_Image_Effect_Im_RoundCorners
  • Horde_Image_Effect_Im_TextWatermark
  • Horde_Image_Effect_Im_Unsharpmask
  • Horde_Image_Effect_Imagick_Border
  • Horde_Image_Effect_Imagick_CenterCrop
  • Horde_Image_Effect_Imagick_Composite
  • Horde_Image_Effect_Imagick_DropShadow
  • Horde_Image_Effect_Imagick_LiquidResize
  • Horde_Image_Effect_Imagick_PhotoStack
  • Horde_Image_Effect_Imagick_PolaroidImage
  • Horde_Image_Effect_Imagick_RoundCorners
  • Horde_Image_Effect_Imagick_SmartCrop
  • Horde_Image_Effect_Imagick_TextWatermark
  • Horde_Image_Effect_Imagick_Unsharpmask
  • Horde_Image_Exception
  • Horde_Image_Exif
  • Horde_Image_Exif_Base
  • Horde_Image_Exif_Bundled
  • Horde_Image_Exif_Exiftool
  • Horde_Image_Exif_Parser_Base
  • Horde_Image_Exif_Php
  • Horde_Image_Gd
  • Horde_Image_Im
  • Horde_Image_Imagick
  • Horde_Image_Png
  • Horde_Image_Svg
  • Horde_Image_Swf
  • Horde_Image_Translation
  • Overview
  • Package
  • Class
  • Tree
  1: <?php
  2: /**
  3:  * This class implements the Horde_Image:: API for SWF, using the PHP
  4:  * Ming extension.
  5:  *
  6:  * Copyright 2002-2012 Horde LLC (http://www.horde.org/)
  7:  *
  8:  * See the enclosed file COPYING for license information (LGPL). If you
  9:  * did not receive this file, see http://www.horde.org/licenses/lgpl21.
 10:  *
 11:  * @author  Chuck Hagenbuch <chuck@horde.org>
 12:  * @package Image
 13:  */
 14: class Horde_Image_Swf extends Horde_Image_Base {
 15: 
 16:     /**
 17:      * Capabilites of this driver.
 18:      *
 19:      * @var array
 20:      */
 21:     var $_capabilities = array('canvas');
 22: 
 23:     /**
 24:      * SWF root movie.
 25:      *
 26:      * @var resource
 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:         // FIXME: honor the 'background' parameter here.
 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:      * Display the movie.
 49:      */
 50:     function display()
 51:     {
 52:         $this->headers();
 53:         $this->_movie->output();
 54:     }
 55: 
 56:     /**
 57:      * Return the raw data for this image.
 58:      *
 59:      * @return string  The raw image data.
 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:      * Creates a color that can be accessed in this object. When a
 73:      * color is set, the rgba values are returned in an array.
 74:      *
 75:      * @param string $name  The name of the color.
 76:      *
 77:      * @return array  The red, green, blue, alpha values of the color.
 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:      * Draws a text string on the image in a specified location, with
107:      * the specified style information.
108:      *
109:      * @param string  $text       The text to draw.
110:      * @param integer $x          The left x coordinate of the start of the text string.
111:      * @param integer $y          The top y coordinate of the start of the text string.
112:      * @param string  $font       The font identifier you want to use for the text.
113:      * @param string  $color      The color that you want the text displayed in.
114:      * @param integer $direction  An integer that specifies the orientation of the text.
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:      * Draw a circle.
138:      *
139:      * @param integer $x      The x co-ordinate of the centre.
140:      * @param integer $y      The y co-ordinate of the centre.
141:      * @param integer $r      The radius of the circle.
142:      * @param string  $color  The line color of the circle.
143:      * @param string  $fill   The color to fill the circle.
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; // = tan(22.5 deg)
158:         $b = $r * 0.707106781; // = sqrt(2)/2 = sin(45 deg)
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:      * Draw a polygon based on a set of vertices.
176:      *
177:      * @param array $vertices  An array of x and y labeled arrays
178:      *                         (eg. $vertices[0]['x'], $vertices[0]['y'], ...).
179:      * @param string $color    The color you want to draw the polygon with.
180:      * @param string $fill     The color to fill the polygon.
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:             // If the color is an array and the vertices is a an array
211:             // of more than 2 points.
212:             return false;
213:         }
214:     }
215: 
216:     /**
217:      * Draw a rectangle.
218:      *
219:      * @param integer $x       The left x-coordinate of the rectangle.
220:      * @param integer $y       The top y-coordinate of the rectangle.
221:      * @param integer $width   The width of the rectangle.
222:      * @param integer $height  The height of the rectangle.
223:      * @param string  $color   The line color of the rectangle.
224:      * @param string  $fill    The color to fill the rectangle.
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:      * Draw a rectangle.
238:      *
239:      * @param integer $x       The left x-coordinate of the rectangle.
240:      * @param integer $y       The top y-coordinate of the rectangle.
241:      * @param integer $width   The width of the rectangle.
242:      * @param integer $height  The height of the rectangle.
243:      * @param integer $round   The width of the corner rounding.
244:      * @param string $color    The line color of the rectangle.
245:      * @param string $fill     The color to fill the rectangle.
246:      */
247:     function roundedRectangle($x, $y, $width, $height, $round, $color = 'black', $fill = 'none')
248:     {
249:         if ($round <= 0) {
250:             // Optimize out any calls with no corner rounding.
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:         // Set corner points to avoid lots of redundant math.
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:         // Start in the upper left.
278:         $p1 = Horde_Image::arcPoints($round, 180, 225);
279:         $p2 = Horde_Image::arcPoints($round, 225, 270);
280: 
281:         // Start at the lower left corner of the top left curve.
282:         $s->movePenTo($x1 + $p1['x1'], $y1 + $p1['y1']);
283: 
284:         // Draw the upper left corner.
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:         // Calculate the upper right points.
289:         $p3 = Horde_Image::arcPoints($round, 270, 315);
290:         $p4 = Horde_Image::arcPoints($round, 315, 360);
291: 
292:         // Connect the top left and right curves.
293:         $s->drawLineTo($x2 + $p3['x1'], $y2 + $p3['y1']);
294: 
295:         // Draw the upper right corner.
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:         // Calculate the lower right points.
300:         $p5 = Horde_Image::arcPoints($round, 0, 45);
301:         $p6 = Horde_Image::arcPoints($round, 45, 90);
302: 
303:         // Connect the top right and lower right curves.
304:         $s->drawLineTo($x3 + $p5['x1'], $y3 + $p5['y1']);
305: 
306:         // Draw the lower right corner.
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:         // Calculate the lower left points.
311:         $p7 = Horde_Image::arcPoints($round, 90, 135);
312:         $p8 = Horde_Image::arcPoints($round, 135, 180);
313: 
314:         // Connect the bottom right and bottom left curves.
315:         $s->drawLineTo($x4 + $p7['x1'], $y4 + $p7['y1']);
316: 
317:         // Draw the lower left corner.
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:         // Close the shape.
322:         $s->drawLineTo($x1 + $p1['x1'], $y1 + $p1['y1']);
323: 
324:         return $this->_movie->add($s);
325:     }
326: 
327:     /**
328:      * Draw a line.
329:      *
330:      * @param integer $x0     The x co-ordinate of the start.
331:      * @param integer $y0     The y co-ordinate of the start.
332:      * @param integer $x1     The x co-ordinate of the end.
333:      * @param integer $y1     The y co-ordinate of the end.
334:      * @param string  $color  The line color.
335:      * @param string  $width  The width of the line.
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:      * Draw a dashed line.
355:      *
356:      * @param integer $x0           The x co-ordinate of the start.
357:      * @param integer $y0           The y co-ordinate of the start.
358:      * @param integer $x1           The x co-ordinate of the end.
359:      * @param integer $y1           The y co-ordinate of the end.
360:      * @param string  $color        The line color.
361:      * @param string  $width        The width of the line.
362:      * @param integer $dash_length  The length of a dash on the dashed line
363:      * @param integer $dash_space   The length of a space in the dashed line
364:      */
365:     function dashedLine($x0, $y0, $x1, $y1, $color = 'black', $width = 1, $dash_length = 2, $dash_space = 2)
366:     {
367:         // Get the length of the line in pixels.
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:         // Draw the dashed line.
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:      * Draw a polyline (a non-closed, non-filled polygon) based on a
389:      * set of vertices.
390:      *
391:      * @param array   $vertices  An array of x and y labeled arrays
392:      *                           (eg. $vertices[0]['x'], $vertices[0]['y'], ...).
393:      * @param string  $color     The color you want to draw the line with.
394:      * @param string  $width     The width of the line.
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:      * Draw an arc.
417:      *
418:      * @param integer $x      The x co-ordinate of the centre.
419:      * @param integer $y      The y co-ordinate of the centre.
420:      * @param integer $r      The radius of the arc.
421:      * @param integer $start  The start angle of the arc.
422:      * @param integer $end    The end angle of the arc.
423:      * @param string $color   The line color of the arc.
424:      * @param string $fill    The fill color of the arc.
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:                 // If we are on the first section, move the pen to the
452:                 // centre and draw out to the edge.
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:                 // Draw the arc.
461:                 $s->drawCurveTo($pts['x3'] + $x, $pts['y3'] + $y, $pts['x2'] + $x, $pts['y2'] + $y);
462:             }
463: 
464:             if ($fill != 'none') {
465:                 // Draw a line from the edge back to the centre to close
466:                 // off the segment.
467:                 $s->drawLineTo($x, $y);
468:             }
469:         }
470: 
471:         return $this->_movie->add($s);
472:     }
473: 
474:     /**
475:      * Draw a rectangle filled with a gradient from $color1 to
476:      * $color2.
477:      *
478:      * @param integer $x       The left x-coordinate of the rectangle.
479:      * @param integer $y       The top y-coordinate of the rectangle.
480:      * @param integer $width   The width of the rectangle.
481:      * @param integer $height  The height of the rectangle.
482:      * @param string  $color   The outline color of the rectangle.
483:      * @param string  $fill1   The name of the start color for the gradient.
484:      * @param string  $fill2   The name of the end color for the gradient.
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: 
API documentation generated by ApiGen