Skip to content

Instantly share code, notes, and snippets.

@RickyRomero
Created July 28, 2012 18:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RickyRomero/3194304 to your computer and use it in GitHub Desktop.
Save RickyRomero/3194304 to your computer and use it in GitHub Desktop.
Creates and verifies full 16,777,216-color PNGs.
<?php
ini_set("memory_limit", "256M");
if (isset($argv[1]))
{
echo "Verifying image...\n";
verify16777216($argv[1]);
}
else
{
echo "No path set. Creating image...\n";
create16777216();
}
function pixelToRGB($x, $y, $w)
{
$pixelNum = ($y * $w) + $x;
$r = (int)($pixelNum % 256);
$g = (int)(floor($pixelNum / 256) % 256);
$b = (int)(floor($pixelNum / (256 * 256)));
return array($r, $g, $b);
}
function create16777216()
{
$size = 4096;
$img = imagecreatetruecolor($size, $size);
for ($i = 0; $i < $size * $size; $i++)
{
$x = $i % $size;
$y = floor($i / $size);
$rgb = pixelToRGB($x, $y, $size);
$color = imagecolorallocate($img, $rgb[0], $rgb[1], $rgb[2]);
imagesetpixel($img, $x, $y, $color);
}
echo "Saving 16777216.png to current directory...\n";
imagepng($img, "16777216.png", 0);
}
function verify16777216($file)
{
$size = 4096;
$img = imagecreatefrompng($file);
for ($i = 0; $i < $size * $size; $i++)
{
$x = $i % $size;
$y = floor($i / $size);
$theirRGB = imagecolorat($img, $x, $y);
$theirR = ($theirRGB >> 16) & 0xFF;
$theirG = ($theirRGB >> 8) & 0xFF;
$theirB = $theirRGB & 0xFF;
$ourRGB = pixelToRGB($x, $y, $size);
if ($theirR !== $ourRGB[0] || $theirG !== $ourRGB[1] || $theirB !== $ourRGB[2])
{
die(
"Images differ at x:" . $x . " y:" . $y . ".\n" .
"Us: r:" . $ourRGB[0] . " g:" . $ourRGB[1] . " b:" . $ourRGB[2] . "\n" .
"Them: r:" . $theirR . " g:" . $theirG . " b:" . $theirB . "\n"
);
}
}
exit("The images are identical.\n");
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment