Username: Posted July 27, 2011 Share Posted July 27, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/243005-php-image-to-ascii-problems-alpha-green/ Share on other sites More sharing options...
Username: Posted July 28, 2011 Author Share Posted July 28, 2011 bump Quote Link to comment https://forums.phpfreaks.com/topic/243005-php-image-to-ascii-problems-alpha-green/#findComment-1248670 Share on other sites More sharing options...
xyph Posted July 28, 2011 Share Posted July 28, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/243005-php-image-to-ascii-problems-alpha-green/#findComment-1248673 Share on other sites More sharing options...
Username: Posted July 28, 2011 Author Share Posted July 28, 2011 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? Quote Link to comment https://forums.phpfreaks.com/topic/243005-php-image-to-ascii-problems-alpha-green/#findComment-1248679 Share on other sites More sharing options...
xyph Posted July 28, 2011 Share Posted July 28, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/243005-php-image-to-ascii-problems-alpha-green/#findComment-1248681 Share on other sites More sharing options...
Username: Posted July 28, 2011 Author Share Posted July 28, 2011 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); Quote Link to comment https://forums.phpfreaks.com/topic/243005-php-image-to-ascii-problems-alpha-green/#findComment-1248682 Share on other sites More sharing options...
xyph Posted July 28, 2011 Share Posted July 28, 2011 Check this function out here http://www.php.net/manual/en/function.imagecolorat.php#79116 Quote Link to comment https://forums.phpfreaks.com/topic/243005-php-image-to-ascii-problems-alpha-green/#findComment-1248684 Share on other sites More sharing options...
Username: Posted July 28, 2011 Author Share Posted July 28, 2011 Check this function out here http://www.php.net/manual/en/function.imagecolorat.php#79116 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; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/243005-php-image-to-ascii-problems-alpha-green/#findComment-1248685 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.