<?php
ini_set('max_execution_time', 10000); //10000s
set_time_limit(10000);
ini_set('memory_limit', '256M');
function microtime_float() //pp 17.3.2014
{
list($usec, $sec) = explode(" ", microtime());
return ((float)$usec + (float)$sec);
}
$time = array();
$time[] = microtime_float();
function timePrint($time)
{
$time[] = microtime_float();
$t = array();
$t[] = round($time[count($time)-1]-$time[0],3);
for ($i=1;$i<count($time);$i++)
{
$t[] = round($time[$i]-$time[$i-1],3);
}
$t = implode(" | ",$t);
echo '<hr>Cas zpracovani '.$t.' s';
}
function transf(&$img,$w,$h)
{
$out = '';
$tmp = array(0,0,0);
$z = array(0,0,0);
$tmp = 0;
$tbl = array();
$code = array();
$n = 256;
$char_count = array();
for ($i=0,$j=-$n;$i<$n;++$i,++$j)
{
$tbl[$j] = chr($i);
$tbl[$i] = chr($i);
$code[$j] = $i;
$code[$i] = $i;
$char_count[$i] = 0;
}
ksort($tbl);
$k=0;
for ($j=0;$j<$h;++$j)
{
for ($i=0;$i<$w;++$i)
{
$rgba = imagecolorat($img, $i, $j);
$b = $rgba & 0xFF;
//echo ($code[$b-$tmp]).", ";
$out .= $tbl[$b-$tmp];
++$char_count[$code[$b-$tmp]];
// $tmp = $rgba;
$tmp = $b;
++$k;
}
//if ($j>1) break;
}
echo '<br>k1 =='.$k.' == ';
rsort($char_count);
$a = 0; $b=0; for ($i=0;$i<256;++$i) {if ($i>16) {$b+=$char_count[$i];} else {$a+=$char_count[$i];}}
echo " 16: ".$a ." / ".$b;
return $out;
}
$tbl_zigzag = //peter jpeg
array(
0, 1, 8, 16, 9, 2, 3, 10,
17, 24, 32, 25, 18, 11, 4, 5,
12, 19, 26, 33, 40, 48, 41, 34,
27, 20, 13, 6, 7, 14, 21, 28,
35, 42, 49, 56, 57, 50, 43, 36,
29, 22, 15, 23, 30, 37, 44, 51,
58, 59, 52, 45, 38, 31, 39, 46,
53, 60, 61, 54, 47, 55, 62, 63
);
function ReZigZag($inp,$zigzag)
{
$out = array();
$i=0;
for($i=0; $i<64; ++$i)
{
$out[$zigzag[$i]] = $inp[$i];
}
return $out;
}
function ZigZag($inp,$zigzag)
{
$out = array();
$i=0;
for($i=0; $i<64; ++$i)
{
$out[$i] = $inp[$zigzag[$i]];
}
return $out;
}
function transf2($img,$w,$h) //2 + zigzag
{
$out = '';
$tmp = array(0,0,0);
$z = array(0,0,0);
$tmp = 0;
$tbl = array();
$code = array();
$n = 256;
for ($i=0,$j=-$n;$i<$n;++$i,++$j)
{
$tbl[$j] = chr($i);
$tbl[$i] = chr($i);
$code[$j] = $i;
$code[$i] = $i;
}
ksort($tbl);
$k=0;
for ($j=0;$j<$h;$j+=8)
{
for ($i=0;$i<$w;$i+=8)
{
for ($s=0;$s<8 && $j+$s<$h;$s+=1)
{
for ($r=0;$r<8 && $i+$r<$w;$r+=1)
{
$rgba = imagecolorat($img, $i+$r, $j+$s);
$b = $rgba & 0xFF;
//echo ($code[$b-$tmp]).", ";
$out .= $tbl[$b-$tmp];
// $tmp = $rgba;
$tmp = $b;
++$k;
}
}
}
//if ($j>1) break;
}
echo '=='.$k.' ==';
return $out;
}
function transf3($img,$w,$h)
{
global $tbl_zigzag;
$out = '';
$tmp = array(0,0,0);
$z = array(0,0,0);
$tmp = 0;
$tbl = array();
$code = array();
$arr = array();
$n = 256;
for ($i=0,$j=-$n;$i<$n;++$i,++$j)
{
$tbl[$j] = chr($i);
$tbl[$i] = chr($i);
$code[$j] = $i;
$code[$i] = $i;
}
ksort($tbl);
$k=0;
for ($j=0;$j<$h;$j+=8)
{
for ($i=0;$i<$w;$i+=8)
{
$n = 0;
for ($s=0;$s<8 && $j+$s<$h;$s+=1)
{
for ($r=0;$r<8 && $i+$r<$w;$r+=1)
{
$rgba = imagecolorat($img, $i+$r, $j+$s);
$arr[$n] = $rgba & 0xFF;
++$n;
++$k;
}
}
//print_r($arr);
$arr = Zigzag($arr,$tbl_zigzag);
//print_r($arr);
$n = 0;
for ($s=0;$s<8 && $j+$s<$h;$s+=1)
{
for ($r=0;$r<8 && $i+$r<$w;$r+=1)
{
//echo ($code[$arr[$n]-$tmp]).", ";
$out .= $tbl[$arr[$n]-$tmp];
$tmp = $arr[$n];
++$n;
}
}
}
//if ($j>1) break;
}
echo '=='.$k.' ==';
return $out;
}
function transf4($img,$w,$h)
{
global $tbl_zigzag;
$out = '';
$tmp = array(0,0,0);
$z = array(0,0,0);
$tmp = 0;
$tbl = array();
$code = array();
$arr = array();
$n = 256;
for ($i=0,$j=-$n;$i<$n;++$i,++$j)
{
$tbl[$j] = chr($i);
$tbl[$i] = chr($i);
$code[$j] = $i;
$code[$i] = $i;
}
ksort($tbl);
$k=0;
for ($j=0;$j<$h;$j+=8)
{
for ($i=0;$i<$w;$i+=8)
{
$n = 0;
$tmp = 0;
for ($s=0;$s<8 && $j+$s<$h;$s+=1)
{
for ($r=0;$r<8 && $i+$r<$w;$r+=1)
{
$rgba = imagecolorat($img, $i+$r, $j+$s);
$arr[$n] = $rgba & 0xFF;
$tmp+= $arr[$n];
++$n;
++$k;
}
}
//echo '<br>x'.$tmp;
$tmp = $tmp>>6;
//print_r($arr);
$arr = Zigzag($arr,$tbl_zigzag);
//if (!isset($tbl[$tmp])) echo '<br>'.$tmp.'<br>';
//else{
$out .= $tbl[$tmp];
//print_r($arr);
$n = 0;
for ($s=0;$s<8 && $j+$s<$h;$s+=1)
{
//echo ($code[$arr[$n]-$tmp]).", ";
for ($r=1;$r<8 && $i+$r<$w;$r+=1)
{
echo ($code[$arr[$n]-$tmp]).", ";
$out .= $tbl[$arr[$n]-$tmp];
// $tmp = $arr[$n];
++$n;
}
}
//}
}
if ($j>1) break;
}
echo '=='.$k.' ==';
return $out;
}
function transf5($img,$w,$h) //2 + zigzag
{
$out = '';
$tmp = array(0,0,0);
$z = array(0,0,0);
$tmp = 0;
$tbl = array();
$code = array();
$n = 256;
$char_count = array();
for ($i=0,$j=-$n;$i<$n;++$i,++$j)
{
$tbl[$j] = chr($i);
$tbl[$i] = chr($i);
$code[$j] = $i;
$code[$i] = $i;
$char_count[$i] = 0;
}
ksort($tbl);
$k=0;
$b=0;
$r=0;
$s=0;
$si = 8;
$sj = 8;
for ($j=0;$j<$h;$j+=$sj)
{
for ($i=0;$i<$w;$i+=$si)
{
$rgba = imagecolorat($img, $i, $j);
$b = $rgba & 0xFF; // staci red barva
$out .= $tbl[$b-$tmp];
++$char_count[$code[$b-$tmp]];
$tmp = $b;
++$k;
//echo ($code[$tmp]).", ";
for ($s=0;$s<$sj && $j+$s<$h;++$s)
{
for ($r=0;$r<$si && $i+$r<$w;++$r)
{
$rgba = imagecolorat($img, $i+$r, $j+$s);
$b = $rgba & 0xFF; // staci red barva
//echo ($code[$b-$tmp]).", ";
if ($r>0 || $s>0)
{
$out .= $tbl[$b-$tmp];
//echo ($code[$b-$tmp]).", ";
++$k;
++$char_count[$code[$b-$tmp]];
}
}
//echo $k.' ';
}
//echo $k.' ';
}
//if ($j>1) break;
//if ($k>300) break;
}
echo '<br>k5 =='.$k.' == ';
rsort($char_count);
$a = 0; $b=0; for ($i=0;$i<256;++$i) {if ($i>32) {$b+=$char_count[$i];} else {$a+=$char_count[$i];}}
echo " 32: ".$a ." / ".$b;
return $out;
}
$name1 = "img/jp-bwgirl.jpeg"; //
//$name1 = "img/jp-bwgirl-2.jpeg"; //
$name2 = "tmp/xxx"; //
$bool = file_exists($name1);
echo 'Soubor '. ($bool ? 'existuje' : 'chyba neexistuje').'<br>';
$b = filesize($name1);
$img = imagecreatefromjpeg($name1);
$w = imagesx($img);
$h = imagesy($img);
$out = '';
$out2 ='';
$out3 = '';
$out4 = '';
$out5 = '';
$out = transf($img,$w,$h);
//$out2 = transf2($img,$w,$h);
//$out3 = transf3($img,$w,$h);
//$out4 = transf4($img,$w,$h);
$out5 = transf5($img,$w,$h);
imagedestroy($img);
/*
$file2 = fopen($name2, "wb");
fwrite($file2,$out);
fclose($file2);
*/
$a=0; $a2=0; $a3=0; $a4=0; $a5=0;
$out = gzcompress($out,9); $a = strlen($out); unset($out);
//$out2 = gzcompress($out2,9); $a2 = strlen($out2); unset($out2);
//$out3 = gzcompress($out3,9); $a3 = strlen($out3); unset($out3);
//$out4 = gzcompress($out4,9); $a4 = strlen($out4); unset($out4);
$out5 = gzcompress($out5,9); $a5 = strlen($out5); unset($out5);
echo '<br>out / in = floor(10000*$a/$b)/100 % gzip';
echo '<br>'.$a. ' / '. $b .' = '.(floor(10000*$a/$b)/100).'% gzip';
echo '<br>'.$a2. ' / '. $b .' = '.(floor(10000*$a2/$b)/100).'% gzip';
echo '<br>'.$a3. ' / '. $b .' = '.(floor(10000*$a3/$b)/100).'% gzip';
echo '<br>'.$a4. ' / '. $b .' = '.(floor(10000*$a4/$b)/100).'% gzip';
echo '<br>'.$a5. ' / '. $b .' = '.(floor(10000*$a5/$b)/100).'% gzip';
echo '<br>'.$w. ' x '. $h .' = '.($w * $h) .'... musi se shodovat s k';
timePrint($time);
?>
<?php
// $code[$j] = $i;
// $code[$i] = $i;
//foreach($tbl as $i=>$j) echo ",".$i.":".$j; return ;
//var_dump($rgba);
// $r = ($rgba >> 16) & 0xFF;
// $g = ($rgba >> 8) & 0xFF;
//var_dump($b);
//echo $r.",".$g.",".$b.", ";
// for ($k=0;$k<3;++$k)
// {
// $z[$k] = $rgba[$k]-$tmp[$k];
// $z[$k] += $z[$k]<0 ? 256 : 0;
// $out .= $tbl[$z[$k]];
// $out .= $tbl[$rgba[$k]-$tmp[$k]];
//echo $rgba[$k].", ";
// $out .= $tbl[$rgba[$k]];
// }
// $out .= $tbl[$r];
// $out .= $tbl[$g];
// $out .= $tbl[$b];
// $out .= 'b';
//$out .= $tbl[$b];
//echo ($code[$b-$tmp]).", ";
?>
Kdyby mel nekdo chut si hrat se source-code...