首页 > 开发 > PHP > 正文

php实现图片转换成ASCII码的方法

2024-05-04 23:33:54
字体:大 中 小
来源:转载
供稿:网友

这篇文章主要介绍了php实现图片转换成ASCII码的方法,涉及php操作图片的技巧,需要的朋友可以参考下

本文实例讲述了php实现图片转换成ASCII码的方法。分享给大家供大家参考。具体如下:

php图片转换成ASCII码,转换后可以直接通过字符串显示图片

 

 
  1. <html> 
  2. <head> 
  3. <title>Ascii</title> 
  4. <style> 
  5. body{ 
  6. line-height:0; 
  7. font-size:1px; 
  8. } 
  9. </style> 
  10. </head> 
  11. <body> 
  12. <?php 
  13. $image = 'image.jpg'; 
  14. // Supports http if allow_url_fopen is enabled 
  15. $image = file_get_contents($image); 
  16. $img = imagecreatefromstring($image); 
  17. $width = imagesx($img); 
  18. $height = imagesy($img); 
  19. for($h=0;$h<$height;$h++){ 
  20. for($w=0;$w<=$width;$w++){ 
  21. $rgb = imagecolorat($img, $w, $h); 
  22. $a = ($rgb >> 24) & 0xFF; 
  23. $r = ($rgb >> 16) & 0xFF; 
  24. $g = ($rgb >> 8) & 0xFF; 
  25. $b = $rgb & 0xFF; 
  26. $a = abs(($a / 127) - 1); 
  27. if($w == $width){ 
  28. echo '<br>'; 
  29. }else{ 
  30. echo '<span style="color:rgba('.$r.','.$g.','.$b.','.$a.');">#</span>'; 
  31. } 
  32. } 
  33. } 
  34. ?> 
  35. </body> 
  36. </html> 

希望本文所述对大家的php程序设计有所帮助。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表