Created
August 22, 2010 00:08
-
-
Save dperini/543067 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
* Author: Diego Perini <[email protected]> | |
* | |
* 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. | |
* | |
* Here is the javascript decoder: | |
* | |
* // decodes and execute Javascript | |
* // code embedded in a PNG8 image | |
* function getDataPNG(g,k){ | |
* var o;(o=new Image).src=g; | |
* o.onload=function(){ | |
* var a,b,c,d,e,f='', | |
* x=o.width,y=o.height; | |
* c=document.createElement('canvas'); | |
* e=c.getContext('2d');c.width=x;c.height=y; | |
* e.drawImage(o,0,0);d=e.getImageData(0,0,x,y).data; | |
* for(a=0;b=d[a];a+=4){f+=String.fromCharCode(b);}k(f)(); | |
* }; | |
* } | |
* | |
* // usage: | |
* // forget about the toString.constructor | |
* // just pass the path to your local js/png file | |
* getDataPNG('dist/nwmatcher.png', toString.constructor); | |
* | |
*/ | |
$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