<?php

class TextToAscii
{
    private static $charsMap = array(
        ' ' => array(
            ' ',
            ' ',
            ' ',
            ' ',
            ' ',
        ),
        '0' => array(
            '###',
            '# #',
            '# #',
            '# #',
            '###',
        ),
        '1' => array(
            '## ',
            ' # ',
            ' # ',
            ' # ',
            '###',
        ),
        '2' => array(
            '###',
            '  #',
            '###',
            '#  ',
            '###',
        ),
        '3' => array(
            '###',
            '  #',
            '###',
            '  #',
            '###',
        ),
        '4' => array(
            '# #',
            '# #',
            '###',
            '  #',
            '  #',
        ),
        '5' => array(
            '###',
            '#  ',
            '###',
            '  #',
            '###',
        ),
        '6' => array(
            '###',
            '#  ',
            '###',
            '# #',
            '###',
        ),
        '7' => array(
            '###',
            '  #',
            '  #',
            '  #',
            '  #',
        ),
        '8' => array(
            '###',
            '# #',
            '###',
            '# #',
            '###',
        ),
        '9' => array(
            '###',
            '# #',
            '###',
            '  #',
            '###',
        ),
        '+' => array(
            '   ',
            ' # ',
            '###',
            ' # ',
            '   ',
        ),
        '-' => array(
            '   ',
            '   ',
            '###',
            '   ',
            '   ',
        ),
        '=' => array(
            '   ',
            '###',
            '   ',
            '###',
            '   ',
        ),
        '?' => array(
            '###',
            '  #',
            ' # ',
            '   ',
            ' # ',
        ),
    );
    
    private static $textChars = array('@', '#', '$', '&');

    public static function render($text)
    {
        $image = array(
            '',
            '',
            '',
            '',
            '',
            '',
        );
        
        foreach (str_split($text) as $char) {
            if (!isset(self::$charsMap[$char])) {
                trigger_error('This character is not supported: '. $char, E_USER_WARNING);
                return '/* CAPTCHA ERROR */';
            }
            
            $textChar =& self::$textChars[ array_rand(self::$textChars) ];
            
            $shift = mt_rand(0, 1);
            $emptyImageLine = str_repeat(' ', strlen(self::$charsMap[$char][0]));
            
            foreach ($image as $imageLineNumber => &$imageLine) {
                $lineNumber = $imageLineNumber-$shift;
                
                if (isset(self::$charsMap[$char][$lineNumber])) {
                    $imageLine .= str_replace(
                        '#',
                        $textChar,
                        self::$charsMap[$char][$lineNumber]
                    );
                } else {
                    $imageLine .= $emptyImageLine;
                }
            }
        }
        
        $image = implode("\n", $image);
        
        return $image;
    }
}

echo TextToAscii::render('3 + 7 - 4');