SnippetsProject Code

JPEG to ASCII Converter

Description

Take a JPG image, and convert it to ASCII code!

Snippet

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
<html>  
<head>  
<title>Ascii</title>  
<style>  
body{  
	line-height:1px;  
	font-size:1px;  
}  
</style>  
</head>  
<body>  
<?php  
function getext($filename) {  
	$pos strrpos($filename,'.');  
	$str substr($filename$pos);  
	return $str;  
}  
$image 'image.jpg'$ext getext($image);  
if($ext == ".jpg"){  
	$img ImageCreateFromJpeg($image);  
}  
else{  
	echo'Wrong File Type';  
}  
$width imagesx($img);  
$height imagesy($img);  

for($h=0;$h<$height;$h++){  
	for($w=0;$w<=$width;$w++){  
		$rgb ImageColorAt($img$w$h);  
		$r = ($rgb >> 16) & 0xFF;  
		$g = ($rgb >> 8) & 0xFF;  
		$b $rgb 0xFF;  
		if($w == $width){  
			echo '<br>';  
		}else{  
		   echo '<span style="color:rgb('.$r.','.$g.','.$b.');">#</span>'; 
		}  
	}  
}  
?>
</body>
</html>

Comments

DISPLAY NAME:

WEBSITE URL (HTTP:// IS OPTIONAL):

ADD YOUR COMMENT:

Image Code

Enter the code from above:

If you don't want to see these images any more Register an account and get more features.

Formatting

Highest Rated Comment.
Stephen September 2nd 2007
Wow! I can't believe such complex output can be achieved by such simple code!!
Other Comments.
PHPKode September 13th 2010 (Website)
well. nice script. thank you.
Coding for Everyone January 3rd 2011 (Website)
Thanks for the script. I am going to use it on my website..
Travis June 20th 2007
I'm not sure what to say about this..... This crazy! I thought it would be just black and white, but I saw a color and the detail! Awesome!
650-296 June 9th 2011 (Website)
Woah. This creates high-quality ASCII images. I\'m worried if it will use too much bandwidth or not.
värdering hus  June 2nd 2011 (Website)
Its so much interesting
jaktfilmer June 2nd 2011 (Website)
keep it up!!!!!!!!!!!!!!!1
kreditkort June 2nd 2011 (Website)
I like it so much
vir0e5 March 14th 2011 (Website)
nice code sir!!
xahoi December 16th 2010 (Website)
Awesome... thank for share
expertise online December 8th 2010 (Website)
Nice code, I've seen a couple sites using this.
php tutorial November 28th 2010 (Website)
Nice code!
Kent UK July 17th 2010 (Website)
Hi I wondered if anyone can help, I need the same function for Gif, BMP and PNG images. This code worked but I need the images in different formats now thanks Marget
CasTex November 23rd 2009 (Website)
Jpg is sometimes gives errors. Thats why I prefer png.
craig July 23rd 2009
I keep getting a parse error for line 53. Is there a ; missing somewhere?
ed June 4th 2009
Interesting, too bad it does not work though: when pressing the 'Create' button. Th epost method calls for

which is an unknown url
ed June 4th 2009
That would be: [[?echo $_SERVER['PHP_SELF'];?]] behind the URl and that is reported as unknown
ed June 4th 2009
That would be 'fishhook open'?echo $_SERVER['PHP_SELF'];?fishhook closed behind teh url and that is an unknown url
Anonymous June 4th 2009
apparently php seems to have problems with the double 'greater than signs' in the lines as:
$r = ($rgb >> 16) & 0xFF;, which makes it think the php code already stops
ed June 4th 2009
Solved. Needed to give it a php extension of course, not an html
Anonymous May 9th 2009
seriously? you whine about people "taking credit" for this? wow...
Mondrak March 23rd 2009 (Website)
I create confusion in the 2 previous comments. To see what I meant see at where I put a living demo.

Regards
Link for Mondrak March 23rd 2009 (Website)
Ach ! the link was stripped ! Click on Mondrak link.
Sorry
Mondrak March 21st 2009 (Website)
Very nice code even though I've a couple of suggestions:
First: instead of "JPEG img URL: " that is showed at 1 px per char I suggest to put the following code:

Second: change the input type in "file" so you get a browse button as well.


That's it !
Thanks again
Mondrak March 21st 2009 (Website)
The code was filtered: substitute [ with &lt; and ] with &gt;

[input type="submit" value=" JPEG img URL " disabled]
Tyranic-Moron December 9th 2008 (Website)
I think Guyblade means the echo 'br';, which is true. Don't need the if either, then.

Nice code otherwise!
Dan December 8th 2008
Awesome....truly awesome. I cannot believe the results, just amazing. Thanks!
petemayo December 8th 2008 (Website)
Very nice!
I have been thinking about doing a script like this forever :)
Great for smaller files.
Too bad it takes so long to render, otherwise could do some cool javascript enlarging / reducing the image.
I bet CSS 3 could do some cool stuff with this as well, doing gradual color fades etc.
guyblade December 8th 2008
Please move the "echo '';" outside the inner for loop. You don't need to do w*h integer comparisons when you know that it will always be the right time to place one after the innter loop completes.

Login



Did you want to add your own codes?
Create an Account its fast and easy!

Did you forget your username/password?
Have us reset it it for you!

Information

3.22 / 5 (427 votes)

40,950 views

40 downloads New

Code Tools

Bookmark and Share

Programmer

admin

Posted on: Jan 01, 2008

View all of Ryan's snips