Skip to content

Instantly share code, notes, and snippets.

@dperini
Created August 22, 2010 00:08

Revisions

  1. dperini revised this gist Aug 22, 2010. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion js2png.php
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,7 @@
    <?php

    /*
    * Author: Diego Perini @[email protected]
    * 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;
  2. dperini revised this gist Aug 22, 2010. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion js2png.php
    Original file line number Diff line number Diff line change
    @@ -12,7 +12,7 @@
    * Here is the javascript decoder:
    *
    * // decodes and execute Javascript
    * // code embedded in a PNG24 image
    * // code embedded in a PNG8 image
    * function getDataPNG(g,k){
    * var o;(o=new Image).src=g;
    * o.onload=function(){
  3. dperini revised this gist Aug 22, 2010. 1 changed file with 23 additions and 1 deletion.
    24 changes: 23 additions & 1 deletion js2png.php
    Original file line number Diff line number Diff line change
    @@ -1,13 +1,35 @@
    <?php

    /*
    * Author: Diego Perini
    * 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 PNG24 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';
  4. dperini created this gist Aug 22, 2010.
    49 changes: 49 additions & 0 deletions js2png.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,49 @@
    <?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);
    }
    ?>