用PHP制做ASCII化的图像

用PHP代码,能够将gif格式的图像转换成具备ASCII字符风格化的png图像,颇有意思,和你们分享一下效果:php

原图:html

用PHP制做ASCII化的图像-行者无疆的图片

生成后效果
用PHP制做ASCII化的图像-行者无疆的图片code

源代码以下:htm

<?php  
$txt =array('A','B','C','D','E','F','G','H','I','J','K');
//加载原始图像
$rawImage = ImageCreateFromGIF('test.gif');
//获取原始图像宽高
$rawImgWidth = ImagesX($rawImage);
$rawImgHeigh = ImagesY($rawImage);
//获取原始图像灰度
$grayData = getGrayData($rawImage,$rawImgWidth,$rawImgHeigh);
//销毁图像
ImageDestroy($rawImage);
//建立文字图像
$txtImage = ImageCreate($rawImgWidth*6,$rawImgHeigh*9);
//新图像背景色
imagecolorallocate($txtImage,0,0,0);
//获取最大灰度
for($i=0;$i<count($grayData);$i++){
$maxGrayArray[$i] = max($grayData[$i]);
}
$maxGray = max($maxGrayArray);
//设置灰度对应颜色
for($i=0;$i<$maxGray+1;$i++){
$color = 255-round(200/$maxGray)*$i+55;
$gray[$i] = imagecolorallocate($txtImage,$color,$color,$color);
}
//绘制字符
for($y=0;$y<$rawImgHeigh;$y++){
for($x=0;$x<$rawImgWidth;$x++){
Imagechar($txtImage,1,$x*6,$y*9,$txt[rand(0,10)],$gray[$grayData[$x][$y]]);
}
}
//建立最终图像
$Image = ImageCreate($rawImgWidth*10,$rawImgHeigh*10);
//拉伸图像
imagecopyresampled($Image, $txtImage, 0, 0, 0, 0,$rawImgWidth*10,$rawImgHeigh*10,$rawImgWidth*6,$rawImgHeigh*9);
//定义文件头
header('Content-type: image/png');
//输出图像
ImagePNG($Image);
//销毁图像
ImageDestroy($Image);
/*
获取灰度值
*/
}

原文连接:http://www.ldsun.com/656.html 图片

演示地址:http://a.ldsun.com/get

相关文章
相关标签/搜索