Created
May 10, 2012 20:32
-
-
Save oranj/2655697 to your computer and use it in GitHub Desktop.
Ascii art generator w/ color
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/php | |
<?php | |
$yscale = 11; | |
$xscale = 5; | |
$target_width = get_tty_width(); | |
$inverted = false; | |
$flags = Array(); | |
$flag_parameters = Array(); | |
$in_parameters = Array(); | |
$params = Array(); | |
for ($i = 1; $i < $argc; $i++) { | |
if (preg_match('/^\-([a-z]+)$/', $argv[$i], $matches)) { | |
for ($j = 0; $j < strlen($matches[1]); $j++) { | |
$letter =$matches[1][$j]; | |
switch ($letter ){ | |
case 'c': | |
$flags['cartoon'] = true; | |
#$flag_parameters[] = "cartoon"; | |
break; | |
case 'o': | |
$flags['open'] = true; | |
} | |
} | |
} else { | |
$in_parameters []= $argv[$i]; | |
} | |
} | |
$flag_parameters [] = 'url'; | |
foreach ($flag_parameters as $i => $param) { | |
if (! isset($in_parameters[$i])) { | |
echo "Parameter Mismatch\n"; | |
exit; | |
} | |
$params[$param] = $in_parameters[$i]; | |
} | |
if (! isset($params['url'])) { | |
echo "Please include a URL.\n"; | |
die (1); | |
} | |
$url = $params['url']; | |
if (preg_match('/imgur\.com\/([^\.]+)$/i', $url)) { | |
$url = $url.'.jpg'; | |
} | |
if (! preg_match('/([a-z]{3,5}):\/\/(.*)/i', $url)) { | |
$url = 'http://'.$url; | |
} | |
$image = file_get_contents($url); | |
if (! $image) { | |
echo "Could not fetch image $url\n"; | |
die(1); | |
} | |
if (@ $flags['open']) { | |
system('open '.$url); | |
} | |
$img = imagecreatefromstring($image); | |
#imagefilter($img, IMG_FILTER_EDGEDETECT); | |
$image_width = imagesx($img); | |
$image_height = imagesy($img); | |
$ascii_w_h_scale = $xscale / $yscale; | |
$img_scale = $image_width/$image_height; | |
$target_height = ($target_width * $ascii_w_h_scale) / ($img_scale); | |
$ascii_x_scale = $image_width / $target_width; | |
$ascii_y_scale = $image_height / $target_height; | |
function brightness_from_index($rgb, &$_r, &$_g, &$_b) { | |
$r = (($rgb >> 16) & 0xFF); | |
$g = (($rgb >> 8) & 0xFF); | |
$b = (($rgb) & 0xFF); | |
$_r += $r; | |
$_g += $b; | |
$_b += $b; | |
$r /= 256; | |
$g /= 256; | |
$b /= 256; | |
#echo "$r $g $b\n"; | |
$out = sqrt((0.241*$r*$r) + (0.691*$g*$g) + (0.068*$b*$b)); | |
#echo "$r $g $b $out\n"; | |
return $out; | |
} | |
function get_tty_width() { | |
$output = exec('tput cols'); | |
return $output; | |
} | |
function get_ascii_pixel($img, $x0, $x1, $y0, $y1, $inverted) { | |
$total = 0; | |
$count = 0; | |
$r = $g = $b = 0; | |
for ($x = round($x0); $x < round($x1); $x++) { | |
for ($y = round($y0); $y < round($y1); $y++) { | |
$total += brightness_from_index(imagecolorat($img, $x, $y), $r, $g, $b); | |
$count++; | |
} | |
} | |
$r /= $count; | |
$g /= $count; | |
$b /= $count; | |
# print_r(Array($r, $g, $b)); | |
$colors = Array( | |
'a000a0' => '0;35', // purple | |
'0000cc' => '0;34', // blue | |
'00ff00' => '0;32', // green | |
'ff0000' => '0;31', // red | |
'ffffff' => '1;37', // white | |
'cccccc' => '0;37', // light gray | |
//'888888' => '1;30', // dark gray | |
'ffff00' => '1;33', // yellow | |
'000000' => '0;30', // black | |
); | |
$diff = INF; | |
$out_color = '1;37'; // white | |
foreach ($colors as $color => $symbol) { | |
$cr = hexdec(substr($color, 0, 2)); | |
$cg = hexdec(substr($color, 2, 2)); | |
$cb = hexdec(substr($color, 4, 2)); | |
$this_diff = sqrt(pow($r - $cr, 2) + | |
pow($g - $cg, 2) + | |
pow($b - $cb, 2)); | |
if ($this_diff < $diff) { | |
$diff = $this_diff; | |
$out_color = $symbol; | |
} | |
} | |
$average = (float)$total / (float)$count; | |
if (! $inverted) { | |
// $average = 1 - $average; | |
} | |
static $pixels = Array( | |
'0.9' => '#', | |
'0.8' => '%', | |
'0.7' => 'T', | |
'0.6' => '=', | |
'0.5' => '+', | |
'0.4' => '~', | |
'0.3' => '-', | |
'0.2' => ',', | |
'0.1' => '.', | |
'0' => ' ', | |
); | |
foreach ($pixels as $prob => $symbol) { | |
if ($average >= $prob) { | |
return "\033[" . $out_color . "m". $symbol. "\033[0m"; | |
} | |
} | |
} | |
for ($y = 0; $y < $target_height - 1; $y++) { | |
for ($x = 0; $x < $target_width - 1; $x++) { | |
echo get_ascii_pixel($img, $x * $ascii_x_scale, ($x+1) * $ascii_x_scale, $y * $ascii_y_scale, ($y + 1) * $ascii_y_scale, $inverted); | |
} | |
echo "\n"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment