Prismatic Posted November 27, 2008 Share Posted November 27, 2008 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 -> -> 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 Quote Link to comment https://forums.phpfreaks.com/topic/134463-coding-fun-converting-files-into-pngs/ Share on other sites More sharing options...
The Little Guy Posted November 27, 2008 Share Posted November 27, 2008 I don't see what is happening, other than converting an image to noise... Quote Link to comment https://forums.phpfreaks.com/topic/134463-coding-fun-converting-files-into-pngs/#findComment-700147 Share on other sites More sharing options...
Prismatic Posted November 27, 2008 Author Share Posted November 27, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/134463-coding-fun-converting-files-into-pngs/#findComment-700166 Share on other sites More sharing options...
corbin Posted November 27, 2008 Share Posted November 27, 2008 Hrmmm entirely pointless, but interesting I guess. Quote Link to comment https://forums.phpfreaks.com/topic/134463-coding-fun-converting-files-into-pngs/#findComment-700187 Share on other sites More sharing options...
The Little Guy Posted November 27, 2008 Share Posted November 27, 2008 Hrmmm entirely pointless, but interesting I guess. Guess that is why I'm confused ??? Quote Link to comment https://forums.phpfreaks.com/topic/134463-coding-fun-converting-files-into-pngs/#findComment-700195 Share on other sites More sharing options...
waynew Posted November 27, 2008 Share Posted November 27, 2008 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 -> -> 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 Quote Link to comment https://forums.phpfreaks.com/topic/134463-coding-fun-converting-files-into-pngs/#findComment-700283 Share on other sites More sharing options...
.josh Posted November 27, 2008 Share Posted November 27, 2008 I wrote a script a while back that turned a picture into ascii art. Not really a difficult thing to do, but it's novel. Quote Link to comment https://forums.phpfreaks.com/topic/134463-coding-fun-converting-files-into-pngs/#findComment-700361 Share on other sites More sharing options...
The Little Guy Posted November 28, 2008 Share Posted November 28, 2008 Me too, its right here: http://phpsnips.com/snippet.php?id=29 Quote Link to comment https://forums.phpfreaks.com/topic/134463-coding-fun-converting-files-into-pngs/#findComment-700795 Share on other sites More sharing options...
.josh Posted November 28, 2008 Share Posted November 28, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/134463-coding-fun-converting-files-into-pngs/#findComment-700959 Share on other sites More sharing options...
corbin Posted November 29, 2008 Share Posted November 29, 2008 "asciify" Should be the word of the day. Quote Link to comment https://forums.phpfreaks.com/topic/134463-coding-fun-converting-files-into-pngs/#findComment-701517 Share on other sites More sharing options...
The Little Guy Posted November 29, 2008 Share Posted November 29, 2008 Old: http://ftp.pcworld.com/pub/screencams/ascii-matrix.html Quote Link to comment https://forums.phpfreaks.com/topic/134463-coding-fun-converting-files-into-pngs/#findComment-701586 Share on other sites More sharing options...
The Little Guy Posted November 29, 2008 Share Posted November 29, 2008 Want to see some high quality text? HD Text: http://dudeel.com/test.php?url=http://www.myclassiclyrics.com/artist_biographies/Spider-Man_biography_1.jpg Origianl image: http://www.myclassiclyrics.com/artist_biographies/Spider-Man_biography_1.jpg Quote Link to comment https://forums.phpfreaks.com/topic/134463-coding-fun-converting-files-into-pngs/#findComment-701603 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.