Convert JPG to RGB colored ASCII


/ Published in: PHP
Save to your folder(s)



Copy this code and paste it in your HTML
  1. <html>
  2. <head>
  3. <title>Ascii</title>
  4. <style>
  5. body{
  6. line-height:1px;
  7. font-size:1px;
  8. }
  9. </style>
  10. </head>
  11. <body>
  12. <?php
  13. function getext($filename) {
  14. $pos = strrpos($filename,'.');
  15. $str = substr($filename, $pos);
  16. return $str;
  17. }
  18.  
  19. $image = 'image.jpg';
  20. $ext = getext($image);
  21. if($ext == ".jpg"){
  22. $img = ImageCreateFromJpeg($image);
  23. }
  24. else{
  25. echo'Wrong File Type';
  26. }
  27. $width = imagesx($img);
  28. $height = imagesy($img);
  29.  
  30. for($h=0;$h<$height;$h++){
  31. for($w=0;$w<=$width;$w++){
  32. $rgb = ImageColorAt($img, $w, $h);
  33. $r = ($rgb >> 16) & 0xFF;
  34. $g = ($rgb >> 8) & 0xFF;
  35. $b = $rgb & 0xFF;
  36. if($w == $width){
  37. echo '<br>';
  38. }else{
  39. echo '<span style="color:rgb('.$r.','.$g.','.$b.');">X</span>';
  40. }
  41. }
  42. }
  43.  
  44. $output = ob_get_contents();
  45.  
  46. // file_output_contents("test.html");
  47.  
  48. echo $output;
  49. ?>

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.