Guest User

Untitled

a guest
May 21st, 2018
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.68 KB | None | 0 0
  1. <?php
  2.  
  3. /*
  4. * (c) 2017/08/20- yoya@awm.jp
  5. * $ composer require meyfa/php-svg
  6. */
  7. require_once("vendor/autoload.php");
  8.  
  9. use SVG\SVGImage;
  10. use SVG\Nodes\Structures\SVGStyle;
  11. use SVG\Nodes\Structures\SVGDefs;
  12. use SVG\Nodes\Gradients\SVGStop;
  13. use SVG\Nodes\Gradients\SVGRadialGradient;
  14. use SVG\Nodes\Shapes\SVGCircle;
  15.  
  16. list($width, $height) = [1000, 1000];
  17.  
  18. $image = new SVGImage($width, $height);
  19. $doc = $image->getDocument();
  20.  
  21. $style = new SVGStyle();
  22. $style->setCss(":root {background-color:black; }");
  23. $doc->addChild($style);
  24.  
  25. $defs = new SVGDefs();
  26. $doc->addChild($defs);
  27.  
  28. $gradTable = [
  29. "rgrad" => ["#F00", "#B00", "black"],
  30. "ggrad" => ["#0F0", "#0B0", "black"],
  31. "bgrad" => ["#22F", "#11B", "black"],
  32. ];
  33.  
  34. foreach ($gradTable as $id => $colors) {
  35. $grad = new SVGRadialGradient(0.5, 0.5, 0.55);
  36. $grad->setAttribute("id", $id);
  37. $stop0 = new SVGStop("50%", $colors[0]);
  38. $stop1 = new SVGStop("80%", $colors[1]);
  39. $stop2 = new SVGStop("100%",$colors[2]);
  40. $grad->addChild($stop0);
  41. $grad->addChild($stop1);
  42. $grad->addChild($stop2);
  43. $defs->addChild($grad);
  44. }
  45.  
  46. $unitX = 40;
  47. $unitY = sqrt($unitX*$unitX - ($unitX/2)*($unitX/2));
  48. $radius = 17;
  49.  
  50. for ($y = 0, $row = 0 ; $y < $height ; $y+= $unitY, $row++) {
  51. for ($x = ($row%2)?0:($unitX/2), $column = 0 ; $x < $width ; $x+= $unitX, $column++) {
  52. $circle = new SVGCircle($x, $y, $radius, $radius);
  53. $idx = (((int)($row*3/2) + $column)%3);
  54. // $color = ["#FF0000", "#00FF00", "#0000FF"][$idx];
  55. $color = ["url(#rgrad)","url(#ggrad)","url(#bgrad)"][$idx];
  56. $circle->setStyle('fill', $color);
  57. $doc->addChild($circle);
  58. }
  59. }
  60.  
  61. header('Content-Type: image/svg+xml');
  62. echo $image;
Add Comment
Please, Sign In to add comment