PHP 实现的将图片转换为TXT


Posted in PHP onOctober 21, 2015

PHP 实现的将图片转换为TXT

<?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);
PHP 相关文章推荐
PHP 和 MySQL 基础教程(三)
Oct 09 PHP
图书管理程序(二)
Oct 09 PHP
php中使用explode查找某个字符是否存在的方法
Jul 12 PHP
PHP中常用的字符串格式化函数总结
Nov 19 PHP
yii2中添加验证码的实现方法
Jan 09 PHP
基于Swoole实现PHP与websocket聊天室
Aug 03 PHP
PHP new static 和 new self详解
Feb 19 PHP
PHP实现留言板功能的详细代码
Mar 25 PHP
PHP使用xpath解析XML的方法详解
May 20 PHP
PHP实现找出链表中环的入口节点
Jan 16 PHP
用Laravel Sms实现laravel短信验证码的发送的实现
Nov 29 PHP
php设计模式之适配器模式实例分析【星际争霸游戏案例】
Apr 07 PHP
PHP实现清除wordpress里恶意代码
Oct 21 #PHP
表单提交错误后返回内容消失问题的解决方法(PHP网站)
Oct 20 #PHP
php的mail函数发送UTF-8编码中文邮件时标题乱码的解决办法
Oct 20 #PHP
PHP页面转UTF-8中文编码乱码的解决办法
Oct 20 #PHP
php限制文件下载速度的代码
Oct 20 #PHP
php获取远程文件大小
Oct 20 #PHP
php根据日期或时间戳获取星座信息和生肖等信息
Oct 20 #PHP
You might like
PHP 得到根目录的 __FILE__ 常量
2008/07/23 PHP
解析Linux下Varnish缓存的配置优化
2013/06/20 PHP
ThinkPHP分页实例
2014/10/15 PHP
详解PHP中foreach的用法和实例
2016/10/25 PHP
Laravel Intervention/image图片处理扩展包的安装、使用与可能遇到的坑详解
2017/11/14 PHP
PHP7修改的函数
2021/03/09 PHP
JSON.parse 解析字符串出错的解决方法
2010/07/08 Javascript
纯js实现瀑布流展现照片(自动适应窗口大小)
2013/04/08 Javascript
js将控件隐藏的方法及display属性介绍
2013/07/04 Javascript
JavaScript学习笔记之Function对象
2015/01/22 Javascript
Nodejs中的this详解
2016/03/26 NodeJs
javascript实现不同颜色Tab标签切换效果
2016/04/27 Javascript
gulp-uglify 与gulp.watch()配合使用时报错(重复压缩问题)
2016/08/24 Javascript
原生js实现旋转木马轮播图效果
2017/02/27 Javascript
Node.js中 __dirname 的使用介绍
2017/06/19 Javascript
JavaScript基于activexobject连接远程数据库SQL Server 2014的方法
2017/07/12 Javascript
JS 60秒后重新发送验证码的实例讲解
2017/07/26 Javascript
vue用Object.defineProperty手写一个简单的双向绑定的示例
2018/07/09 Javascript
vue定义全局变量和全局方法的方法示例
2018/08/01 Javascript
微信小程序 MinUI组件库系列之badge徽章组件示例
2018/08/20 Javascript
js 数组当前行添加数据方法详解
2020/07/28 Javascript
vue实现tab栏点击高亮效果
2020/08/19 Javascript
jquery实现拖拽小方块效果
2020/12/10 jQuery
[03:40]DOTA2英雄梦之声_第01期_炼金术士
2014/06/23 DOTA
python使用range函数计算一组数和的方法
2015/05/07 Python
python迭代器与生成器详解
2016/03/10 Python
Python按钮的响应事件详解
2019/03/04 Python
python 函数嵌套及多函数共同运行知识点讲解
2020/03/03 Python
Pyside2中嵌入Matplotlib的绘图的实现
2021/02/22 Python
科颜氏加拿大官方网站: Kiehl’s加拿大
2016/08/16 全球购物
美国最大的网络男装服装品牌:Bonobos
2017/05/25 全球购物
学校政风行风自查自纠报告
2014/10/21 职场文书
离婚财产分隔协议书
2014/10/23 职场文书
第二批党的群众路线教育实践活动总结报告
2014/10/30 职场文书
2016年六一文艺汇演开幕词
2016/03/04 职场文书
win10蓝屏0xc0000001安全模式进不了怎么办?win10出现0xc0000001的解决方法
2022/08/05 数码科技