问题描述
我有一个很大的ASCII文本,表示像ASCII-ART这样的位图。现在我在找一种类似倒置ASCII艺术生成器的东西。我喜欢将每个字符转换为彩色像素。
有没有可以做这种事情的免费工具?
推荐答案
我刚刚使用image-gd库编写了一个非常简朴的PHP脚本。它从textarea Formular中读取文本,并使用ASCII值和一些乘数函数为字符分配颜色,以使近邻ASCII(如&q;a&q;和&q;b&q;)之间的颜色差异可见。目前,它仅适用于已知文本大小。
<?php
if(isset($_POST['text'])){
//in my case known size of text is 204*204, add your own size here:
asciiToPng(204,204,$_POST['text']);
}else{
$out = "<form name ='textform' action='' method='post'>";
$out .= "<textarea type='textarea' cols='100' rows='100' name='text' value='' placeholder='Asciitext here'></textarea><br/>";
$out .= "<input type='submit' name='submit' value='create image'>";
$out .= "</form>";
echo $out;
}
function asciiToPng($image_width, $image_height, $text)
{
// first: lets type cast;
$image_width = (integer)$image_width;
$image_height = (integer)$image_height;
$text = (string)$text;
// create a image
$image = imagecreatetruecolor($image_width, $image_height);
$black = imagecolorallocate($image, 0, 0, 0);
$x = 0;
$y = 0;
for ($i = 0; $i < strlen($text)-1; $i++) {
//assign some more or less random colors, math functions are just to make a visible difference e.g. between "a" and "b"
$r = pow(ord($text{$i}),4) % 255;
$g = pow(ord($text{$i}),3) % 255;
$b = ord($text{$i})*2 % 255;
$color = ImageColorAllocate($image, $r, $g, $b);
//assign random color or predefined color to special chars ans draw pixel
if($text{$i}!='#'){
imagesetpixel($image, $x, $y, $color);
}else{
imagesetpixel($image, $x, $y, $black);
}
$x++;
if($text{$i}=="
"){
$x = 0;
$y++;
}
}
// show image, free memory
header('Content-type: image/png');
ImagePNG($image);
imagedestroy($image);
}
?>