Functions
WebDev/*nix assorted functions & hacks
Search
find all
Array
-
Bash
-
Classes
-
Config
-
DateTime
-
Debug
-
dotjs
-
File
-
foooo
-
Git
-
HTML
-
Images
-
Javascript
-
Linux
-
Math
-
Misc
-
MySQL
-
OS X
-
Reading
-
Redis
-
Shell scripts
-
snippets
-
SQL
-
Strings
-
TextMate
-
Web
-
\(^o^)/ Fun!
-
_Misc hacks
-
_Pages
\(^o^)/ Fun!
* Readme
Alphanumeric images to ascii
Conway's Game of Life as TextMate command
Langton's Ant
Quine
Sierpiński Carpet
Sierpiński Triangle
Towers of Hanoi
8
found
auth
Name:
Category: \(^o^)/ Fun!
Body:
Writes characters to an image resource, and for each character, saves a .txt file with a '1' for black, and a ' ' for white pixels, so 'a.txt' becomes 1111 11 1 111 11 1 1 1 1 11 111 1 <?php $im = imagecreatetruecolor(10, 16); $font = '/Library/Fonts/Andale Mono.ttf'; # Create some colors $white = imagecolorallocate($im, 255, 255, 255); $black = imagecolorallocate($im, 0, 0, 1); $red = imagecolorallocate($im, 255, 0, 0); $blue = imagecolorallocate($im, 0, 0, 255); imagefill($im, 0, 0, $white); $out = ''; foreach(str_split(' abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789') as $char) { # apparently using negative colours makes them non-anti-aliased (aliased?) which is easier for drawing our ascii imagefttext($im, 12, 0, 0, 12, -$black, $font, $char); # prevent 'A.txt' overwriting 'a.txt' $filename = (! is_numeric($char) && (strtoupper($char) == $char)) ? $char.$char : $char; for($i=0;$i<16;$i++) { for($j=0;$j<10;$j++) { $x = imagecolorat($im, $j, $i); $out .= ($x == $black) ? '1' : ' '; } $out .= "\n"; } file_put_contents("images/chars2/$filename.txt", $out); $out = ''; imagefilledrectangle($im, 0, 0, 10, 16, $white); } imagedestroy($im); ?> And then, you can write a lil' commandline thing that takes text and ascify's it, like this: #!/usr/bin/php <?php array_shift($argv); echo aski(join(" ", $argv)); while ($foo = trim(fgets(STDIN))) { if (in_array($foo, array('quit', 'q', 'exit', 'bye'))) { echo "Bye!\n"; die(); } echo aski($foo); } function aski($string, $wrap=16) { $alphabet = 'abcdefghijklmnopqrstuvwxyz'; $chars = str_split($string); $outlines = 0; $colcount = 1; foreach($chars as $char) { # if it's an uppercase char, grab the double-char filename $charfile = (stristr($alphabet, $char) && ($char == strtoupper($char))) ? "$char$char.txt" : "$char.txt"; if (! file_exists($charfile)) $charfile = 'xx.txt'; $data = trim(file_get_contents($charfile), "\n"); if (! isset($height)) $height = count(explode("\n", $data)); foreach(explode("\n", $data) as $key => $line) { $out[$key+($outlines*$height)] .= $line; } if ($colcount++ >= $wrap) { $outlines++; $colcount = 1; } } $out = join("\n", $out)."\n"; $out = str_replace(1, '#', $out); return $out; } ?> nemo$ ./aski blaat # ### # # # # # # # ### # #### #### ###### ## # # ## ## # # # # # # # # # # ### ### # # # # ## # ## # # # ## # # # # # # ## # # # ## # ## # # ### ### ### # ### # #### (yes, I was bored.)
Footer