用PHP制作ASCII化的图像

本文介绍使用PHP将GIF格式的图像转换为具有ASCII字符风格化的PNG图像的方法,并提供了实现代码及转换前后效果对比。

用PHP代码,可以将gif格式的图像转换成具有ASCII字符风格化的png图像,很有意思,和大家分享一下效果:

原图:

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

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

源代码如下:

<?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/

转载于:https://my.oschina.net/u/1472492/blog/209543

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值