Skip to content

Instantly share code, notes, and snippets.

@dperini
Created August 22, 2010 00:08
Show Gist options
  • Save dperini/543067 to your computer and use it in GitHub Desktop.
Save dperini/543067 to your computer and use it in GitHub Desktop.
<?php
/*
* Author: Diego Perini
*
* this is what I have in my build system to embed
* the packed version of NWMatcher in a PNG file;
* actually any data like JSON or XML can be
* transported in compressed PNG files and
* avoids HTTP compression requirements.
*/
$filename = 'dist/nwmatcher-pac.js';
if (file_exists($filename)) {
$size = filesize($filename);
$width = ceil(sqrt($size / 1));
$height = $width - 1;
$im = imagecreate($width, $height);
$fs = fopen($filename, 'r');
$data = fread($fs, $size);
fclose($fs);
$i = 0;
$colors = array();
for ($y = 0; $height > $y; ++$y) {
for ($x = 0; $width > $x; ++$x) {
if ($i < $size) {
$byte = ord($data[$i]);
if (!isset($colors[$byte])) {
$colors[$byte] = imagecolorallocate($im, $byte, $byte, $byte);
}
$color = $colors[$byte];
imagesetpixel($im, $x, $y, $color);
++$i;
}
}
}
header('Content-Type: image/png');
imagepng($im, NULL, 9);
imagedestroy($im);
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment