php 如何将image图片转化为字符串
2019-06-13 09:04:08来源:博客园 阅读 ()
前两天研究php中的 imagick 扩展的时候,突发奇想实现的一个小功能感觉挺有意思,在这里记录一下:
将一张image图片转化为字符串的形式,先上一张效果图。(运行笔记中的代码需要先安装 php_imagick 扩展 教程可以看这里:PHP扩展之 Imagick安装)
,;+: ,+:.
.fLLLLLLLLLf fLLLLLLLLLf.
.LLLLCLLLLLLLCL* *LLLLLLLLLLLLLL.
.LLLLLLLLLLLLLLLCL. .LLLLLLLLLLLLLLLLL.
+LLLLLLLLLLLLLLCLLLL LLLLLLLLLLLLLLLLLLL*
tLLLLLLLLLLLLLLLLLLLL: :LLLLLLLLLLLLLLLLLLLLt
.LLLLLLLLLLLLLLLLLLLLLL LLLLLLLLLLLLLLLLLLCLLL:
LCLLLLLLLLLLLLLLLLLLLLL. .LLLLLLLLLLLLLLLLLCLLLLL
+LL. LLLLLLLLLLLLLLLLLLLLLLLt tLLLLLLLLLLLLLLLLLLLLLLL .LL*
.LLLCLt LLLLLLLLLLLLLLLLLLLLLLLL LLLLLLLLLLLLLLLLLLLLLLLL tLLLLL,
fLLLLLLLL. fLLLLLLLLLLLLLLLLLLLLLLL LLLLLLLLLLLLLLLLLLLLLLLf .LLLLLLLLf
LLLLLLLLLLLf ;LLLLLLLLLLLLLLLLLLLLLLL: :LLLLLLLLLLLLLLLLLLLLLLL; tLLCLLLLLLLL.
.LLLLLLLLLLLLLL. LLLLLLLLLLLLLLLLLLLLLLLt tLLLLLLLLLLLLLLLLLLLLLLL .LLLLLLLLLLLLLL,
LLLLLLLLLLLLLLLL* ,LLLLLLLLLLLLLLLLLLLLLLf fLLLLLLLLLLLLLLLLLLLLLL: *LLLLLLLLLLLLLLLC.
LLLLLLLLLLLLLLLLLLL fCLLLLLLLLLLLLLLLLLLLLL LLLLLLLLLLLLLLLLLCLLLLf LLLCLLLLLLLLLLLLLLL
:LCLLLLLLLLLLLLLLLLLL: LLLLLLLLLLLLLLLLLLLLLL LLLLLLLL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LLLLLLLLLLLLLLLLLLLLL: :LLLLLLLLLLLLLLLLLLLLLLLL
fLLLLLLLLLLLCLLLLCL, ,LLLLLLLLLLLLLLLLLLf
.fLLLLLLLLLLL; ;LLLLCLLLLLLL.
+GGG+ fG0G, +GGG+ LGGG. .GGGGt tG0G* L0GG; GGGG ;GGG0GGGGG00GGL ,GGGt
+GGG+ L0GG, +GGG+ LGGG. GG00GG+ C0G0, +G0GGG tG0G; ;GGGLtffffffff* ,GGGt
+GGG* fGGG, +GGG+ LGGG. CGGGfG0G; ,GGGC .GGG0GGt :0GGf ;GGG* ,GGGt
+GGG*........LGGG, +GGG+ LGGG. LGG0. LGG0, *GGG+ LGGGtGGG: GGGG ;GGGf++++++, :GGGt
+GGGG0G0GGG0GGG0G, +GGG+ LGG0. tG0G: CGGG. C0GG. +0GG: CGGG tGGG: ;GG0GGGGG0G* ,GGGt
+GGGL********C0GG, +GGG* CGG0. *GG0*,,,,:GGGG .GGGL.0GGt ,G0Gt,GGGt ;G0G*...... ,GGGt
+GGG* LGGG, ,GGGG :GGGC ;G0GGG0GGGGG0GGC +0G0GGGC +GGGGG0C ;0GG+ ,GGGt
+GGG+ fGGG, *G0GGf:.,;CGGG0, :GGGL;;;;;;;;*0GGL LGG00G, LGGGGG, ;0GGt,,,,,:,::, ,GGGt
+GGG+ fGGG, ,GG0000GG0GGL .0GGC *G00t 0GGG* 0G0G+ ;GGGGGGGGG0GGGG. ,G0Gt
.,,,. ,,,: .,:,,. .... .... ... ... .............. ...
原图:
<?php /** * 图片转换为字符图 * @param string $imagePath 图片地址 * @param int $width x轴方向缩放比例 * @param int $height y轴方向缩放比例 * @return string 生成的字符串图 */ function img2String($imagePath,$width=1,$height=1) { //检查是否安装 php_imagick 扩展 if(!extension_loaded("imagick")){ exit("请先安装 imagick 模块"); } $imagick = new \Imagick(realpath($imagePath)); //将生成的imagick对象转化为一个像素迭代器,可以从该迭代器中获取没个像素的 rgb 的值。不知道什么是rgb的请问度娘 $imageIterator = new \ImagickPixelIterator($imagick); $str='O80GCLft*+;:,. '; foreach ($imageIterator as $rows => $pixels) { if($rows%$width == 1 || $width == 1){ echo "\n"; foreach ($pixels as $column => $pixel) { if($column%$height == 1 || $height == 1){ //灰度计算公式 某个像素点的灰度值 = 红色值*0.3 + 绿色值 * 0.59 + 蓝色值 * 0.11 $gray = $pixel->getColor()['r']*0.3 + $pixel->getColor()['g']*0.59 + $pixel->getColor()['b']*0.11; //由于 rgb 三原色的数值范围在 [0,255] 所以求出的灰度值的范围也是 [0,255],所以我们计算像素的 "饱和度" = 灰度值/255; $r = $gray/255; //根据像素的 "饱和度" 选择合适的字符 $offset=(int)ceil($r*(strlen($str)-1)); if($offset==(strlen($str)-1)){ echo " "; }else{ echo $str[$offset]; } } } $imageIterator->syncIterator(); } } } img2String('huawei.jpg',2,1);
原文链接:https://www.cnblogs.com/itsuibi/p/10997258.html
如有疑问请与原作者联系
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
下一篇:【php设计模式】装饰器模式
- PHP写UltraEdit插件脚本实现方法 2020-03-29
- php 带逗号千位符数字的处理方法 2020-03-28
- PHP三元运算符的结合性介绍 2020-03-28
- PHP静态延迟绑定和普通静态效率的对比 2020-03-28
- 基于php流程控制语句和循环控制语句 2020-03-28
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash