<?php

  header ("Content-type: image/svg+xml");

  // ---------------------------------------------------------------------
  //
  // Mark Brautigam
  // 12 August 2015
  //
  // use like this:
  // <img alt='foobar' src='randomsvg.php?width=600&height=400&npoly=50&color=a' />
  // 
  // width & height = the size of the resulting SVG image
  // npoly = the number of polygons to use
  // color = the color scheme:
  //   r = red, o = orange, y = yellow, g = green,
  //   c = cyan, b = blue, v = violet, m = magenta,
  //   x = grays (does not work well),
  //   a = all colors
  //
  // ---------------------------------------------------------------------
  
  // ---------------------------------------------------------------------
  //  MAIN CODE CALL
  // ---------------------------------------------------------------------
  draw_svg(); 

  // ---------------------------------------------------------------------
  function draw_svg() 
  // ---------------------------------------------------------------------
  {
    $npoly = isset ($_GET['npoly']) ? $_GET['npoly'] : 100;
    $width = isset ($_GET['width']) ? $_GET['width'] : 600;
    $height = isset ($_GET['height']) ? $_GET['height'] : 600;
    $color = isset ($_GET['color']) ? $_GET['color'] : 'a';

    draw_svg_header ($width, $height);
    draw_first_rectangle ($width, $height, $color);
    for ($i=0; $i<$npoly; $i++) {
      draw_poly ($width, $height, $color);
    }
    echo "</svg>\n";
  }

  // ---------------------------------------------------------------------
  function draw_svg_header ($w, $h) 
  // ---------------------------------------------------------------------
  {
    printf ("<svg width='%d' height='%d' viewbox='0 0 %d %d' xmlns='http://www.w3.org/2000/svg' xmlns:xlink='http://www.w3.org/1999/xlink'>\n", $w, $h, $w, $h );
  }

  // ---------------------------------------------------------------------
  function draw_poly ($w, $h, $c)
  // ---------------------------------------------------------------------
  {
    $rand = rand (0, 5);
    if ($rand < 3)
      draw_circle ($w, $h, $c);
    else if ($rand < 5)
      draw_triangle ($w, $h, $c);
    else 
      draw_rectangle ($w, $h, $c);
  }

  // ---------------------------------------------------------------------
  function draw_circle ($w, $h, $c) 
  // ---------------------------------------------------------------------
  {
    $rx = rand (-$w, $w);
    $ry = rand (-$h, $h);
    $rr = rand (0, max($w, $h));
    $rc = get_random_color ($c);
    $rf = rand (0, 1000) / 2000;
    printf ("<circle cx='%d' cy='%d' r='%d' fill='%s' fill-opacity='%f' />\n", 
      $rx, $ry, $rr, $rc, $rf);
  }

  // ---------------------------------------------------------------------
  function draw_triangle ($w, $h, $c) 
  // ---------------------------------------------------------------------
  {
    $rx1 = rand (-$w, $w);
    $rx2 = rand (-$w, $w);
    $rx3 = rand (-$w, $w);
    $ry1 = rand (-$h, $h);
    $ry2 = rand (-$h, $h);
    $ry3 = rand (-$h, $h);
    $rc = get_random_color ($c);
    $rf = rand (0, 1000) / 2000;
    printf ("<polygon points='%d %d %d %d %d %d' fill='%s' fill-opacity='%f' />\n",
      $rx1, $ry1, $rx2, $ry2, $rx3, $ry3, $rc, $rf);
  }

  // ---------------------------------------------------------------------
  function draw_rectangle ($w, $h, $c) 
  // ---------------------------------------------------------------------
  {
    $rx1 = rand (-$w, $w);
    $rx2 = rand (-$w, $w);
    $ry1 = rand (-$h, $h);
    $ry2 = rand (-$h, $h);
    $rc = get_random_color ($c);
    $rf = rand (0, 1000) / 2000;
    printf ("<rect x='%d' y='%d' width='%d' height='%d' fill='%s' fill-opacity='%f' />\n",
      $rx1, $ry1, abs($rx2-$rx1), abs($ry2-$ry1), $rc, $rf);
  }

  // ---------------------------------------------------------------------
  function draw_first_rectangle ($w, $h, $c)
  // ---------------------------------------------------------------------
  {
    $rc = get_random_color ($c);
    printf ("<rect x='0' y='0' width='%d' height='%d' fill='%s' />\n", $w, $h, $rc);
  }

  // ---------------------------------------------------------------------
  function get_random_color ($c)
  // ---------------------------------------------------------------------
  {
    $rr = rand (0, 127);
    $rg = rand (0, 127);
    $rb = rand (0, 127);
    switch ($c) {
      case "r" : $rr += 128; break;
      case "o" : $rr += 128; $rg += 64; break;
      case "y" : $rr += 128; $rg += 128; break;
      case "g" : $rg += 128; break;
      case "c" : $rg += 128; $rb += 128; break;
      case "b" : $rb += 128; break;
      case "v" : $rr += 64; $rb += 128; break;
      case "m" : $rr += 128; $rb += 128; break;
      case "x" : $rr += 64; $rg += 64; $rb += 64; break;
      case "a" :
        switch (rand (0, 5)) {
          case 0 : $rr += 128; break;
          case 1 : $rr += 128; $rg += 128; break;
          case 2 : $rg += 128; break;
          case 3 : $rg += 128; $rb += 128; break;
          case 4 : $rb += 128; break;
          case 5 : $rr += 128; $rb += 128; break;
        }
        break;
    }
    return sprintf ("#%0X%0X%0X", $rr, $rg, $rb);
  }

?>