Home    TrueColor    MyImage    About Allayers

If you are looking for a script that will put truetype text in a JPEG or PNG this is not for you! The free for download Truecolor script contains a simple but useful image text class that uses the GD library and truetype fonts.

TextPNG: text to image, image to text

Demo: pixel-based PHP/GD operations

This PHP scriptlet however performs a strange and maybe senseless trick.

It reads text contents from a file or an online page and hides that text in an image, a truecolor (24 bits) PNG (Portable Network Graphic).

So how does it work?
The numeric (ordinal) value of a single character varies between 0 and 255. As a matter of computer coincedence the same goes for the RGB (Red, Green, Blue) values of a single pixel in a truecolor image.
Saving the image to file and then reading it demands that colors do not get damaged by for instance JPEG compression.
PNG files reproduce exactly whatever text was put in. The script performs surprisingly well too.

The images are quite small and they could be used for some kind of data exchange. Do truecolor optical receptors exist? Some kind of encryption could be added. A simple gzcompress and a base64 encoding would make the images smaller yet. Also interesting is that optical patterns are sometimes formed by (apparently) repetitive parts in the source text.

The code can copied from the textarea below on this page or downloaded as zip.

It reads the content, writes it to a PNG file and then reads the file back to text again.

Samples: encoded HTML content of some main pages

Allayers.comGoogle.com2meta.comGetItDaily.comPHP.netHotscripts.com

TextPNG PHP Source

<?
// open online page for reading page
$handle = fopen("http://".$page,"r");

// read contents
$content = "";
do {
    
$data = fread($handle, 8192);
    if (
strlen($data) == 0) {
        break;
    }
    
$content .= $data;
} while(
true);
fclose ($handle);

// get string in array
$allchars = preg_split('//', $content, -1, PREG_SPLIT_NO_EMPTY);

// RGB = 3 chars per pixel. So size is approximately...
$size = ceil(sqrt( ceil(sizeof($allchars)/3) ));

// now create the image and assign ordinal value of chars to pixel RGB values
$im = imagecreatetruecolor($size,$size);
$ptr=0;
for(
$i=0;$i<$size;$i++){
   for(
$j=0;$j<$size;$j++){
     
$r = ord($allchars[$ptr]);
     
$g = ord($allchars[$ptr+1]);
     
$b = ord($allchars[$ptr+2]);
     
$col = imagecolorallocate ($im,$r,$g,$b);
     
imagesetpixel($im,$i,$j,$col);
     
$ptr += 3;
     if (
$ptr >= sizeof($allchars))break;
     }
   }

// save the image
$newname=date("ymdhms").".png";
imagepng($im,$newname);
imagedestroy($im);

// open png for reading again and save to string
$cm = imagecreatefrompng($newname);
for(
$i=0;$i<$size;$i++){
   for(
$j=0;$j<$size;$j++){
    
$col = imagecolorat($cm, $i, $j);
    
$rgb = imagecolorsforindex($cm, $col);
    
$newchars .= chr($rgb["red"]);
    
$newchars .= chr($rgb["green"]);
    
$newchars .= chr($rgb["blue"]);
   }
}

// $newchars is a text string and can be displayed...
// ...
?>


Another PHP samplet: Simple Meta Content Refresh Slideshow and Image Browser



Allayers - Copyright 2003 - 2004 ALLAYERS.COM