国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發(fā) > PHP > 正文

PHP 實(shí)現(xiàn)的將圖片轉(zhuǎn)換為TXT

2024-05-04 23:39:41
字體:
供稿:網(wǎng)友

今天在用PHP寫一個(gè)小插件的時(shí)候,遇到了一個(gè)小小的問題,就是需要將圖片轉(zhuǎn)換為TXT文本的內(nèi)容。簡單的說就是將圖片轉(zhuǎn)換為ASCII碼,下面把代碼分享給大家。

PHP 實(shí)現(xiàn)的將圖片轉(zhuǎn)換為TXT

 

 
  1. <?php 
  2. /* 
  3. 2015年10月19日10:24:59 
  4.  
  5. */ 
  6. // 打開一幅圖像 
  7.  
  8. $file_name='d:/ascii_dora.png'
  9. $chars = "$@B%8&WM#*oahkbdpqwmZO0QLCJUYXzcvunxrjft//|()1{}[]?-_+~<>i!lI;:,/"^`'. "
  10. function getimgchars($color_tran,$chars){ 
  11. $length = strlen($chars); 
  12. $alpha=$color_tran['alpha']; 
  13. $r=$color_tran['red']; 
  14. $g=$color_tran['green']; 
  15. $b=$color_tran['blue']; 
  16. $gray = intval(0.2126 * $r + 0.7152 * $g + 0.0722 * $b); 
  17.  
  18. if($gray==0){ 
  19. return '.'
  20.  
  21. if($gray<196){ 
  22. $unit = (256.0 + 1)/$length; 
  23. return $chars[intval($gray/$unit)]; 
  24.  
  25. return " "
  26.  
  27.  
  28. function color_img($color_tran,$chars){ 
  29. $length = strlen($chars); 
  30. $alpha=$color_tran['alpha']; 
  31.  
  32. $r=$color_tran['red']; 
  33. $g=$color_tran['green']; 
  34. $b=$color_tran['blue']; 
  35. $gray = intval(0.2126 * $r + 0.7152 * $g + 0.0722 * $b); 
  36. $rand=rand (0, $length-1); 
  37. $color="rgb(".$r.",".$g.",".$b.")"
  38. $char=$chars[$rand]; 
  39. return '<span style="color:'.$color.'" >'.$char."</span>";; 
  40.  
  41.  
  42. function resize_img($file_name,$chars,$flage=true){ 
  43. //header('Content-Type: image/jpeg'); 
  44. list($width, $height,$type) = getimagesize($file_name); 
  45. $fun='imagecreatefrom' . image_type_to_extension($type, false); 
  46. if($type==3){ 
  47. $flage=false
  48. $fun($file_name); 
  49. $new_height =100; 
  50. $percent=$height/$new_height; 
  51. $new_width=$width/$percent; 
  52. $image_p = imagecreatetruecolor($new_width, $new_height); 
  53. $image = $fun($file_name); 
  54. imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height); 
  55. if($flage){ 
  56. return $image_p; 
  57. }else
  58. return $image; 
  59.  
  60.  
  61. $im=resize_img($file_name,$chars); 
  62.  
  63. $width=imagesx($im); 
  64. $height=imagesy($im); 
  65.  
  66. $back_text=""
  67.  
  68. for($i=1;$i<=$height;$i++){ 
  69. for($j=1;$j<=$width;$j++){ 
  70. $color_index = imagecolorat($im, $j-1, $i-1); 
  71. $color_tran = imagecolorsforindex($im, $color_index); 
  72. $back_text.=color_img($color_tran,$chars,false); 
  73. $back_text.="<br/>"
  74.  
  75. echo "<pre>"
  76. echo $back_text; 
  77. echo "</pre>"
  78. //file_put_contents('1.txt',$back_text); 

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 丹东市| 阳朔县| 句容市| 南和县| 和田市| 普宁市| 平山县| 贵港市| 万源市| 龙里县| 会同县| 武夷山市| 临汾市| 枞阳县| 开封县| 屏南县| 吉木乃县| 乌兰察布市| 崇文区| 玛曲县| 宜城市| 禄劝| 元朗区| 平远县| 都安| 静乐县| 开远市| 冀州市| 两当县| 昌平区| 克东县| 嘉兴市| 札达县| 紫云| 保亭| 登封市| 城步| 普陀区| 枣阳市| 金堂县| 广平县|