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

$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'];
}
$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 = imagecolorallocate($gd, 255, 0, 0); 

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 = imagecolorallocate($gd, rand(0, 255), rand(0, 255), rand(0, 255)); 
  } else {
  $a = rand(0, 2);
  $x = ($x + $corners[$a]['x']) / 2;
  $y = ($y + $corners[$a]['y']) / 2;
  }
}
 
header('Content-Type: image/png');
imagepng($gd);

?>
