<?php
// pixellate.php
// RJM Programming
// December, 2017

$penup=false;
$reverseopacity = 0;
$zeroes = 4;
$width = 200;
$height = 200;
$rgbas=[""];
$rgbai=[-1];
$rgbac=0;
$two=2;

function huhpenup($inth) {
  global $penup;
  if (strpos($inth,"+") !== false) {
    $penup=true;
  } else if (strlen((" " . trim($inth))) == strlen($inth)) {
    $penup=true;
  }
  return str_replace(" ","",str_replace("+","",$inth));
}

function ourimagesetpixelline($pgd,$px,$py,$px2,$py2,$pred) { // rasterize
global $penup;
if (!$penup) {
$llen=pow((($px - $px2) * ($px - $px2) + ($py - $py2) * ($py - $py2)), 0.5);
$sofar=-1.0;
while ($sofar < $llen) {
if ($sofar < 0.0) {
  $sofar=0.0;
  $npx=$px;
  $npy=$py;
} else if (($sofar + 1.0) > $llen) {
  $npx=$px2;
  $npy=$py2;
} else {
  $npx=round($px + ($px2 - $px) * $sofar / $llen);
  $npy=round($py + ($py2 - $py) * $sofar / $llen);
}
if (isset($_GET['size'])) {
  for ($ipx=($npx - $_GET['size']); $ipx<=($npx + $_GET['size']); $ipx++) {
  for ($ipy=($npy - $_GET['size']); $ipy<=($npy + $_GET['size']); $ipy++) {
  imagesetpixel($pgd,$ipx,$ipy,$pred);
  }
  }
} else if (isset($_POST['size'])) {
  for ($ipx=($npx - $_POST['size']); $ipx<=($npx + $_POST['size']); $ipx++) {
  for ($ipy=($npy - $_POST['size']); $ipy<=($npy + $_POST['size']); $ipy++) {
  imagesetpixel($pgd,$ipx,$ipy,$pred);
  }
  }
} else {
  imagesetpixel($pgd,$npx,$npy,$pred);
}
$sofar += 1.0;
}
}
}

function ourimagesetpixel($pgd,$px,$py,$pred) {
if (isset($_GET['size'])) {
  for ($ipx=($px - $_GET['size']); $ipx<=($px + $_GET['size']); $ipx++) {
  for ($ipy=($py - $_GET['size']); $ipy<=($py + $_GET['size']); $ipy++) {
  imagesetpixel($pgd,$ipx,$ipy,$pred);
  }
  }
} else if (isset($_POST['size'])) {
  for ($ipx=($px - $_POST['size']); $ipx<=($px + $_POST['size']); $ipx++) {
  for ($ipy=($py - $_POST['size']); $ipy<=($py + $_POST['size']); $ipy++) {
  imagesetpixel($pgd,$ipx,$ipy,$pred);
  }
  }
} else {
  imagesetpixel($pgd,$px,$py,$pred);
}
}

function modeideas($ingd) {
ob_start(); 
imagepng($ingd);
$image_data = ob_get_contents(); 
ob_end_clean(); 
$image_data_base64 = base64_encode($image_data);
return $image_data_base64;
}

if (isset($_GET['width'])) {
  $width = $_GET['width'];
} else if (isset($_POST['width'])) {
  $width = $_POST['width'];
}
if (isset($_GET['height'])) {
  $height = $_GET['height'];
} else if (isset($_POST['height'])) {
  $height = $_POST['height'];
}
if (isset($_GET['zeroes'])) {
  $zeroes = $_GET['zeroes'];
} else if (isset($_POST['zeroes'])) {
  $zeroes = $_POST['zeroes'];
}
if (isset($_GET['opacity'])) {
  $op = urldecode($_GET['opacity']);
  $reverseopacity = floor(127.0 - ($op * 127.0));
} else if (isset($_POST['opacity'])) {
  $op = urldecode($_POST['opacity']);
  $reverseopacity = floor(127.0 - ($op * 127.0));
}

$x = $width;
$y = $height;
$howmany = 1;
for ($i = 0; $i < $zeroes; $i++) {
  $howmany *= 10;
}

$gd = imagecreatetruecolor($x, $y);

if (isset($_GET['positive']) || isset($_POST['positive'])) {
$background = imagecreatetruecolor(709,709); 
$wb = imagecolorallocatealpha($gd, 255, 255, 255, $reverseopacity); 
imagefill($gd,0,0,$wb);
//imagecopyresampled($gd, $new_img,(709-$imageWidth)/2,(709-$imageHeight)/2, 0, 0, $imageWidth, $imageHeight, $width, $height);
}
 
$corners[0] = array('x' => ($width / 2), 'y' =>  10);
$corners[1] = array('x' =>   0, 'y' => ($height - 10));
$corners[2] = array('x' => $width, 'y' => ($height - 10));

$red = imagecolorallocatealpha($gd, 255, 0, 0, $reverseopacity); 
$xyarray=[];
$factor=(pi() / 180.0);

if (isset($_GET['circle'])) {
  $cbits=explode(",",urldecode($_GET['circle']));
  $joff=0;
  $koff=0;
  for ($ioff=$joff; $ioff<sizeof($cbits); $ioff+=3) {
  $prefix='';
  if (sizeof($cbits) >= 3) {
    for ($a=0.0; $a<=360.0; $a+=1.0) {
      if (isset($_GET['arc'])) {
        if (strpos(urldecode($_GET['arc']), ",") !== false) {
        $prefix='';
        if ((0.0 + $a) < (0.0 + explode(",",urldecode($_GET['arc']))[0])) $prefix='+';
        if ((0.0 + $a) > (0.0 + explode(",",urldecode($_GET['arc']))[1])) $prefix='+';
        } else {
        if ((0.0 + $a) > (0.0 + urldecode($_GET['arc']))) $prefix='+';
        }
      }
      if (strpos($cbits[0 + $ioff + $koff],"rgb") !== false && strpos($cbits[0 + $ioff + $koff],"(") !== false) {
        if ($rgbai[$rgbac] < 0) {
          $rgbas[$rgbac] = $cbits[0 + $ioff + $koff];
          $rgbai[$rgbac] = sizeof($xyarray);
        } else {
          $rgbas[sizeof($rgbai)] = $cbits[0 + $ioff + $koff];
          $rgbai[sizeof($rgbai)] = sizeof($xyarray);
        } 
        $koff++;
      }
      $xyarray[sizeof($xyarray)] = $prefix . ($cbits[0 + $ioff + $koff] + sin($a * $factor) * $cbits[2 + $ioff + $koff]);
      $xyarray[sizeof($xyarray)] = $prefix . ($cbits[1 + $ioff + $koff] - cos($a * $factor) * $cbits[2 + $ioff + $koff]);
    }
if (isset($_GET['positive']) || isset($_POST['positive'])) {
$red = imagecolorallocatealpha($gd, 0, 0, 0, $reverseopacity); 
} else {
$red = imagecolorallocatealpha($gd, 255, 255, 255, $reverseopacity); 
}
for ($i = 0; $i < (sizeof($xyarray) - $two); $i+=2) {
  $penup=false;
  if ($rgbai[$rgbac] == $i) {
    $rbits=explode(":",explode(")",explode("(",$rgbas[$rgbac])[1])[0]);
    if (sizeof($rbits) >= 4) {
    $reverseopacity=$rbits[3];
$red = imagecolorallocatealpha($gd, $rbits[0], $rbits[1], $rbits[2], $reverseopacity); 
    } else {
$red = imagecolorallocatealpha($gd, $rbits[0], $rbits[1], $rbits[2], $reverseopacity); 
    }
    if (($rgbac + 1) > sizeof($rgbas)) {
      $rgbas[sizeof($rgbai)] = "";
      $rgbai[sizeof($rgbai)] = -1;
    }
    $rgbac++;
  }
  ourimagesetpixelline($gd, round(huhpenup($xyarray[$i])),round(huhpenup($xyarray[$i + 1])), round(huhpenup($xyarray[(($i + 2) % sizeof($xyarray))])),round(huhpenup($xyarray[(($i + 3) % sizeof($xyarray))])), $red);
  //$red = imagecolorallocatealpha($gd, rand(0, 255), rand(0, 255), rand(0, 255), $reverseopacity); 
}
$xyarray=[];
  }
  }
} else if (isset($_POST['circle'])) {
  $cbits=explode(",",urldecode($_POST['circle']));
  $joff=0;
  $koff=0;
  for ($ioff=$joff; $ioff<sizeof($cbits); $ioff+=3) {
  $prefix='';
  if (sizeof($cbits) >= 3) {
    for ($a=0.0; $a<=360.0; $a+=1.0) {
      if (isset($_POST['arc'])) {
        if (strpos(urldecode($_POST['arc']), ",") !== false) {
        $prefix='';
        if ((0.0 + $a) < (0.0 + explode(",",urldecode($_POST['arc']))[0])) $prefix='+';
        if ((0.0 + $a) > (0.0 + explode(",",urldecode($_POST['arc']))[1])) $prefix='+';
        } else {
        if ((0.0 + $a) > (0.0 + urldecode($_POST['arc']))) $prefix='+';
        }
      }
      if (strpos($cbits[0 + $ioff + $koff],"rgb") !== false && strpos($cbits[0 + $ioff + $koff],"(") !== false) {
        if ($rgbai[$rgbac] < 0) {
          $rgbas[$rgbac] = $cbits[0 + $ioff + $koff];
          $rgbai[$rgbac] = sizeof($xyarray);
          echo $cbits[0 + $ioff + $koff];
          exit;
        } else {
          $rgbas[sizeof($rgbai)] = $cbits[0 + $ioff + $koff];
          $rgbai[sizeof($rgbai)] = sizeof($xyarray);
        } 
        $koff++;
      }
      $xyarray[sizeof($xyarray)] = $prefix . ($cbits[0 + $ioff + $koff] + sin($a * $factor) * $cbits[2 + $ioff + $koff]);
      $xyarray[sizeof($xyarray)] = $prefix . ($cbits[1 + $ioff + $koff] - cos($a * $factor) * $cbits[2 + $ioff + $koff]);
    }
if (isset($_GET['positive']) || isset($_POST['positive'])) {
$red = imagecolorallocatealpha($gd, 0, 0, 0, $reverseopacity); 
} else {
$red = imagecolorallocatealpha($gd, 255, 255, 255, $reverseopacity); 
}
for ($i = 0; $i < (sizeof($xyarray) - $two); $i+=2) {
  $penup=false;
  if ($rgbai[$rgbac] == $i) {
    $rbits=explode(":",explode(")",explode("(",$rgbas[$rgbac])[1])[0]);
    if (sizeof($rbits) >= 4) {
    $reverseopacity=$rbits[3];
$red = imagecolorallocatealpha($gd, $rbits[0], $rbits[1], $rbits[2], $reverseopacity); 
    } else {
$red = imagecolorallocatealpha($gd, $rbits[0], $rbits[1], $rbits[2], $reverseopacity); 
    }
    if (($rgbac + 1) > sizeof($rgbas)) {
      $rgbas[sizeof($rgbai)] = "";
      $rgbai[sizeof($rgbai)] = -1;
    }
    $rgbac++;
  }
  ourimagesetpixelline($gd, round(huhpenup($xyarray[$i])),round(huhpenup($xyarray[$i + 1])), round(huhpenup($xyarray[(($i + 2) % sizeof($xyarray))])),round(huhpenup($xyarray[(($i + 3) % sizeof($xyarray))])), $red);
  //$red = imagecolorallocatealpha($gd, rand(0, 255), rand(0, 255), rand(0, 255), $reverseopacity); 
}
$xyarray=[];
  }
  }
}

$penup=false;
$rgbas=[""];
$rgbai=[-1];
$rgbac=0;
$koff=0;
$axyarray=[];

if (isset($_GET['tarea']) || isset($_GET['url'])) {
  $pregm='';
  $postgm='';
  $gm='';
  if (isset($_GET['url']) && isset($_GET['tarea'])) {
  if ($_GET['url'] != '') {
  $gm=strtolower(file_get_contents(urldecode($_GET['url'])));
  $postgm=' ';
  } else {
  $gm=strtolower(urldecode($_GET['tarea']));
  }
  } else if (isset($_GET['url']) && !isset($_GET['tarea'])) {
  $gm=strtolower(file_get_contents(urldecode($_GET['url'])));
  $postgm=' ';
  } else if (!isset($_GET['url']) && isset($_GET['tarea'])) {
  $gm=strtolower(urldecode($_GET['tarea']));
  } else if (isset($_GET['tarea'])) {
  $gm=strtolower(urldecode($_GET['tarea']));
  }
  if (strpos($gm, ' coords=') === false && strpos($gm, ',') !== false && $postgm == '') {
   $pregm='  coords="';
   $postgm=str_replace(';','"  coords="',$gm) . '"';
   $gm=$postgm;
   $postgm='';
  }
  $gms=explode("</map>",$gm);
  $igms=sizeof($gms);
  if ($igms > 1) $igms--;
  for ($ii=0; $ii<$igms; $ii++) {
  $acoords = explode(' coords="', str_replace(" coords='",' coords="',str_replace('RGB(','rgba(',str_replace('RGBA(','rgba(',str_replace('rgb(','rgba(',($pregm . $gms[$ii] . $postgm))))));
  $dstr="";
  $delim="";
  for ($i=1; $i<sizeof($acoords); $i++) {
  $inq=explode("'",explode('"',$acoords[$i])[0])[0];
  $inqs=explode(",",$inq);
  if (strpos($acoords[-1 + $i], 'rgba(') !== false && strpos($acoords[-1 + $i], ')') !== false) {
  $huhrs=explode('rgba(', $acoords[-1 + $i]);
  $dstr.='rgba(' . str_replace(',',':',$huhrs[-1 + sizeof($huhrs)]) . '),';
  }
  if (sizeof($inqs) == 4 && strpos(strtolower($acoords[-1 + $i]), ' shape="rect"') !== false) {
  $dstr.=($delim . $inqs[0] . ',' . $inqs[1] . ',' . $inqs[0] . ',' . $inqs[3] . ',' . $inqs[2] . ',' . $inqs[3] . ',' . $inqs[2] . ',' . $inqs[1] . ',' . $inqs[0] . ',' . $inqs[1]);
  } else {
  $dstr.=($delim . $inq);
  }
  $delim="+0,0,";
  }
  $axyarray=explode(",", $dstr);
if (isset($_GET['positive']) || isset($_POST['positive'])) {
$red = imagecolorallocatealpha($gd, 0, 0, 0, $reverseopacity); 
} else {
$red = imagecolorallocatealpha($gd, 255, 255, 255, $reverseopacity); 
}
for ($i = 0; $i < sizeof($axyarray); $i++) {
      if (strpos($axyarray[$i],"rgb") !== false && strpos($axyarray[$i],"(") !== false) {
        if ($rgbai[$rgbac] < 0) {
          $rgbas[$rgbac] = $axyarray[$i];
          $rgbai[$rgbac] = sizeof($xyarray) - (sizeof($xyarray) % 2);
        } else {
          $rgbas[sizeof($rgbai)] = $axyarray[$i];
          $rgbai[sizeof($rgbai)] = sizeof($xyarray) - (sizeof($xyarray) % 2);
        } 
      } else {
        $xyarray[sizeof($xyarray)] = $axyarray[$i];
      }
}
for ($i = 0; $i < (sizeof($xyarray) - $two); $i+=2) {
  $penup=false;
  if ($rgbai[$rgbac] == $i) {
    $rbits=explode(":",explode(")",explode("(",$rgbas[$rgbac])[1])[0]);
    if (sizeof($rbits) >= 4) {
    $reverseopacity=$rbits[3];
$red = imagecolorallocatealpha($gd, $rbits[0], $rbits[1], $rbits[2], $reverseopacity); 
    } else {
$red = imagecolorallocatealpha($gd, $rbits[0], $rbits[1], $rbits[2], $reverseopacity); 
    }
    if (($rgbac + 1) > sizeof($rgbas)) {
      $rgbas[sizeof($rgbai)] = "";
      $rgbai[sizeof($rgbai)] = -1;
    }
    $rgbac++;
  }
  ourimagesetpixelline($gd, round(huhpenup($xyarray[$i])),round(huhpenup($xyarray[$i + 1])), round(huhpenup($xyarray[(($i + 2) % sizeof($xyarray))])),round(huhpenup($xyarray[(($i + 3) % sizeof($xyarray))])), $red);
  //$red = imagecolorallocatealpha($gd, rand(0, 255), rand(0, 255), rand(0, 255), $reverseopacity); 
}
}
} else if (isset($_POST['tarea']) || isset($_POST['url'])) {
  $pregm='';
  $postgm='';
  $gm='';
  if (isset($_POST['url']) && isset($_POST['tarea'])) {
  if ($_POST['url'] != '') {
  $gm=strtolower(file_get_contents(urldecode($_POST['url'])));
  $postgm=' ';
  } else {
  $gm=strtolower(urldecode($_POST['tarea']));
  }
  } else if (isset($_POST['url']) && !isset($_POST['tarea'])) {
  $gm=strtolower(file_get_contents(urldecode($_POST['url'])));
  $postgm=' ';
  } else if (!isset($_POST['url']) && isset($_POST['tarea'])) {
  $gm=strtolower(urldecode($_POST['tarea']));
  } else if (isset($_POST['tarea'])) {
  $gm=strtolower(urldecode($_POST['tarea']));
  }
  if (strpos($gm, ' coords=') === false && strpos($gm, ',') !== false && $postgm == '') {
   $pregm='  coords="';
   $postgm=str_replace(';','"  coords="',$gm) . '"';
   $gm=$postgm;
   $postgm='';
  }
  $gms=explode("</map>",$gm);
  $igms=sizeof($gms);
  if ($igms > 1) $igms--;
  for ($ii=0; $ii<$igms; $ii++) {
  $acoords = explode(' coords="', str_replace(" coords='",' coords="',str_replace('RGB(','rgba(',str_replace('RGBA(','rgba(',str_replace('rgb(','rgba(',($pregm . $gms[$ii] . $postgm))))));
  $dstr="";
  $delim="";
  for ($i=1; $i<sizeof($acoords); $i++) {
  $inq=explode("'",explode('"',$acoords[$i])[0])[0];
  $inqs=explode(",",$inq);
  if (strpos($acoords[-1 + $i], 'rgba(') !== false && strpos($acoords[-1 + $i], ')') !== false) {
  $huhrs=explode('rgba(', $acoords[-1 + $i]);
  $dstr.='rgba(' . str_replace(',',':',$huhrs[-1 + sizeof($huhrs)]) . '),';
  }
  if (sizeof($inqs) == 4 && strpos(strtolower($acoords[-1 + $i]), ' shape="rect"') !== false) {
  $dstr.=($delim . $inqs[0] . ',' . $inqs[1] . ',' . $inqs[0] . ',' . $inqs[3] . ',' . $inqs[2] . ',' . $inqs[3] . ',' . $inqs[2] . ',' . $inqs[1] . ',' . $inqs[0] . ',' . $inqs[1]);
  } else {
  $dstr.=($delim . $inq);
  }
  $delim="+0,0,";
  }
  $axyarray=explode(",", $dstr);
if (isset($_GET['positive']) || isset($_POST['positive'])) {
$red = imagecolorallocatealpha($gd, 0, 0, 0, $reverseopacity); 
} else {
$red = imagecolorallocatealpha($gd, 255, 255, 255, $reverseopacity); 
}
for ($i = 0; $i < sizeof($axyarray); $i++) {
      if (strpos($axyarray[$i],"rgb") !== false && strpos($axyarray[$i],"(") !== false) {
        if ($rgbai[$rgbac] < 0) {
          $rgbas[$rgbac] = $axyarray[$i];
          $rgbai[$rgbac] = sizeof($xyarray) - (sizeof($xyarray) % 2);
        } else {
          $rgbas[sizeof($rgbai)] = $axyarray[$i];
          $rgbai[sizeof($rgbai)] = sizeof($xyarray) - (sizeof($xyarray) % 2);
        } 
      } else {
        $xyarray[sizeof($xyarray)] = $axyarray[$i];
      }
}
for ($i = 0; $i < (sizeof($xyarray) - $two); $i+=2) {
  $penup=false;
  if ($rgbai[$rgbac] == $i) {
    $rbits=explode(":",explode(")",explode("(",$rgbas[$rgbac])[1])[0]);
    if (sizeof($rbits) >= 4) {
    $reverseopacity=$rbits[3];
$red = imagecolorallocatealpha($gd, $rbits[0], $rbits[1], $rbits[2], $reverseopacity); 
    } else {
$red = imagecolorallocatealpha($gd, $rbits[0], $rbits[1], $rbits[2], $reverseopacity); 
    }
    if (($rgbac + 1) > sizeof($rgbas)) {
      $rgbas[sizeof($rgbai)] = "";
      $rgbai[sizeof($rgbai)] = -1;
    }
    $rgbac++;
  }
  ourimagesetpixelline($gd, round(huhpenup($xyarray[$i])),round(huhpenup($xyarray[$i + 1])), round(huhpenup($xyarray[(($i + 2) % sizeof($xyarray))])),round(huhpenup($xyarray[(($i + 3) % sizeof($xyarray))])), $red);
  //$red = imagecolorallocatealpha($gd, rand(0, 255), rand(0, 255), rand(0, 255), $reverseopacity); 
}
}
}

$penup=false;
$rgbas=[""];
$rgbai=[-1];
$rgbac=0;
$koff=0;
$axyarray=[];

if (isset($_GET['coords']) || isset($_GET['closecoords']) || sizeof($xyarray) > 0) {
$two=2;
if (isset($_GET['closecoords'])) {
 $two=0;
 if (sizeof($xyarray) == 0) {
 if (isset($_GET['coords'])) {
 $xyarray=[];
 $axyarray=explode(",",str_replace(";",",+0,0,",urldecode($_GET['coords'])));
 } else {
 $xyarray=[];
 $axyarray=explode(",",str_replace(";",",+0,0,",urldecode($_GET['closecoords'])));
 }
 }
} else if (sizeof($xyarray) == 0) {
 $xyarray=[];
 $axyarray=explode(",",str_replace(";",",+0,0,",urldecode($_GET['coords'])));
}
if (isset($_GET['positive']) || isset($_POST['positive'])) {
$red = imagecolorallocatealpha($gd, 0, 0, 0, $reverseopacity); 
} else {
$red = imagecolorallocatealpha($gd, 255, 255, 255, $reverseopacity); 
}
for ($i = 0; $i < sizeof($axyarray); $i++) {
      if (strpos($axyarray[$i],"rgb") !== false && strpos($axyarray[$i],"(") !== false) {
        if ($rgbai[$rgbac] < 0) {
          $rgbas[$rgbac] = $axyarray[$i];
          $rgbai[$rgbac] = sizeof($xyarray) - (sizeof($xyarray) % 2);
        } else {
          $rgbas[sizeof($rgbai)] = $axyarray[$i];
          $rgbai[sizeof($rgbai)] = sizeof($xyarray) - (sizeof($xyarray) % 2);
        } 
      } else {
        $xyarray[sizeof($xyarray)] = $axyarray[$i];
      }
}
for ($i = 0; $i < (sizeof($xyarray) - $two); $i+=2) {
  $penup=false;
  if ($rgbai[$rgbac] == $i) {
    $rbits=explode(":",explode(")",explode("(",$rgbas[$rgbac])[1])[0]);
    if (sizeof($rbits) >= 4) {
    $reverseopacity=$rbits[3];
$red = imagecolorallocatealpha($gd, $rbits[0], $rbits[1], $rbits[2], $reverseopacity); 
    } else {
$red = imagecolorallocatealpha($gd, $rbits[0], $rbits[1], $rbits[2], $reverseopacity); 
    }
    if (($rgbac + 1) > sizeof($rgbas)) {
      $rgbas[sizeof($rgbai)] = "";
      $rgbai[sizeof($rgbai)] = -1;
    }
    $rgbac++;
  }
  ourimagesetpixelline($gd, round(huhpenup($xyarray[$i])),round(huhpenup($xyarray[$i + 1])), round(huhpenup($xyarray[(($i + 2) % sizeof($xyarray))])),round(huhpenup($xyarray[(($i + 3) % sizeof($xyarray))])), $red);
  //$red = imagecolorallocatealpha($gd, rand(0, 255), rand(0, 255), rand(0, 255), $reverseopacity); 
}
} else if (isset($_POST['coords']) || isset($_POST['closecoords'])) {
$two=2;
if (isset($_POST['closecoords'])) {
 $two=0;
 if (sizeof($xyarray) == 0) {
 if (isset($_POST['coords'])) {
 $xyarray=explode(",",str_replace(";",",+0,0,",urldecode($_POST['coords'])));
 } else {
 $xyarray=explode(",",str_replace(";",",+0,0,",urldecode($_POST['closecoords'])));
 }
 }
} else if (sizeof($xyarray) == 0) {
 $xyarray=explode(",",str_replace(";",",+0,0,",urldecode($_POST['coords'])));
}
if (isset($_GET['positive']) || isset($_POST['positive'])) {
$red = imagecolorallocatealpha($gd, 0, 0, 0, $reverseopacity); 
} else {
$red = imagecolorallocatealpha($gd, 255, 255, 255, $reverseopacity); 
}
for ($i = 0; $i < (sizeof($xyarray) - $two); $i+=2) {
  $penup=false;
  ourimagesetpixelline($gd, round(huhpenup($xyarray[$i])),round(huhpenup($xyarray[$i + 1])), round(huhpenup($xyarray[(($i + 2) % sizeof($xyarray))])),round(huhpenup($xyarray[(($i + 3) % sizeof($xyarray))])), $red);
  //$red = imagecolorallocatealpha($gd, rand(0, 255), rand(0, 255), rand(0, 255), $reverseopacity); 
}
} else if (!isset($_GET['circle']) && !isset($_POST['circle'])) {
for ($i = 0; $i < $howmany; $i++) {
  ourimagesetpixel($gd, round($x),round($y), $red);
  if (1 == 1) {
  $x = rand(0, $width);
  $y = rand(0, $height);
  $red = imagecolorallocatealpha($gd, rand(0, 255), rand(0, 255), rand(0, 255), $reverseopacity); 
  } else {
  $a = rand(0, 2);
  $x = ($x + $corners[$a]['x']) / 2;
  $y = ($y + $corners[$a]['y']) / 2;
  }
}
}
 
if (!isset($_GET['mode']) && !isset($_POST['mode'])) {
header('Content-Type: image/png');
imagepng($gd);
} else { // thanks to https://stackoverflow.com/questions/8551754/convert-gd-output-to-base64 for ideas
if (isset($_GET['mode'])) {
if (strpos(strtolower(urldecode($_GET['mode'])),'image') !== false && strlen(urldecode($_GET['mode'])) == strlen('image')) {
$idb = modeideas($gd);
echo '<!doctype html><html><body><img style="width:' . $width . 'px;height:' . $height . 'px;" src="data:image/png;base64,' . $idb . '"></img></body></html>';
} else if (strpos(strtolower(urldecode($_GET['mode'])),'image') !== false && strlen(urldecode($_GET['mode'])) != strlen('image')) {
$idb = modeideas($gd);
echo '<!doctype html><html><head><scr' . 'ipt type="text/javascript"> parent.document.getElementById("' . substr(urldecode($_GET['mode']),strlen('image')) . '").style.width="' . $width . 'px";   parent.document.getElementById("' . substr(urldecode($_GET['mode']),strlen('image')) . '").style.height="' . $height . 'px";   parent.document.getElementById("' . substr(urldecode($_GET['mode']),strlen('image')) . '").src="data:image/png;base64,' . $idb . '";  </scr' . "ipt></head><body></body></html>";
} else if (strpos(strtolower(urldecode($_GET['mode'])),'img') !== false && strlen(urldecode($_GET['mode'])) == strlen('img')) {
$idb = modeideas($gd);
echo '<!doctype html><html><body><img style="width:' . $width . 'px;height:' . $height . 'px;" src="data:image/png;base64,' . $idb . '"></img></body></html>';
} else if (strpos(strtolower(urldecode($_GET['mode'])),'img') !== false && strlen(urldecode($_GET['mode'])) != strlen('img')) {
$idb = modeideas($gd);
echo '<!doctype html><html><head><scr' . 'ipt type="text/javascript"> parent.document.getElementById("' . substr(urldecode($_GET['mode']),strlen('img')) . '").style.width="' . $width . 'px";   parent.document.getElementById("' . substr(urldecode($_GET['mode']),strlen('img')) . '").style.height="' . $height . 'px";   parent.document.getElementById("' . substr(urldecode($_GET['mode']),strlen('img')) . '").src="data:image/png;base64,' . $idb . '";  </scr' . "ipt></head><body></body></html>";
} else if (strpos(strtolower(urldecode($_GET['mode'])),'div') !== false && strlen(urldecode($_GET['mode'])) == strlen('div')) {
$idb = modeideas($gd);
echo '<!doctype html><html><body><div style="width:' . $width . 'px;height:' . $height . 'px;"><img style="width:' . $width . 'px;height:' . $height . 'px;" src="data:image/png;base64,' . $idb . '"></img></div></body></html>';
} else if (strpos(strtolower(urldecode($_GET['mode'])),'div') !== false && strlen(urldecode($_GET['mode'])) != strlen('div')) {
$idb = modeideas($gd);
echo '<!doctype html><html><head><scr' . 'ipt type="text/javascript"> parent.document.getElementById("' . substr(urldecode($_GET['mode']),strlen('div')) . '").style.width="' . $width . 'px";   parent.document.getElementById("' . substr(urldecode($_GET['mode']),strlen('div')) . '").style.height="' . $height . 'px";  parent.document.getElementById("' . substr(urldecode($_GET['mode']),strlen('div')) . '").innerHTML="<img style=width:' . $width . "px;height:" . $height . "px; src='data:image/png;base64," . $idb . "'></img>" . '";  </scr' . "ipt></head><body></body></html>";
} else if (strpos(strtolower(urldecode($_GET['mode'])),'canvas') !== false && strlen(urldecode($_GET['mode'])) == strlen('canvas')) {
$idb = modeideas($gd);
$jsis=" function onl() { var canvas = document.getElementById('c'); \n"; // thanks to https://stackoverflow.com/questions/17090571/is-there-a-way-to-set-background-image-as-a-base64-encoded-image for ideas
$jsis.=" canvas.width = '" . $width . "';  \n"; 
$jsis.=" canvas.height = '" . $height . "';  \n"; 
$jsis.=" var ctx = canvas.getContext('2d'); \n\n"; 
$jsis.=" var image = new Image(); \n"; 
$jsis.=" image.width = '" . $width . "';  \n"; 
$jsis.=" image.height = '" . $height . "';  \n"; 
$jsis.=" image.onload = function() {  \n"; 
$jsis.="     ctx.drawImage(image, 0, 0, " . $width . ", " . $height . ", 0, 0, " . $width . ", " . $height . ");  \n"; 
$jsis.=" }; \n"; 
$jsis.=" image.src = 'data:image/png;base64," . $idb . "'; } \n"; 
echo '<!doctype html><html><head><scr' . 'ipt type="text/javascript"> ' . $jsis . '  </scr' . 'ipt></head><body onload=onl();><canvas id=c width="' . $width . 'px" height="' . $height . 'px"</canvas></body></html>';
} else if (strpos(strtolower(urldecode($_GET['mode'])),'canvas') !== false && strlen(urldecode($_GET['mode'])) != strlen('canvas')) {
$idb = modeideas($gd);
$jsis=" function onl() { var canvas = parent.document.getElementById('" . substr(urldecode($_GET['mode']),strlen('canvas')) . "'); \n"; // thanks to https://stackoverflow.com/questions/17090571/is-there-a-way-to-set-background-image-as-a-base64-encoded-image for ideas
$jsis.=" canvas.width = '" . $width . "';  \n"; 
$jsis.=" canvas.height = '" . $height . "';  \n"; 
$jsis.=" var ctx = canvas.getContext('2d'); \n\n"; 
$jsis.=" var image = new Image(); \n"; 
$jsis.=" image.width = '" . $width . "';  \n"; 
$jsis.=" image.height = '" . $height . "';  \n"; 
$jsis.=" image.onload = function() {  \n"; 
$jsis.="     ctx.drawImage(image, 0, 0, " . $width . ", " . $height . ", 0, 0, " . $width . ", " . $height . ");  \n"; 
$jsis.=" }; \n"; 
$jsis.=" image.src = 'data:image/png;base64," . $idb . "'; } \n"; 
echo '<!doctype html><html><head><scr' . 'ipt type="text/javascript"> ' . $jsis . '  </scr' . 'ipt></head><body onload=onl();></body></html>';
} else if (strpos(strtolower(urldecode($_GET['mode'])),'background') !== false && strlen(urldecode($_GET['mode'])) == strlen('background')) {
$idb = modeideas($gd);
echo '<!doctype html><html><body style=' . "'" . 'background:URL("data:image/png;base64,' . $idb . '");' . "'></body></html>";
} else if (strpos(strtolower(urldecode($_GET['mode'])),'background') !== false && strlen(urldecode($_GET['mode'])) != strlen('background')) {
$idb = modeideas($gd);
echo '<!doctype html><html><head><scr' . "ipt type='text/javascript'> if (parent.document.getElementById('" . substr(urldecode($_GET['mode']),strlen('background')) . "')) { parent.document.getElementById('" . substr(urldecode($_GET['mode']),strlen('background')) . "').style.width='" . $width . "px';   parent.document.getElementById('" . substr(urldecode($_GET['mode']),strlen('background')) . "').style.height='" . $height . "px';    parent.document.getElementById('" . substr(urldecode($_GET['mode']),strlen('background')) . "').style.backgroundImage=" . "'" . 'URL("data:image/png;base64,' . $idb . '")' . "';   } else {  parent.document.body.style.width='" . $width . "px';   parent.document.body.style.height='" . $height . "px';    parent.body.style.backgroundImage=" . "'" . 'URL("data:image/png;base64,' . $idb . '")' . "'; parent.document.body.style.width='" . $width . "px';   parent.document.body.style.height='" . $height . "px';    parent.body.style.backgroundImage=" . "'" . 'URL("data:image/png;base64,' . $idb . '")' . "'; }   </scr" . "ipt></head><body></body></html>";
} else { 
header('Content-Type: image/png');
imagepng($gd);
}


} else {
if (strpos(strtolower(urldecode($_POST['mode'])),'image') !== false && strlen(urldecode($_POST['mode'])) == strlen('image')) {
$idb = modeideas($gd);
echo '<!doctype html><html><body><img style="width:' . $width . 'px;height:' . $height . 'px;" src="data:image/png;base64,' . $idb . '"></img></body></html>';
} else if (strpos(strtolower(urldecode($_POST['mode'])),'image') !== false && strlen(urldecode($_POST['mode'])) != strlen('image')) {
$idb = modeideas($gd);
echo '<!doctype html><html><head><scr' . 'ipt type="text/javascript"> parent.document.getElementById("' . substr(urldecode($_POST['mode']),strlen('image')) . '").style.width="' . $width . 'px";   parent.document.getElementById("' . substr(urldecode($_POST['mode']),strlen('image')) . '").style.height="' . $height . 'px";  parent.document.getElementById("' . substr(urldecode($_POST['mode']),strlen('image')) . '").src="data:image/png;base64,' . $idb . '";  </scr' . "ipt></head><body></body></html>";
} else if (strpos(strtolower(urldecode($_POST['mode'])),'img') !== false && strlen(urldecode($_POST['mode'])) == strlen('img')) {
$idb = modeideas($gd);
echo '<!doctype html><html><body><img style="width:' . $width . 'px;height:' . $height . 'px;" src="data:image/png;base64,' . $idb . '"></img></body></html>';
} else if (strpos(strtolower(urldecode($_POST['mode'])),'img') !== false && strlen(urldecode($_POST['mode'])) != strlen('img')) {
$idb = modeideas($gd);
echo '<!doctype html><html><head><scr' . 'ipt type="text/javascript"> parent.document.getElementById("' . substr(urldecode($_POST['mode']),strlen('img')) . '").style.width="' . $width . 'px";   parent.document.getElementById("' . substr(urldecode($_POST['mode']),strlen('img')) . '").style.height="' . $height . 'px";  parent.document.getElementById("' . substr(urldecode($_POST['mode']),strlen('img')) . '").src="data:image/png;base64,' . $idb . '";  </scr' . "ipt></head><body></body></html>";
} else if (strpos(strtolower(urldecode($_POST['mode'])),'div') !== false && strlen(urldecode($_POST['mode'])) == strlen('div')) {
$idb = modeideas($gd);
echo '<!doctype html><html><body><div style="width:' . $width . 'px;height:' . $height . 'px;"><img style="width:' . $width . 'px;height:' . $height . 'px;" src="data:image/png;base64,' . $idb . '"></img></div></body></html>';
} else if (strpos(strtolower(urldecode($_POST['mode'])),'div') !== false && strlen(urldecode($_POST['mode'])) != strlen('div')) {
$idb = modeideas($gd);
echo '<!doctype html><html><head><scr' . 'ipt type="text/javascript"> parent.document.getElementById("' . substr(urldecode($_POST['mode']),strlen('div')) . '").style.width="' . $width . 'px";   parent.document.getElementById("' . substr(urldecode($_POST['mode']),strlen('div')) . '").style.height="' . $height . 'px";  parent.document.getElementById("' . substr(urldecode($_POST['mode']),strlen('div')) . '").innerHTML="<img style=width:' . $width . "px;height:" . $height . "px; src='data:image/png;base64," . $idb . "'></img>" . '";  </scr' . "ipt></head><body></body></html>";
} else if (strpos(strtolower(urldecode($_POST['mode'])),'canvas') !== false && strlen(urldecode($_POST['mode'])) == strlen('canvas')) {
$idb = modeideas($gd);
$jsis=" function onl() { var canvas = document.getElementById('c'); \n"; // thanks to https://stackoverflow.com/questions/17090571/is-there-a-way-to-set-background-image-as-a-base64-encoded-image for ideas
$jsis.=" canvas.width = '" . $width . "';  \n"; 
$jsis.=" canvas.height = '" . $height . "';  \n"; 
$jsis.=" var ctx = canvas.getContext('2d'); \n\n"; 
$jsis.=" var image = new Image(); \n"; 
$jsis.=" image.width = '" . $width . "';  \n"; 
$jsis.=" image.height = '" . $height . "';  \n"; 
$jsis.=" image.onload = function() {  \n"; 
$jsis.="     ctx.drawImage(image, 0, 0, " . $width . ", " . $height . ", 0, 0, " . $width . ", " . $height . ");  \n"; 
$jsis.=" }; \n"; 
$jsis.=" image.src = 'data:image/png;base64," . $idb . "'; } \n"; 
echo '<!doctype html><html><head><scr' . 'ipt type="text/javascript"> ' . $jsis . '  </scr' . 'ipt></head><body onload=onl();><canvas id=c width="' . $width . 'px" height="' . $height . 'px"</canvas></body></html>';
} else if (strpos(strtolower(urldecode($_POST['mode'])),'canvas') !== false && strlen(urldecode($_POST['mode'])) != strlen('canvas')) {
$idb = modeideas($gd);
$jsis=" function onl() { var canvas = parent.document.getElementById('" . substr(urldecode($_POST['mode']),strlen('canvas')) . "'); \n"; // thanks to https://stackoverflow.com/questions/17090571/is-there-a-way-to-set-background-image-as-a-base64-encoded-image for ideas
$jsis.=" canvas.width = '" . $width . "';  \n"; 
$jsis.=" canvas.height = '" . $height . "';  \n"; 
$jsis.=" var ctx = canvas.getContext('2d'); \n\n"; 
$jsis.=" var image = new Image(); \n"; 
$jsis.=" image.width = '" . $width . "';  \n"; 
$jsis.=" image.height = '" . $height . "';  \n"; 
$jsis.=" image.onload = function() {  \n"; 
$jsis.="     ctx.drawImage(image, 0, 0, " . $width . ", " . $height . ", 0, 0, " . $width . ", " . $height . ");  \n"; 
$jsis.=" }; \n"; 
$jsis.=" image.src = 'data:image/png;base64," . $idb . "'; } \n"; 
echo '<!doctype html><html><head><scr' . 'ipt type="text/javascript"> ' . $jsis . '  </scr' . 'ipt></head><body onload=onl();></body></html>';
} else if (strpos(strtolower(urldecode($_POST['mode'])),'background') !== false && strlen(urldecode($_POST['mode'])) == strlen('background')) {
$idb = modeideas($gd);
echo '<!doctype html><html><body style=' . "'" . 'background:URL("data:image/png;base64,' . $idb . '");' . "'></body></html>";
} else if (strpos(strtolower(urldecode($_POST['mode'])),'background') !== false && strlen(urldecode($_POST['mode'])) != strlen('background')) {
$idb = modeideas($gd);
echo '<!doctype html><html><head><scr' . "ipt type='text/javascript'> if (parent.document.getElementById('" . substr(urldecode($_POST['mode']),strlen('background')) . "')) { parent.document.getElementById('" . substr(urldecode($_POST['mode']),strlen('background')) . "').style.width='" . $width . "px';   parent.document.getElementById('" . substr(urldecode($_POST['mode']),strlen('background')) . "').style.height='" . $height . "px';    parent.document.getElementById('" . substr(urldecode($_POST['mode']),strlen('background')) . "').style.backgroundImage=" . "'" . 'URL("data:image/png;base64,' . $idb . '")' . "';   } else {  parent.document.body.style.width='" . $width . "px';   parent.document.body.style.height='" . $height . "px';    parent.body.style.backgroundImage=" . "'" . 'URL("data:image/png;base64,' . $idb . '")' . "'; parent.document.body.style.width='" . $width . "px';   parent.document.body.style.height='" . $height . "px';    parent.body.style.backgroundImage=" . "'" . 'URL("data:image/png;base64,' . $idb . '")' . "'; }   </scr" . "ipt></head><body></body></html>";
} else { 
header('Content-Type: image/png');
imagepng($gd);
}
}
}

?>
