/ ASCII图

PHP实现图片转ASCII图

原本是一个基于html5 canvas的JS实现,但是考虑到并不是所有浏览器都支持,所以就花了点时间改写成PHP,功能基本上没差别。

JS实现的链接:http://www.nihilogic.dk/labs/jsascii/

下面是PHP代码:

" . $strThisChar . ""; } else { $strChars .= $strThisChar; } } $strChars .= "
"; } $fFontSize = (2/$fResolution)*$iScale; $fLineHeight = (2/$fResolution)*$iScale; $fLetterSpacing = 0; if ($strResolution == "low") { switch ($iScale) { case 1 : $fLetterSpacing = -1; break; case 2 : case 3 : $fLetterSpacing = -2.1; break; case 4 : $fLetterSpacing = -3.1; break; case 5 : $fLetterSpacing = -4.15; break; } } if ($strResolution == "medium") { switch ($iScale) { case 1 : $fLetterSpacing = 0; break; case 2 : $fLetterSpacing = -1; break; case 3 : $fLetterSpacing = -1.04; break; case 4 : case 5 : $fLetterSpacing = -2.1; break; } } if ($strResolution == "high") { switch ($iScale) { case 1 : case 2 : $fLetterSpacing = 0; break; case 3 : case 4 : case 5 : $fLetterSpacing = -1; break; } } $width = ceil($iWidth/$fResolution)*$iScale; $height = ceil($iHeight/$fResolution)*$iScale; $style = "display:inline;width:$width px;height:$height px;white-space:pre;margin:0px;padding:0px;font:$strFont"; $style .= "letter-spacing:$fLetterSpacing px;font-size:$fFontSize px;text-align:left;text-decoration:none"; echo "
$strChars
"; } asciifyImage('microbe.png',3,false,0,false,false,"medium",null); ?>

效果图点击链接查看http://www.ostools.net/jsbin/mjsqdnmo/1

顺便写了个在线生成的工具http://asciiimage.duapp.com

PHP实现图片转ASCII图
Share this