Jump to content

Coding fun: Converting files into PNGs


Prismatic

Recommended Posts

So a while ago I ran across a script on a person's blog that would load a file in, any file, and convert it to a grayscale PNG, each pixel consisting of one ASCII value representation of a character from the loaded file.

 

I really wish I could remember where I found it but I cant, unfortunatly, to give credit for the original idea.

 

My plan was to take the file and read in 3 characters at a time, converting each character into an ASCII value and assigning that to the R, G and B fields of the pixel.

 

Here's our source photo next to the image generated from the original script, and finally after my modifications

 

anomymous.jpg -> sffn38.png -> Format.png

 

And the script,

 

<?php
$filename = "anomymous.jpg";

if (file_exists($filename))
{
    /** Load the file */
    $fs = fopen($filename, "r");
    $data = fread($fs, filesize($filename));
    fclose($fs);

    $i = 0;
    $d = 0;
    $key = 0;

    /**
    * Loop through and create an array with child arrays each containing
    * 3 entries, which will be our R, G and B values later on
    */
    $file = str_split($data);
    for ($y=0; $y < count($file); $y++)
    {
        /**
        * Place the 3 values inside our array,
        * We'll later be going back through this array to
        * create our image from the data inside it.
        */
        if($d <= 2)
        {
            $pixelData[$key][] = ord($data[$i]);
            $i++;
            $d++;
        }
        else
        {
            $key++;
            $d = 0;

            $pixelData[$key][] = ord($data[$i]);
            $i++;
            $d = 1;
        }
    }

    /**
    * Now we want to create the image, we'll use a modified pagination system
    * to split the array up into equal parts, except for the last row.. We'll pad
    * it out with black pixels
    */

    $rows = count($pixelData);
    $xlen = 100;
    $xlen_last = ceil($rows/$xlen);

    $im = imagecreatetruecolor($xlen, $xlen_last);
    $i = 0;

    for($y = 0; $y <= ceil($rows / $xlen); $y++)
    {
        for($x = 0; $x <= $xlen; $x++)
        {
            imagesetpixel($im, $x, $y, imagecolorallocate($im, $pixelData[$i][0], $pixelData[$i][1], $pixelData[$i][2]));
            $i++;
        }
    }

    if($_GET['debug'] == true)
    {
        print_r($pixelData);
    }
    else
    {
        header("Content-Type: image/png");
        imagepng($im);
        imagedestroy($im);
    }
}
?>

 

It's messy but works :)

 

 

What I do in my spare time :)

 

Link to comment
https://forums.phpfreaks.com/topic/134463-coding-fun-converting-files-into-pngs/
Share on other sites

I don't see what is happening, other than converting an image to noise...

 

it only looks like noise.

 

Each pixel of an image can hold 3 ASCII representations of a character (R, G and B channels)

 

The script loads in the target file, convert each character it encounters in the file into it's ASCII counterpart, and assign 3 at a time to a single pixel.

 

Here's a script to reconstruct a picture generated by the script in the OP.

 

There are some bugs which I think may be due to it not correctly translating some characters into ASCII.

 

<?php
$filename = "hw.png";

if (file_exists($filename))
{
    $img = imagecreatefrompng($filename);

    $img_y = imagesy($img);
    $img_x = imagesy($img);

    $i = 0;

    for($y = 0; $y <= $img_y; $y++)
    {
        for($x = 0; $x <= $img_x; $x++)
        {
            $rgb = @imagecolorat($img, $x, $y);

            $r = ($rgb >> 16) & 0xFF;
            $g = ($rgb >>  & 0xFF;
            $b = $rgb & 0xFF;

            echo chr($r).chr($g).chr($b);
        }
    }

    print_r($Reconstruct);
}
?>

 

Again it's not noise

So a while ago I ran across a script on a person's blog that would load a file in, any file, and convert it to a grayscale PNG, each pixel consisting of one ASCII value representation of a character from the loaded file.

 

I really wish I could remember where I found it but I cant, unfortunatly, to give credit for the original idea.

 

My plan was to take the file and read in 3 characters at a time, converting each character into an ASCII value and assigning that to the R, G and B fields of the pixel.

 

Here's our source photo next to the image generated from the original script, and finally after my modifications

 

anomymous.jpg -> sffn38.png -> Format.png

 

ZOMG HE'S ANONYMOUS

 

And the script,

 

<?php
$filename = "anomymous.jpg";

if (file_exists($filename))
{
    /** Load the file */
    $fs = fopen($filename, "r");
    $data = fread($fs, filesize($filename));
    fclose($fs);

    $i = 0;
    $d = 0;
    $key = 0;

    /**
    * Loop through and create an array with child arrays each containing
    * 3 entries, which will be our R, G and B values later on
    */
    $file = str_split($data);
    for ($y=0; $y < count($file); $y++)
    {
        /**
        * Place the 3 values inside our array,
        * We'll later be going back through this array to
        * create our image from the data inside it.
        */
        if($d <= 2)
        {
            $pixelData[$key][] = ord($data[$i]);
            $i++;
            $d++;
        }
        else
        {
            $key++;
            $d = 0;

            $pixelData[$key][] = ord($data[$i]);
            $i++;
            $d = 1;
        }
    }

    /**
    * Now we want to create the image, we'll use a modified pagination system
    * to split the array up into equal parts, except for the last row.. We'll pad
    * it out with black pixels
    */

    $rows = count($pixelData);
    $xlen = 100;
    $xlen_last = ceil($rows/$xlen);

    $im = imagecreatetruecolor($xlen, $xlen_last);
    $i = 0;

    for($y = 0; $y <= ceil($rows / $xlen); $y++)
    {
        for($x = 0; $x <= $xlen; $x++)
        {
            imagesetpixel($im, $x, $y, imagecolorallocate($im, $pixelData[$i][0], $pixelData[$i][1], $pixelData[$i][2]));
            $i++;
        }
    }

    if($_GET['debug'] == true)
    {
        print_r($pixelData);
    }
    else
    {
        header("Content-Type: image/png");
        imagepng($im);
        imagedestroy($im);
    }
}
?>

 

It's messy but works :)

 

 

What I do in my spare time :)

 

Me too, its right here:

http://phpsnips.com/snippet.php?id=29

 

Mine was a bit more in-depth.... 

 

- It accepted any image format that the gd library can handle. 

- You could have the image resized first

- You could specify x1,y1,x2,y2 coords to asciify a target area.

- It "weighed" the pixel color and matched it to a certain ascii character, to give it more depth.  For instance, '#' has more of a presence than '.' So '#' was used for darker colors and '.' was used for lighter colors.  My ascii palette consisted of like 10 'shades.' or something.

- You could specify how many ascii characters used to render

- Render it in color or black and white (black and white ones looked better the more ascii chars you used)

- Option to save the output to a file

 

I think there was 1 or 2 other features but it's been a while.  Like, I know I was working on user being able to enter in their own ascii chars (to make your own palette), but I'm not sure if I finished that or not.  I'll have to dig around for it.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.