SnippetsProject Code

JPG 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
45
46
47
48
49
50
51
52
53
<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;
}
if(!isset($_POST['submit'])){
?>
<form action="<?echo $_SERVER['PHP_SELF'];?>" method="post">
	JPG img URL: <input type="text" name="image"><br>
	<input type="submit" name="submit" value="Create">
</form>
<?
}else{
	$image $_POST['image'];
	$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

ADD YOUR COMMENT:

Formatting

Comments.
Kent UK July 17th (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.
Stephen September 2nd 2007
Wow! I can't believe such complex output can be achieved by such simple code!!
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!

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 (408 votes)

28,107 views

1 downloads New

Code Tools

Bookmark and Share

Programmer

admin

Posted on: Jan 01, 2008

View all of Ryan's snips