前两天研究php中的 imagick 扩展的时候,突发奇想实现的一个小功能感觉挺有意思,在这里记录一下:
将一张image图片转化为字符串的形式,先上一张效果图。(运行笔记中的代码需要先安装 php_imagick 扩展 教程可以看这里:php扩展之 imagick安装)
,;+:,+:.
.flllllllllfflllllllllf.
.llllclllllllcl**llllllllllllll.
.lllllllllllllllcl..lllllllllllllllll.
+llllllllllllllclllllllllllllllllllllll*
tllllllllllllllllllll::l题目的好处lllllllllllllllllllt
.llllllllllllllllllllllllllllllllllllllllclll:
lclllllllllllllllllllll..lllllllllllllllllclllll
+ll.lllllllllllllllllllllllttlllllllllllllllllllllll.ll*
.lllcltlllllllllllllllllllllllllllllllllllllllllllllllltlllll,
fllllllll.fllllllllllllllllllllllllllllllllllllllllllllllf.llllllllf
lllllllllllf;lllllllllllllllllllllll::lllllllllllllllllllllll;tllcllllllll.
.llllllllllllll.lllllllllllllllllllllllttlllllllllllllllllllllll.llllllllllllll,
llllllllllllllll*,llllllllllllllllllllllffllllllllllllllllllllll:*lllllllllllllllc.
lllllllllllllllllllfcllllllllllllllllllllllllllllllllllllllcllllflllclllllllllllllll
:lcllllllllllllllllll:lllllllllllllllllllllllllllllllllllllllcllll,llllllllllllllllllll:
flllllllllllllllllllllfllllllllllllllllllllllllllllllllllllllllllflclllllllllllllllllllf
fllllllllllllllllllllllllcllllllllllllllllllllllllllllllllllllll.lllllllllllllllllllllllf
:lllllllllllllllllllllllc,.llllllllllllllllllllllllllllllllllllll.,llllllllllllllllllllllll:
lllllllllllllllllllllllllt.llllllllllllllllllllllllllllllllllll,*lllllllllllllllllllllllll
llllllllllllllllllllllllll.llllllllllllllllllllllllllllllllll.llllllllllllllllllllllllll
llllllllllllllllllllllllll.lllllllllllllllllllllllllllllllc.lllcllllllllllllllllllllll
+.,lllllllllllllllllllllllll,.llllllllllllllllllllllllllclll.lllllllllllllllllllllllll,,*
:llclf,lllllllllll公务员入职培训lllllllllllcl+llllllllllllllllllllllllllll;cllcllllllllllllllllllll,.fllll;
fllllllll+flllllllllllllllllllllltfllllllllllllllllllllllllf*lllllllllllllllllllllll+lllllllll
lllllllllllll,:llllllllllllcllllllllf+clllllllllllllllllllll+flllllllllllllclllllll:,lllllllllllll
llllllllllllllllt+llllllllclcllllcllll,lllllllllfflllllllll,llllllllllllllllllll+tllllllllllllllll
+lllllllllllllllllll:;llllllllllllllllll.llllllllttlllllllllllcllllllllllllll;;lllllllllllllllllll*
llllllllllllllllllllllf,llllllllllllllllfllllll;;lllllllllllllllllllllll,.flcllllllllllllllllllll
;llllllllllllllllllc海南岛图片llllll:.flllllllllclll+lllll.lclll+lllllllllllllf.:lllllllllllllllllllllllll;
+llllllllllllllllllcllllllllt.:lllllllllll.clllllll.lllllllllll:tlllllllllllllllllllllllllll*
,llllllllllllllllllllllllllllll,tclllllllllllllllllllllt,llllllllllllllllllllllllllllll:
flllllllllllllllllllllllllclllll;.flllll,:::lllllf.;lllclllllllllllllllllllllllllllf
fllllllllllllllllllllllllllllllllt,lllfll,tllllllllllll荷兰进口奶粉排行榜lllllllllllcllllllllf
,lllllllllllllllllllllllllllllllllffllllllllllllllllllllllllcllllllll,
:flllllllllllllllllllllcllllllllllfcllllllllllllllllllllllllllllllf:
....….….
,,:;++*ttffffflllllllllllll;:cllllllllllllfffftt**++::,.
fllllllllllllllllllllllllllclllll::lllllllllllllllllllllllllllllllll
+lllcllllllllllllllllllllllll;;llllclllllllllllllllllllllll+
flllllllllllllllllllllll::llllllllllllllllllllllll
flllllllllllcllllcl,,llllllllllllllllllf
.flllllllllll;;llllclllllll.
+ggg+fg0g,+ggg+lggg..ggggttg0g*l0gg;gggg;ggg0ggggg00ggl,gggt
+ggg+l0gg,+ggg+lggg.gg00gg+c0g0,+g0gggtg0g;;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:cgggtggg:;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;;;;;;;;*0ggllgg00g,lggggg,;0ggt,,,,,:,::,,gggt
+ggg+fggg,,gg0000gg0ggl.0ggc*g00t0ggg*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("请贫困申请书1500切合实际一点先安装 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; //根据像素的 "饱和度" 选择合适的字符 $offt=(int)ceil($r*(strlen($str)-1)); if($offt==(strlen($str)-1)){ echo ""; }el{ echo $str[$offt]; } } } $imageiterator->synciterator(); } }}img2string('huawei.jpg',2,1);
本文发布于:2023-04-07 03:52:17,感谢您对本站的认可!
本文链接:https://www.wtabcd.cn/fanwen/zuowen/c88fb8e58c0d008596fabd611f219287.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文word下载地址:PHP扩展之 Imagick安装.doc
本文 PDF 下载地址:PHP扩展之 Imagick安装.pdf
留言与评论(共有 0 条评论) |