Created

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist

Damn Simple PHP Ascii Art Generator

View ascii.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
#!/usr/bin/php -q
<?php
if(isset($argv[1]) && strlen($argv[1])) {
$file = $argv[1];
}else{
echo 'Please Specify a File';
exit(1);
}
$img = imagecreatefromstring(file_get_contents($file));
list($width, $height) = getimagesize($file);
$scale = 10;
$chars = array(
' ', '\'', '.', ':',
'|', 'T', 'X', '0',
'#',
);
$chars = array_reverse($chars);
$c_count = count($chars);
for($y = 0; $y <= $height - $scale - 1; $y += $scale) {
for($x = 0; $x <= $width - ($scale / 2) - 1; $x += ($scale / 2)) {
$rgb = imagecolorat($img, $x, $y);
$r = (($rgb >> 16) & 0xFF);
$g = (($rgb >> 8) & 0xFF);
$b = ($rgb & 0xFF);
$sat = ($r + $g + $b) / (255 * 3);
echo $chars[ (int)( $sat * ($c_count - 1) ) ];
}
echo PHP_EOL;
}

Can't you replace:

$chars = array(
    ' ', '\'', '.', ':',
    '|', 'T',  'X', '0',
    '#',
);

$chars = array_reverse($chars);

$c_count = count($chars);

with:

$chars = "#0XT|:.' ";

$c_count = strlen($chars);

I'm not understanding why the array_reverse() is being used as you can declare the array in reverse to begin with. Also the chars do not need to be in an array as a string is already indexed. $chars[1] for instance will grab the 2nd char from a string. Other than that, awesome script! =o)

Owner

Its declared as an array rather than a string to allow me to use multibyte characters more easily, I'm not here but I had experimented with it.

As for the array_reverse, its simply so its easier for my brain to wrap it self around, starting at lightest and going to darkest. That is all.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.