Jump to content

php image to ascii problems (alpha -> green)


Username:

Recommended Posts

I made a small project last night that converts an image into text (with color etc). When the image is transparent, it spits out something like this.

 

http://i.imgur.com/PEET5.png

 

sometimes images look like this, even without a transparent background:

 

http://i.imgur.com/WbEOF.png

 

 

Can you help me? :)

 

Here's my code

 

function imgToAscii($img) {
$ext = substr(strrchr($img, '.'), 1);
if($ext == "jpg" || $ext == "jpeg") { 
	$imgh	= imagecreatefromjpeg($img); 
} elseif($ext == "png") { 
	$imgh	= imagecreatefrompng($img);
} elseif($ext == "gif") { 
	$imgh	= imagecreatefromgif($img);
} else { 
	die("<font color='white'>Unknown file format!</font>"); 
}

$w	= imagesx($imgh);
$h	= imagesy($imgh);

for($y=0; $y<$h; $y++) {
       		for($x=0; $x<=$w; $x++){ 
		$rgb = imagecolorat($imgh, $x, $y);
		$r = ($rgb >> 16) & 0xFF;
		$g = ($rgb >>  & 0xFF;
		$b = $rgb & 0xFF;
		$hex = "#".dechex($r).dechex($g).dechex($b);
		if($x == $w) { 
			$art .= "<br>"; 
		} else { 
			$art .= "<font style='font-size:1px;line-height:1;letter-spacing:-1px;size:1px;font:fixedsys;' color='".$hex."'>@</font>"; 
		}  
	}
}		
return $art;	
}

 

I tried adding an alpha value, but it made regular images worse and no change on transparent ones.

Link to comment
Share on other sites

You're making a font 1px that isn't meant to be 1px. Things will look ugly :(

 

As for PNG, check what kind of values are returned when you use imagecolorat() on a transparent pixel.

 

I dunno, lemme look.

I adjusted some things (changed from using hex codes to raw RGB format color: rgb($r, $g, $b)) and now not ALL images are green, just some images (like the Google logo) have discoloration on it's text.

 

It actually looks almost perfect if I set the font to 1px, any higher looks stretched etc. I'm using @ signs so it's able to be round, not just square, the picture just looks a little blurry after.

 

Do you want decimal values or hex values?

 

Both?

Link to comment
Share on other sites

This is for you, not for me.

 

Alpha levels are hard to translate into ASCII, sadly. I would almost create a box with the same color as the background, drop the PNG on top of it, and solve from there. Transparency is no longer an issue.

Link to comment
Share on other sites

This is for you, not for me.

 

Alpha levels are hard to translate into ASCII, sadly. I would almost create a box with the same color as the background, drop the PNG on top of it, and solve from there. Transparency is no longer an issue.

 

Why is that? Transparency in fonts etc is supported natively by CSS.

 

Could I just check for the color it sends out and tell it to put color: transparent; instead?

 

Oh, and after I changed it to RGB(), transparency makes the image have a blue tint instead.

 

And, transparency is this:

color: rgb(0, 0, 251);

Link to comment
Share on other sites

 

I tried adding an alpha channel to my script, but it just made it appear worse. :(

 

just read the rest of that script, I get it now :)

$alpha = (imagecolorat($im,$x,$y) & 0x7F000000) >> 24;
                //DITHER!
                if ($alpha > 3 && (
                        $alpha >=127-3 ||
                        (rand(0,127))>=(127-$alpha)
                    )){
                    imagesetpixel($im,$x,$y,$transparentColor); // <----------- that
                }

 

edit:

 

I added it, with no change. Did I do it wrong? :(

<?php
function imgToAscii($img, $letter) {
echo "
<style type='text/css'>
.ascii { 
	font-smooth:never;
	font-size:1px;
	line-height:1;
	letter-spacing:-1px;
	font:fixedsys;
}
</style>";
$ext = substr(strrchr($img, '.'), 1);
if($ext == "jpg" || $ext == "jpeg") { 
	$imgh	= imagecreatefromjpeg($img); 
} elseif($ext == "png") { 
	$imgh	= imagecreatefrompng($img);
} elseif($ext == "gif") { 
	$imgh	= imagecreatefromgif($img);
} else { 
	die("<font color='white'>Unknown file format!</font>"); 
}
$trans = imagecolorallocate($imgh, 0xfe, 0x3, 0xf4 );
$w	= imagesx($imgh);
$h	= imagesy($imgh);
for($y=0; $y<$h; $y++) {
       		for($x=0; $x<=$w; $x++){ 
		$rgb = imagecolorat($imgh, $x, $y);
		$r = ($rgb >> 16) & 0xFF;
  			$g = ($rgb >> 8 ) & 0xFF;
  			$b = $rgb & 0xFF;
		$alpha = (imagecolorat($imgh,$x,$y) & 0x7F000000) >> 24;
                	if ($alpha > 3 && ($alpha >=127-3 || (rand(0,127))>=(127-$alpha))){
                    		imagesetpixel($imgh,$x,$y,$trans);
               		}

		if($x == $w) { 
			$art .= "<br>"; 
		} else { 
			$art .= "<span class='ascii' style='color: rgb($r, $g, $b);'>$letter</span>"; 
		}  
	}
}	
return $art;	
}
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.