function readln () {
$ stdin = fopen ( 'php: // stdin' , 'r' );
return trim ( fread ( $ stdin , 2048 ));
}
')
function ascii_decrypt ( $ encryptedtext )
{
$ encryptedtext = str_replace ( '' , '' , $ encryptedtext );
$ encryptedtext = str_replace ( " \ n " , '' , $ encryptedtext );
$ encryptedtext = str_replace ( " \ r " , '' , $ encryptedtext );
$ blockstack = str_split ( $ encryptedtext , 8 );
$ plaintext = "" ;
while ( list ( $ key , $ val ) = each ( $ blockstack ))
{
$ plaintext . = bin2ascii ( $ val );
}
return $ plaintext ;
}
function ascii_encrypt ( $ plaintext )
{
$ plaintextlength = strlen ( $ plaintext );
$ i = 0 ;
for ( $ x = 0 ; $ x < $ plaintextlength ; $ x ++ )
{
$ pt . = ascii2bin ( $ plaintext [ $ x ]);
// $ encryptedtext. = "";
}
return $ pt ;
}
function ascii2bin ( $ char )
{
$ char = decbin ( ord ( $ char ));
$ char = str_pad ( $ char , 8 , '0' , STR_PAD_LEFT);
return $ char ;
}
function bin2ascii ( $ binary )
{
$ char = substr ( $ binary , $ x , 8 );
$ char = chr ( bindec ( $ char ));
return $ char ;
}
echo "Input file:" ;
$ fl = readln ( $ fl );
if ( file_exists ( $ fl )) $ file = stripslashes ( file_get_contents ( $ fl )); else die ( "Input file not found! \ n " );
// header ('Content-Type: image / png');
// Convert the file contents to zeroes and ones ...
// And for some reason we turn it over)
$ e = strrev (ascii_encrypt ( $ file ));
//// Simple code to calculate the height and width of the picture
$ l = strlen ( $ e );
$ h = floor ( sqrt ( $ l ));
while ( $ l % $ h ! == 0 ) $ h - ;
$ w = $ l / $ h ;
// Split the whole mess one character at a time.
$ wordDot = str_split ( $ e , 1 );
$ line = 0 ;
$ column = 0 ;
$ image = imagecreate ( $ w , $ h );
$ black = imagecolorallocate ( $ image , 0 , 0 , 0 ); // Determine the black color
$ white = imagecolorallocate ( $ image , 255 , 255 , 255 ); //White
foreach ( $ wordDot as $ val )
{
// If 0, then we pick a white dot, and if 1 - black
if ( $ val == 0 ) imageline ( $ image , $ column , $ line , $ column , $ line , $ white );
else imageline ( $ image , $ column , $ line , $ column , $ line , $ black );
if ( $ column == $ w -1 ) { $ line ++ ; $ column = -1 ;}
$ column ++ ;
}
// Save the result
imagepng ( $ image , './' . $ fl . '.png' );
imagedestroy ( $ image );
echo "Input file:" ;
$ fly = readln ();
if ( file_exists ( $ fly )) $ image = imagecreatefrompng ( $ fly ); else die ( "File not found! \ n " );
// Calculate the width and height of the image
$ w = imagesx ( $ image );
$ h = imagesy ( $ image );
// B number of pixels in it (S = a * b)
$ wh = $ w * $ h ;
$ x = 0 ;
$ y = 0 ;
for ( $ t = 0 ; $ t < $ wh ; $ t ++ )
{
// Determine the color of the point.
$ rgb = imagecolorat ( $ image , $ x , $ y );
$ colors = imagecolorsforindex ( $ image , $ rgb );
$ cl = $ colors [ 'red' ] . $ colors [ 'green' ] . $ colors [ 'blue' ];
// If white, then set 0, and if black - 1
if ( $ cl == '255255255' ) $ binary . = '0' ;
if ( $ cl == '000' ) $ binary . = '1' ;
if ( $ x == $ w -1 ) { $ y ++ ; $ x = -1 ;}
$ x ++ ;
}
// Reverse and convert to text
$ dt = ascii_decrypt ( strrev ( $ binary ));
// Write to file
file_put_contents ( str_replace ( '.png' , '' , $ fly ), $ dt );
Source: https://habr.com/ru/post/116761/
All Articles