Image to Text to Image...

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Image to Text to Image...

Post by JAM »

Please solve my temporary blindness.
I'm trying to load an image and recreate it using text. I just made an img2ascii/html/text converter and I just have to do the other way around, for no particular reason. :lol:

But... The image is not displaying correctly. Mainly a dark rectangle, with a not so dark rectangle inside it. Below some code for you amusement. My first thought is that a char (here: ·) actually is larger than a pixel, but I'm getting hardly any colour output at all.
Not really sure this is a way to create an image...

Not to fond of image conversions, but if someone have input please do let me know.

Code: Select all

$cfg['ImagePath'] = './images/fav.jpg'; // img to use
    $image = @imagecreatefromjpeg($cfg['ImagePath']);
    $image = resizeimage($image, $cfg['Ascii']['Width']); // user function, depending on GD version
    $xmax = imagesx($image);
    $ymax = imagesy($image);
    $im = imagecreate($xmax, $ymax); // the new image
    $bcol = imagecolorallocate($im, 90, 90, 90);
    imagecolortransparent($im, $bcol);
    $font = imageloadfont('font.gdf'); // own bitmapfont

    for ($y = 0; $y < $ymax; $y++) { // loop original image Y
        for ($x = 0; $x < $xmax; $x++) { // loop original image X
            $colindex = imagecolorat($image, $x, $y); // get index
            $colrgb = imagecolorsforindex($image, $colindex); // get rgb
            $col = imagecolorallocate($im, $colrgb['red'], $colrgb['green'], $colrgb['blue']); // get color from rgb
            // the $x & $y here is a big questionmark.
            imagestring($im, $font, $x, $y, '·', $col); // output
        }
    }
Post Reply