Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?
- // File and new size
- $action = $argv[1];
- $output = dirname(__FILE__).'/test_c.gif';
- $max_pix = 20;
- $colors = array(
- "A" => array(255, 255, 255),
- "B" => array(204, 204, 204),
- "C" => array(153, 153, 153),
- "D" => array(102, 102, 102),
- "E" => array(51, 51, 51),
- "F" => array(0, 0, 0)
- );
- $bintree = array(
- "AB" => array("0000", array("A"=>"00", "B"=>"10", "C"=>"010", "D"=>"011", "E"=>"110", "F"=>"111")),
- "AC" => array("0001", array("A"=>"00", "B"=>"010", "C"=>"10", "D"=>"011", "E"=>"110", "F"=>"111")),
- "AD" => array("0010", array("A"=>"00", "B"=>"010", "C"=>"011", "D"=>"10", "E"=>"110", "F"=>"111")),
- "AE" => array("0011", array("A"=>"00", "B"=>"010", "C"=>"011", "D"=>"110", "E"=>"10", "F"=>"111")),
- "AF" => array("0100", array("A"=>"00", "B"=>"010", "C"=>"011", "D"=>"110", "E"=>"111", "F"=>"10")),
- "BC" => array("0101", array("A"=>"010", "B"=>"00", "C"=>"10", "D"=>"011", "E"=>"110", "F"=>"111")),
- "BD" => array("0110", array("A"=>"010", "B"=>"00", "C"=>"011", "D"=>"10", "E"=>"110", "F"=>"111")),
- "BE" => array("0111", array("A"=>"010", "B"=>"00", "C"=>"011", "D"=>"110", "E"=>"10", "F"=>"111")),
- "BF" => array("1000", array("A"=>"010", "B"=>"00", "C"=>"011", "D"=>"110", "E"=>"111", "F"=>"10")),
- "CD" => array("1001", array("A"=>"010", "B"=>"011", "C"=>"00", "D"=>"10", "E"=>"110", "F"=>"111")),
- "CE" => array("1010", array("A"=>"010", "B"=>"011", "C"=>"00", "D"=>"110", "E"=>"10", "F"=>"111")),
- "CF" => array("1011", array("A"=>"010", "B"=>"011", "C"=>"00", "D"=>"110", "E"=>"111", "F"=>"10")),
- "DE" => array("1100", array("A"=>"010", "B"=>"011", "C"=>"110", "D"=>"00", "E"=>"10", "F"=>"111")),
- "DF" => array("1101", array("A"=>"010", "B"=>"011", "C"=>"110", "D"=>"00", "E"=>"111", "F"=>"10")),
- "EF" => array("1110", array("A"=>"010", "B"=>"011", "C"=>"110" ,"D"=>"111", "E"=>"00", "F"=>"10"))
- );
- $bintreerev = array(
- "0000" => array("00"=>"A", "10"=>"B", "010"=>"C", "011"=>"D", "110"=>"E", "111"=>"F"),
- "0001" => array("00"=>"A", "010"=>"B", "10"=>"C", "011"=>"D", "110"=>"E", "111"=>"F"),
- "0010" => array("00"=>"A", "010"=>"B", "011"=>"C", "10"=>"D", "110"=>"E", "111"=>"F"),
- "0011" => array("00"=>"A", "010"=>"B", "011"=>"C", "110"=>"D", "10"=>"E", "111"=>"F"),
- "0100" => array("00"=>"A", "010"=>"B", "011"=>"C", "110"=>"D", "111"=>"E", "10"=>"F"),
- "0101" => array("010"=>"A", "00"=>"B", "10"=>"C", "011"=>"D", "110"=>"E", "111"=>"F"),
- "0110" => array("010"=>"A", "00"=>"B", "011"=>"C", "10"=>"D", "110"=>"E", "111"=>"F"),
- "0111" => array("010"=>"A", "00"=>"B", "011"=>"C", "110"=>"D", "10"=>"E", "111"=>"F"),
- "1000" => array("010"=>"A", "00"=>"B", "011"=>"C", "110"=>"D", "111"=>"E", "10"=>"F"),
- "1001" => array("010"=>"A", "011"=>"B", "00"=>"C", "10"=>"D", "110"=>"E", "111"=>"F"),
- "1010" => array("010"=>"A", "011"=>"B", "00"=>"C", "110"=>"D", "10"=>"E", "111"=>"F"),
- "1011" => array("010"=>"A", "011"=>"B", "00"=>"C", "110"=>"D", "111"=>"E", "10"=>"F"),
- "1100" => array("010"=>"A", "011"=>"B", "110"=>"C", "00"=>"D", "10"=>"E", "111"=>"F"),
- "1101" => array("010"=>"A", "011"=>"B", "110"=>"C", "00"=>"D", "111"=>"E", "10"=>"F"),
- "1110" => array("010"=>"A", "011"=>"B", "110"=>"C" ,"111"=>"D", "00"=>"E", "10"=>"F")
- );
- $b64 = array(
- "A", "B", "C", "D", "E", "F", "G", "H",
- "I", "J", "K", "L", "M", "N", "O", "P",
- "Q", "R", "S", "T", "U", "V", "W", "X",
- "Y", "Z", "a", "b", "c", "d", "e", "f",
- "g", "h", "i", "j", "k", "l", "m", "n",
- "o", "p", "q", "r", "s", "t", "u", "v",
- "w", "x", "y", "z", "0", "1", "2", "3",
- "4", "5", "6", "7", "8", "9", "+", "/"
- );
- $b64rev = array(
- "A"=>0, "B"=>1, "C"=>2, "D"=>3, "E"=>4, "F"=>5, "G"=>6, "H"=>7,
- "I"=>8, "J"=>9, "K"=>10, "L"=>11, "M"=>12, "N"=>13, "O"=>14, "P"=>15,
- "Q"=>16, "R"=>17, "S"=>18, "T"=>19, "U"=>20, "V"=>21, "W"=>22, "X"=>23,
- "Y"=>24, "Z"=>25, "a"=>26, "b"=>27, "c"=>28, "d"=>29, "e"=>30, "f"=>31,
- "g"=>32, "h"=>33, "i"=>34, "j"=>35, "k"=>36, "l"=>37, "m"=>38, "n"=>39,
- "o"=>40, "p"=>41, "q"=>42, "r"=>43, "s"=>44, "t"=>45, "u"=>46, "v"=>47,
- "w"=>48, "x"=>49, "y"=>50, "z"=>51, "0"=>52, "1"=>53, "2"=>54, "3"=>55,
- "4"=>56, "5"=>57, "6"=>58, "7"=>59, "8"=>60, "9"=>61, "+"=>62, "/"=>63
- );
- if ($action == "encode") {
- $filename = dirname(__FILE__).'/'.$argv[2].'.jpg';
- // Get new sizes
- list($width, $height) = getimagesize($filename);
- if ($width>=$height) {
- $r = $max_pix/$width;
- $w = $max_pix;
- $h = round($r*$height);
- $o = "L"; // Orientation
- } else {
- $r = $max_pix/$height;
- $h = $max_pix;
- $w = round($r*$width);
- $o = "P"; // Orientation
- }
- // Load
- $n_img = imagecreatetruecolor($w, $h);
- $source = imagecreatefromjpeg($filename);
- function closestColor($r, $g, $b) {
- global $colors;
- $diff_array = array();
- foreach ($colors as $v) {
- $diff = pow(($r - $v[0]) * 0.299, 2) + pow(($g - $v[1]) * 0.587,2) + pow(($b - $v[2]) * 0.114,2);
- $diff_array[] = $diff;
- }
- $smallest = min($diff_array);
- $key = array_search($smallest, $diff_array);
- return $key;
- }
- // Resize
- imagecopyresized($n_img, $source, 0, 0, 0, 0, $w, $h, $width, $height);
- $c_img = imagecreatetruecolor($w, $h);
- $string = "";
- for ($y=0; $y<$h; $y++) { // Width
- for ($x=0; $x<$w; $x++) { // Height
- $rgb = imagecolorat($n_img, $x, $y);
- $r = ($rgb >> 16) & 0xFF;
- $g = ($rgb >> 8) & 0xFF;
- $b = $rgb & 0xFF;
- $paint = closestColor($r, $g, $b);
- if($paint==0){$paint="A";}
- if($paint==1){$paint="B";}
- if($paint==2){$paint="C";}
- if($paint==3){$paint="D";}
- if($paint==4){$paint="E";}
- if($paint==5){$paint="F";}
- $new_color = imagecolorallocate($c_img, $colors[$paint][0], $colors[$paint][1], $colors[$paint][2]);
- imagesetpixel($c_img, $x, $y, $new_color);
- $string .= $paint;
- }
- }
- // Output
- imagegif($c_img, $output);
- $bin_table = array(
- "A" => substr_count($string, "A"),
- "B" => substr_count($string, "B"),
- "C" => substr_count($string, "C"),
- "D" => substr_count($string, "D"),
- "E" => substr_count($string, "E"),
- "F" => substr_count($string, "F")
- );
- $highest = max($bin_table);
- foreach ($bin_table as $key => $value) {
- if ($value == $highest) {
- $keyone = $key;
- }
- }
- unset($bin_table[$keyone]);
- $highest = max($bin_table);
- foreach ($bin_table as $key => $value) {
- if ($value == $highest) {
- $keytwo = $key;
- }
- }
- $bt_id = $keyone.$keytwo;
- $bt_id = (array_key_exists($bt_id,$bintree) ? $bt_id : strrev($bt_id));
- $strfile = fopen(dirname(__FILE__)."/test.txt", "wb");
- $binary_data = $w==$max_pix?"1":"0";
- $binary_data.= $bintree[$bt_id][0];
- $newstring = str_replace("A", $bintree[$bt_id][1]["A"], $string);
- $newstring = str_replace("B", $bintree[$bt_id][1]["B"], $newstring);
- $newstring = str_replace("C", $bintree[$bt_id][1]["C"], $newstring);
- $newstring = str_replace("D", $bintree[$bt_id][1]["D"], $newstring);
- $newstring = str_replace("E", $bintree[$bt_id][1]["E"], $newstring);
- $newstring = str_replace("F", $bintree[$bt_id][1]["F"], $newstring);
- $binary_data.= $newstring;
- $chunks = str_split($binary_data, 6);
- foreach ($chunks as &$char) {
- while (strlen($char) < 6) {
- $char .= "0";
- }
- $char = $b64[bindec($char)];
- }
- fputs($strfile, implode("", $chunks));
- }
- if ($action == "decode") {
- $decode_string = $argv[2];
- $bin_string = "";
- while (strlen($decode_string) > 0) {
- $a = substr($decode_string, 0, 1);
- $t = decbin($b64rev[$a]);
- while (strlen($t) < 6) {
- $t = "0".$t;
- }
- $bin_string .= $t;
- $decode_string = substr($decode_string, 1);
- }
- // Bit decode
- // Get orientation (Portrait or Landscape)
- // 1 = Landscape
- // 0 = Portrait
- $o = substr($bin_string, 0, 1);
- $o = 1;
- $bin_string = substr($bin_string, 1);
- // Get the bitcode for the correct binary tree
- $bt = substr($bin_string, 0, 4);
- $bin_string = substr($bin_string, 4);
- foreach ($bintreerev as $key => $value) {
- if ($bt == $key) { $bt = $value; }
- }
- // Decode the binary string into pixels.
- $pixel_count = 0;
- $img_string = "";
- while (strlen($bin_string) > 1) {
- $t = substr($bin_string, 0, 2);
- $delete = 2;
- if (!array_key_exists($t, $bt)) {
- $t = substr($bin_string, 0, 3);
- ++$delete;
- }
- $img_string .= $bt[$t];
- $bin_string = substr($bin_string, $delete);
- ++$pixel_count;
- }
- // Calculate image resolution
- switch ($o) {
- case 0:
- $h = $max_pix;
- $w = floor($pixel_count/$max_pix);
- break;
- case 1:
- $w = $max_pix;
- $h = floor($pixel_count/$max_pix);
- break;
- }
- // Generate Pixels
- $c_img = imagecreatetruecolor($w, $h);
- $n_img = imagecreatetruecolor(($w*2), ($h*2));
- for ($y=0; $y<$h; $y++) { // Width
- for ($x=0; $x<$w; $x++) { // Height
- $pix = substr($img_string, 0, 1);
- $new_color = imagecolorallocate($c_img, $colors[$pix][0], $colors[$pix][1], $colors[$pix][2]);
- imagesetpixel($c_img, $x, $y, $new_color);
- $img_string = substr($img_string, 1);
- }
- }
- // Output
- imageantialias($n_img, 1);
- imagealphablending($n_img, 1);
- imagecopyresampled($n_img, $c_img, 0, 0, 0, 0, ($w*2), ($h*2), $w, $h);
- $gaussian = array(
- array(1.0, 2.0, 1.0),
- array(2.0, 4.0, 2.0),
- array(1.0, 2.0, 1.0)
- );
- imageconvolution($n_img, $gaussian, 16, 0);
- imagegif($n_img, $output);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement