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

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;
}

$reverseopacity = 0;
$zeroes = 4;
$width = 200;
$height = 200;
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);
 
$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); 

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);
}
}
}

?>
