前两天研究php中的 imagick 扩展的时候,突发奇想实现的一个小功能感觉挺有意思,在这里记录一下:
将一张image图片转化为字符串的形式,先上一张效果图。(运行笔记中的代码需要先安装 php_imagick 扩展 教程可以看这里:PHP扩展之 Imagick安装)
;: ,: .LLLLL LLLLL. LLCLLLC* *LLLLLLL LLLLLLLLL LLLLLLLLL +LLLLLLLLL LLLLLLLLL* LLLLLLLLLL: :LLLLLLLLLL .LLLLLLLLLLL LLLLLLLLLCL: LLLLLLLLLLLL LLLLLLLLLLLL L. LLLLLLLLLLLL LLLLLLLLLLLL .L LLL LLLLLLLLLLLL LLLLLLLLLLLL LLL fLLLL fLLLLLLLLLLL LLLLLLLLLLLf LLLLf LLLLLf ;LLLLLLLLLLL: :LLLLLLLLLLL; tLLLLL. LLLLLLL. LLLLLLLLLLLt tLLLLLLLLLLL .LLLLLLL LLLLLLLL LLLLLLLLLLLf fLLLLLLLLLLL LLLLLLLL. LLLLLLLLLL fLLLLLLLLLLL LLLLLLLLLLLf LLLLLLLLLL LLLLLLLLLL: LLLLLLLLLLL LLLLLLLLLLL ,LLLLLLLLLL LLLLLLLLLLL LLLLLLLLLLL LLLLLLLLLLL LLLLLLLLLLL LLLLLLLLLLLL CLLLLLLLLL LLLLLLLLLL. LLLLLLLLLLLL LLLLLLLLLLLL, LLLLLLLLLL LLLLLLLLLL ,LLLLLLLLLLLL LLLLLLLLLLLLL .LLLLLLLLL LLLLLLLLL, LLLLLLLLLLLLL LLLLLLLLLLLLL LLLLLLLLL LLLLLLLLL LLLLLLLLLLLLL LLLLLLLLLLLLL. LLLLLLLL LLLLLLLL. LCLLLLLLLLLLL + LLLLLLLLLLLLL LLLLLLLL LLLLLLLL LLLLLLLLLLLLL * LCf LLLLLLLLLLLC+ LLLLLLL LLLLLLL ;LCLLLLLLLLLL fLL LLLL+ fLLLLLLLLLLL fLLLLLL LLLLLLf LLLLLLLLLLLL +LLLL LLLLLL, :LLLLLLLLLLf CLLLLL LLLLLL fLLLLLLCLLL: ,LLLLLL LLLLLLLL LLLLCCLLLL ,LLLLf fLLLL, LLLLLLLLLL LLLLLLLL LLLLLLLLLL ;LLLLLLLLL LLLLt tLLLL LLLLLLLLL; LLLLLLLLLL LLLLLLLLLLLf LLLLLLLL LLL; ;LLL LLLLLLLL fCLLLLLLLLLL ;LLLLLLLLLLLL: .LLLLLLL +LL. CL+ LLLLLLL. :LLLLLLLLLLLL; LLLLLLLLLCLLLL :LLLLL CL LL LLLLL: LLLLLLLLLLLLLL ,LLLLLLLLLLLLLLL tLLLL L L LLLLt LLLLLLLLLLLLLLL: fLLLLLLLLLLLLCLL; fLL , : LLf ;LCLLLLLLLLLLLLLf fLLLLLLLLLLLLLLLL LL fL LLLLLLLLLLLLLLLLf ,LLLLLLLLLLLLLLLLf fLLLLLLLLLLLLLLLL, :LLLLLLLLLLLLLLLL CLLLLLLLLLLLLLLL: . . . . .. ,:+*tffLLLLLLL CLLLLLLfft*+:. fLLLLLLLLLLLLLLLL LLLLLLLLLLLLLLLLL LLLLLLLLLLLLLL; ;LLLLLLLLLLLLLL LLLLLLLLLLLL LLLLLLLLLLLL LLLLLLLLC, ,LLLLLLLLL fLLLLL; ;LLLLLL GG f0, GG LG. GGt GG 0G GG ;G0GG0GL GG GG LG, GG LG. G0G CG, +0G t0; ;GLffff* GG GG fG, GG LG. CGf0; ,GC GGGt 0G ;G* GG GG....LG, GG LG. G0 G0 GG GGGG GG ;Gf+++ GG GG00G0G0, GG LG. t0: CG. CG.+G:CG tG: ;GGGGG GG GG****CG, GG CG. G0,,:GG .GL0G ,0tGG ;0*... GG GG LG, GG GG ;0G0GG0G 00GC GGGC ;G+ GG GG fG, *0G:,CG0 GG;;;;*GL LG0, LGG, ;Gt,,::, GG GG fG, G00G0G .GC G0 GG GG ;GGGG0GG GG ,, ,, ,,. .. .. .. .. ....... ..
原图:
<?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){ echo "\n"; foreach ($pixels as $column => $pixel) { if($column%$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('fo.jpg',5,3);