PHP 实现的将图片转换为TXT

2017-11-19编程教程
196

<?php
/*
2015年10月19日10:24:59
 
*/
// 打开一幅图像
 
$file_name='d:\ascii_dora.png';
$chars = "$@B%8&WM#*oahkbdpqwmZO0QLCJUYXzcvunxrjft/\|()1{}[]?-_+~<>i!lI;:,\"^`'. ";
function getimgchars($color_tran,$chars){
  $length = strlen($chars);
  $alpha=$color_tran['alpha'];
  $r=$color_tran['red'];
  $g=$color_tran['green'];
  $b=$color_tran['blue'];
  $gray = intval(0.2126 * $r + 0.7152 * $g + 0.0722 * $b);
 
  if($gray==0){
    return '.';
  }
 
  if($gray<196){
     $unit = (256.0 + 1)/$length;
    return $chars[intval($gray/$unit)];
  }
 
  return " ";
 
}
 
function color_img($color_tran,$chars){
  $length = strlen($chars);
  $alpha=$color_tran['alpha'];
 
  $r=$color_tran['red'];
  $g=$color_tran['green'];
  $b=$color_tran['blue'];
  $gray = intval(0.2126 * $r + 0.7152 * $g + 0.0722 * $b);
  $rand=rand (0, $length-1);
  $color="rgb(".$r.",".$g.",".$b.")";
  $char=$chars[$rand];
  return '<span style="color:'.$color.'" >'.$char."</span>";;
   
}
 
function resize_img($file_name,$chars,$flage=true){
  //header('Content-Type: image/jpeg');
  list($width, $height,$type) = getimagesize($file_name);
  $fun='imagecreatefrom' . image_type_to_extension($type, false);
  if($type==3){
    $flage=false;
  }
  $fun($file_name);
  $new_height =100;
  $percent=$height/$new_height;
  $new_width=$width/$percent;
  $image_p = imagecreatetruecolor($new_width, $new_height);
  $image = $fun($file_name);
  imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
  if($flage){
    return $image_p;
  }else{
    return $image;
  }
 
}
 
$im=resize_img($file_name,$chars);
 
$width=imagesx($im);
$height=imagesy($im);
 
$back_text="";
 
for($i=1;$i<=$height;$i++){
  for($j=1;$j<=$width;$j++){
    $color_index = imagecolorat($im, $j-1, $i-1);
    $color_tran = imagecolorsforindex($im, $color_index);
    $back_text.=color_img($color_tran,$chars,false);
  }
  $back_text.="<br/>";
}
  
echo "<pre>";
echo $back_text;
echo "</pre>";
//file_put_contents('1.txt',$back_text);

The End

相关推荐

layui根据百度地图经纬度在弹出层中显示位置
首先你需要引入百度地图的js script type="text/javascript" src="https://api.map.baidu.com/api?v=2.0ak=你的ak"/script ak ,注意是要浏览器端的ak,这个直接到百度开发者平台申请。 引入layui,这个大家可以到layui官网看看怎么引入layer, 注意:这里要提...
2025-01-09 编程教程
240

C#之socket编程
编程需要恒心和毅力,最主要的是要有信心,循序渐进的完成任务。 一、socket类用于网络通信 命名空间System.Net.Sockets,完整的类引用System.Net.Sockets.Socket。Socket类支持各种网络协议。 二、简单的控制台程序 using System;using System.Collections....
2023-03-07 编程教程
114

百度UEditor编辑器如何禁止过滤div等网页html标签
将设计排版好的页面html代码上传到数据库,再读取出来的时候发现所有的div都被替换成了p标签。 解决方法: 首先在ueditor.all.js文件内搜索allowDivTransToP,找到如下的代码,将true设置为false me.setOpt({ 'allowDivTransToP':false, 'disabledTableInTable'...
2022-11-23 编程教程
495

Ajax中文传值出现乱码的解决办法
Ajax技术的核心为Javascript,而javascript使用的是UTF-8编码,因此在页面采用GBK或者其他编码,同时没有进行编码转换时,就会出现中文乱码的问题。 以下是分别使用GET和POST方式传值,并且页面采用GBK和UTF-8编码在IE和FF下的不同测试结果和出现乱码时的解...
2022-11-19 编程教程
364

PHP错误Warning: Cannot modify header information - headers alr
今天在用php进行图片保存输出时候,图片一直显示错误,后面用调试模式下提示:Warning: Cannot modify header information - headers already sent by... 看了一些网上的方法也没解决,最后在php.ini配置output_buffering默认为4096就没有遇到这个错误了: o...
2022-05-18 编程教程
69

Parse error: syntax error, unexpected '&'解决办法
在使用PHP5.4及以上版本时,在调用函数时,使用引用符号时,会出现Parse error: syntax error, unexpected 或PHP Fatal error: Call-time pass-by-reference has been removed者,这是由于在函数调用时通过引用传递参数已被弃用,因为它影响了代码的整洁,如...
2022-05-11 编程教程
295