Advertisement
Guest User

jdstankosky twitter image

a guest
Apr 16th, 2013
351
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 10.15 KB | None | 0 0
  1. <?
  2. // File and new size
  3. $action = $argv[1];
  4.  
  5. $output = dirname(__FILE__).'/test_c.gif';
  6.  
  7. $max_pix = 20;
  8.  
  9. $colors = array(
  10.     "A" => array(255, 255, 255),
  11.     "B" => array(204, 204, 204),
  12.     "C" => array(153, 153, 153),
  13.     "D" => array(102, 102, 102),
  14.     "E" => array(51, 51, 51),
  15.     "F" => array(0, 0, 0)
  16. );
  17.  
  18. $bintree = array(
  19.     "AB" => array("0000", array("A"=>"00",  "B"=>"10",  "C"=>"010", "D"=>"011", "E"=>"110", "F"=>"111")),
  20.     "AC" => array("0001", array("A"=>"00",  "B"=>"010", "C"=>"10",  "D"=>"011", "E"=>"110", "F"=>"111")),
  21.     "AD" => array("0010", array("A"=>"00",  "B"=>"010", "C"=>"011", "D"=>"10",  "E"=>"110", "F"=>"111")),
  22.     "AE" => array("0011", array("A"=>"00",  "B"=>"010", "C"=>"011", "D"=>"110", "E"=>"10",  "F"=>"111")),
  23.     "AF" => array("0100", array("A"=>"00",  "B"=>"010", "C"=>"011", "D"=>"110", "E"=>"111", "F"=>"10")),
  24.  
  25.     "BC" => array("0101", array("A"=>"010", "B"=>"00",  "C"=>"10",  "D"=>"011", "E"=>"110", "F"=>"111")),
  26.     "BD" => array("0110", array("A"=>"010", "B"=>"00",  "C"=>"011", "D"=>"10",  "E"=>"110", "F"=>"111")),
  27.     "BE" => array("0111", array("A"=>"010", "B"=>"00",  "C"=>"011", "D"=>"110", "E"=>"10",  "F"=>"111")),
  28.     "BF" => array("1000", array("A"=>"010", "B"=>"00",  "C"=>"011", "D"=>"110", "E"=>"111", "F"=>"10")),
  29.  
  30.     "CD" => array("1001", array("A"=>"010", "B"=>"011", "C"=>"00",  "D"=>"10",  "E"=>"110", "F"=>"111")),
  31.     "CE" => array("1010", array("A"=>"010", "B"=>"011", "C"=>"00",  "D"=>"110", "E"=>"10",  "F"=>"111")),
  32.     "CF" => array("1011", array("A"=>"010", "B"=>"011", "C"=>"00",  "D"=>"110", "E"=>"111", "F"=>"10")),
  33.  
  34.     "DE" => array("1100", array("A"=>"010", "B"=>"011", "C"=>"110", "D"=>"00",  "E"=>"10",  "F"=>"111")),
  35.     "DF" => array("1101", array("A"=>"010", "B"=>"011", "C"=>"110", "D"=>"00",  "E"=>"111", "F"=>"10")),
  36.  
  37.     "EF" => array("1110", array("A"=>"010", "B"=>"011", "C"=>"110" ,"D"=>"111", "E"=>"00",  "F"=>"10"))
  38. );
  39.  
  40. $bintreerev = array(
  41.     "0000" => array("00"=>"A",  "10"=>"B", "010"=>"C", "011"=>"D", "110"=>"E", "111"=>"F"),
  42.     "0001" => array("00"=>"A",  "010"=>"B", "10"=>"C", "011"=>"D", "110"=>"E", "111"=>"F"),
  43.     "0010" => array("00"=>"A",  "010"=>"B", "011"=>"C", "10"=>"D", "110"=>"E", "111"=>"F"),
  44.     "0011" => array("00"=>"A",  "010"=>"B", "011"=>"C", "110"=>"D", "10"=>"E",  "111"=>"F"),
  45.     "0100" => array("00"=>"A",  "010"=>"B", "011"=>"C", "110"=>"D", "111"=>"E", "10"=>"F"),
  46.  
  47.     "0101" => array("010"=>"A", "00"=>"B",  "10"=>"C",  "011"=>"D", "110"=>"E", "111"=>"F"),
  48.     "0110" => array("010"=>"A", "00"=>"B",  "011"=>"C", "10"=>"D",  "110"=>"E", "111"=>"F"),
  49.     "0111" => array("010"=>"A", "00"=>"B",  "011"=>"C", "110"=>"D", "10"=>"E",  "111"=>"F"),
  50.     "1000" => array("010"=>"A", "00"=>"B",  "011"=>"C", "110"=>"D", "111"=>"E", "10"=>"F"),
  51.  
  52.     "1001" => array("010"=>"A", "011"=>"B", "00"=>"C",  "10"=>"D",  "110"=>"E", "111"=>"F"),
  53.     "1010" => array("010"=>"A", "011"=>"B", "00"=>"C",  "110"=>"D", "10"=>"E",  "111"=>"F"),
  54.     "1011" => array("010"=>"A", "011"=>"B", "00"=>"C",  "110"=>"D", "111"=>"E", "10"=>"F"),
  55.  
  56.     "1100" => array("010"=>"A", "011"=>"B", "110"=>"C", "00"=>"D",  "10"=>"E",  "111"=>"F"),
  57.     "1101" => array("010"=>"A", "011"=>"B", "110"=>"C", "00"=>"D",  "111"=>"E", "10"=>"F"),
  58.  
  59.     "1110" => array("010"=>"A", "011"=>"B", "110"=>"C" ,"111"=>"D", "00"=>"E",  "10"=>"F")
  60. );
  61.  
  62. $b64 = array(
  63.     "A", "B", "C", "D", "E", "F", "G", "H",
  64.     "I", "J", "K", "L", "M", "N", "O", "P",
  65.     "Q", "R", "S", "T", "U", "V", "W", "X",
  66.     "Y", "Z", "a", "b", "c", "d", "e", "f",
  67.     "g", "h", "i", "j", "k", "l", "m", "n",
  68.     "o", "p", "q", "r", "s", "t", "u", "v",
  69.     "w", "x", "y", "z", "0", "1", "2", "3",
  70.     "4", "5", "6", "7", "8", "9", "+", "/"
  71. );
  72.  
  73. $b64rev = array(
  74.     "A"=>0,  "B"=>1,  "C"=>2,  "D"=>3,  "E"=>4,  "F"=>5,  "G"=>6,  "H"=>7,
  75.     "I"=>8,  "J"=>9,  "K"=>10, "L"=>11, "M"=>12, "N"=>13, "O"=>14, "P"=>15,
  76.     "Q"=>16, "R"=>17, "S"=>18, "T"=>19, "U"=>20, "V"=>21, "W"=>22, "X"=>23,
  77.     "Y"=>24, "Z"=>25, "a"=>26, "b"=>27, "c"=>28, "d"=>29, "e"=>30, "f"=>31,
  78.     "g"=>32, "h"=>33, "i"=>34, "j"=>35, "k"=>36, "l"=>37, "m"=>38, "n"=>39,
  79.     "o"=>40, "p"=>41, "q"=>42, "r"=>43, "s"=>44, "t"=>45, "u"=>46, "v"=>47,
  80.     "w"=>48, "x"=>49, "y"=>50, "z"=>51, "0"=>52, "1"=>53, "2"=>54, "3"=>55,
  81.     "4"=>56, "5"=>57, "6"=>58, "7"=>59, "8"=>60, "9"=>61, "+"=>62, "/"=>63
  82. );
  83.  
  84. if ($action == "encode") {
  85.     $filename = dirname(__FILE__).'/'.$argv[2].'.jpg';
  86.     // Get new sizes
  87.     list($width, $height) = getimagesize($filename);
  88.     if ($width>=$height) {
  89.         $r = $max_pix/$width;
  90.         $w = $max_pix;
  91.         $h = round($r*$height);
  92.         $o = "L"; // Orientation
  93.     } else {
  94.         $r = $max_pix/$height;
  95.         $h = $max_pix;
  96.         $w = round($r*$width);
  97.         $o = "P"; // Orientation
  98.     }
  99.  
  100.     // Load
  101.     $n_img = imagecreatetruecolor($w, $h);
  102.     $source = imagecreatefromjpeg($filename);
  103.     function closestColor($r, $g, $b) {
  104.         global $colors;
  105.         $diff_array = array();
  106.         foreach ($colors as $v) {
  107.             $diff = pow(($r - $v[0]) * 0.299, 2) + pow(($g - $v[1]) * 0.587,2) + pow(($b - $v[2]) * 0.114,2);
  108.             $diff_array[] = $diff;
  109.         }
  110.         $smallest = min($diff_array);
  111.         $key = array_search($smallest, $diff_array);
  112.         return $key;
  113.     }
  114.  
  115.     // Resize
  116.     imagecopyresized($n_img, $source, 0, 0, 0, 0, $w, $h, $width, $height);
  117.  
  118.     $c_img = imagecreatetruecolor($w, $h);
  119.     $string = "";
  120.     for ($y=0; $y<$h; $y++) {     // Width
  121.         for ($x=0; $x<$w; $x++) { // Height
  122.             $rgb = imagecolorat($n_img, $x, $y);
  123.             $r = ($rgb >> 16) & 0xFF;
  124.             $g = ($rgb >> 8) & 0xFF;
  125.             $b = $rgb & 0xFF;
  126.             $paint = closestColor($r, $g, $b);
  127.             if($paint==0){$paint="A";}
  128.             if($paint==1){$paint="B";}
  129.             if($paint==2){$paint="C";}
  130.             if($paint==3){$paint="D";}
  131.             if($paint==4){$paint="E";}
  132.             if($paint==5){$paint="F";}
  133.             $new_color = imagecolorallocate($c_img, $colors[$paint][0], $colors[$paint][1], $colors[$paint][2]);
  134.             imagesetpixel($c_img, $x, $y, $new_color);
  135.             $string .= $paint;
  136.         }
  137.     }
  138.  
  139.  
  140.     // Output
  141.     imagegif($c_img, $output);
  142.  
  143.     $bin_table = array(
  144.         "A" => substr_count($string, "A"),
  145.         "B" => substr_count($string, "B"),
  146.         "C" => substr_count($string, "C"),
  147.         "D" => substr_count($string, "D"),
  148.         "E" => substr_count($string, "E"),
  149.         "F" => substr_count($string, "F")
  150.     );
  151.  
  152.  
  153.     $highest = max($bin_table);
  154.     foreach ($bin_table as $key => $value) {
  155.         if ($value == $highest) {
  156.             $keyone = $key;
  157.         }
  158.     }
  159.     unset($bin_table[$keyone]);
  160.     $highest = max($bin_table);
  161.     foreach ($bin_table as $key => $value) {
  162.         if ($value == $highest) {
  163.             $keytwo = $key;
  164.         }
  165.     }
  166.     $bt_id = $keyone.$keytwo;
  167.     $bt_id = (array_key_exists($bt_id,$bintree) ? $bt_id : strrev($bt_id));
  168.     $strfile = fopen(dirname(__FILE__)."/test.txt", "wb");
  169.  
  170.     $binary_data = $w==$max_pix?"1":"0";
  171.     $binary_data.= $bintree[$bt_id][0];
  172.  
  173.     $newstring = str_replace("A", $bintree[$bt_id][1]["A"], $string);
  174.     $newstring = str_replace("B", $bintree[$bt_id][1]["B"], $newstring);
  175.     $newstring = str_replace("C", $bintree[$bt_id][1]["C"], $newstring);
  176.     $newstring = str_replace("D", $bintree[$bt_id][1]["D"], $newstring);
  177.     $newstring = str_replace("E", $bintree[$bt_id][1]["E"], $newstring);
  178.     $newstring = str_replace("F", $bintree[$bt_id][1]["F"], $newstring);
  179.  
  180.     $binary_data.= $newstring;
  181.     $chunks = str_split($binary_data, 6);
  182.  
  183.     foreach ($chunks as &$char) {
  184.         while (strlen($char) < 6) {
  185.             $char .= "0";
  186.         }
  187.         $char = $b64[bindec($char)];
  188.     }
  189.  
  190.     fputs($strfile, implode("", $chunks));
  191. }
  192.  
  193. if ($action == "decode") {
  194.     $decode_string = $argv[2];
  195.     $bin_string = "";
  196.     while (strlen($decode_string) > 0) {
  197.         $a = substr($decode_string, 0, 1);
  198.         $t = decbin($b64rev[$a]);
  199.         while (strlen($t) < 6) {
  200.             $t = "0".$t;
  201.         }
  202.         $bin_string .= $t;
  203.         $decode_string = substr($decode_string, 1);
  204.     }
  205.  
  206.     // Bit decode
  207.  
  208.     // Get orientation (Portrait or Landscape)
  209.     //     1 = Landscape
  210.     //     0 = Portrait
  211.     $o = substr($bin_string, 0, 1);
  212.     $o = 1;
  213.     $bin_string = substr($bin_string, 1);
  214.  
  215.     // Get the bitcode for the correct binary tree
  216.     $bt = substr($bin_string, 0, 4);
  217.     $bin_string = substr($bin_string, 4);
  218.     foreach ($bintreerev as $key => $value) {
  219.         if ($bt == $key) { $bt = $value; }
  220.     }
  221.  
  222.     // Decode the binary string into pixels.
  223.     $pixel_count = 0;
  224.     $img_string = "";
  225.     while (strlen($bin_string) > 1) {
  226.         $t = substr($bin_string, 0, 2);
  227.         $delete = 2;
  228.         if (!array_key_exists($t, $bt)) {
  229.             $t = substr($bin_string, 0, 3);
  230.             ++$delete;
  231.         }
  232.         $img_string .= $bt[$t];
  233.         $bin_string = substr($bin_string, $delete);
  234.         ++$pixel_count;
  235.     }
  236.  
  237.     // Calculate image resolution
  238.     switch ($o) {
  239.         case 0:
  240.             $h = $max_pix;
  241.             $w = floor($pixel_count/$max_pix);
  242.             break;
  243.         case 1:
  244.             $w = $max_pix;
  245.             $h = floor($pixel_count/$max_pix);
  246.             break;
  247.     }
  248.  
  249.     // Generate Pixels
  250.     $c_img = imagecreatetruecolor($w, $h);
  251.     $n_img = imagecreatetruecolor(($w*2), ($h*2));
  252.  
  253.     for ($y=0; $y<$h; $y++) {     // Width
  254.         for ($x=0; $x<$w; $x++) { // Height
  255.             $pix = substr($img_string, 0, 1);
  256.             $new_color = imagecolorallocate($c_img, $colors[$pix][0], $colors[$pix][1], $colors[$pix][2]);
  257.             imagesetpixel($c_img, $x, $y, $new_color);
  258.             $img_string = substr($img_string, 1);
  259.         }
  260.     }
  261.  
  262.     // Output
  263.     imageantialias($n_img, 1);
  264.     imagealphablending($n_img, 1);
  265.     imagecopyresampled($n_img, $c_img, 0, 0, 0, 0, ($w*2), ($h*2), $w, $h);
  266.     $gaussian = array(
  267.         array(1.0, 2.0, 1.0),
  268.         array(2.0, 4.0, 2.0),
  269.         array(1.0, 2.0, 1.0)
  270.     );
  271.     imageconvolution($n_img, $gaussian, 16, 0);
  272.     imagegif($n_img, $output);
  273. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement