(1)函数名称和参数定义
Function asciifyImage($img,$asciiscale,$asciicolor,
$asciialpha,$asciiblock,$asciiinvert,$asciiresolution,
$asciichars){
......
}
(2)一些变量的定义和初始化
asciiresolution,$asciichars){
$strChars = ""; // 字符画字符串
$strFont = "courier new";
// 把用来替代像素的字符集合分割到数组中。
$aDefaultCharList = str_split(" .,:;i1tfLCG08@");
$aDefaultColorCharList = str_split(" CGO08@");
$iScale = $asciiscale?$asciiscale:1;
$bColor = $asciicolor; // 颜色
$bAlpha = $asciialpha;
$bBlock = $asciiblock; // 灰度块
$bInvert = $asciiinvert;
$strResolution = $asciiresolution?$asciiresolution:"medium";
// 转化尺寸类型
$aCharList = $asciichars?$asciichars:($bColor ?
$aDefaultColorCharList : $aDefaultCharList);
$fResolution = 0.5;
switch ($strResolution) {
case "low" : $fResolution = 0.25; break;
case "medium" : $fResolution = 0.5; break;
case "high" : $fResolution = 1; break;
}
(3)实现字符画
$im = imagecreatefrompng($img); // 打开图片
// 得到宽度和高度
$iWidth = ceil(imagesx($im) * $fResolution);
$iHeight = ceil(imagesy($im) * $fResolution);
for($y=0;$y<$iHeight;$y+=2){
for($x=0;$x<$iWidth;$x++){
// 添加颜色
$color_index =
imagecolorsforindex($im,imagecolorat($im,
ceil($x/$fResolution), ceil($y/$fResolution)));
$iRed = $color_index['red'];
$iGreen = $color_index['green'];
$iBlue = $color_index['blue'];
$iAlpha = $color_index['alpha'];