Polygon.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. <?php
  2. namespace Grafika\Gd\DrawingObject;
  3. use Grafika\DrawingObject\Polygon as Base;
  4. use Grafika\DrawingObjectInterface;
  5. use Grafika\Gd\Editor;
  6. /**
  7. * Class Rectangle
  8. * @package Grafika
  9. */
  10. class Polygon extends Base implements DrawingObjectInterface
  11. {
  12. public function draw($image)
  13. {
  14. if(function_exists('imageantialias')){
  15. imageantialias($image->getCore(), true);
  16. }
  17. $points = $this->points();
  18. $count = count($this->points);
  19. // Create filled polygon
  20. if( null !== $this->fillColor){
  21. list($r, $g, $b, $alpha) = $this->getFillColor()->getRgba();
  22. $fillColorResource = imagecolorallocatealpha(
  23. $image->getCore(), $r, $g, $b,
  24. Editor::gdAlpha($alpha)
  25. );
  26. imagefilledpolygon($image->getCore(), $points,
  27. $count,
  28. $fillColorResource
  29. );
  30. }
  31. // Create polygon borders. It will be placed on top of the filled polygon (if present)
  32. if ( 0 < $this->getBorderSize() and null !== $this->borderColor) { // With border > 0 AND borderColor !== null
  33. list($r, $g, $b, $alpha) = $this->getBorderColor()->getRgba();
  34. $borderColorResource = imagecolorallocatealpha(
  35. $image->getCore(), $r, $g, $b,
  36. Editor::gdAlpha($alpha)
  37. );
  38. imagepolygon($image->getCore(), $points,
  39. $count,
  40. $borderColorResource
  41. );
  42. }
  43. return $image;
  44. }
  45. protected function points(){
  46. $points = array();
  47. foreach($this->points as $point){
  48. $points[] = $point[0];
  49. $points[] = $point[1];
  50. }
  51. if( count($points) < 6 ){
  52. throw new \Exception('Polygon needs at least 3 points.');
  53. }
  54. return $points;
  55. }
  56. }