<?php

/**
 * OCR
 *
 * @author Alix Axel
 */
class OCR
{
    private 
$im;

    function 
Color($x$y)
    {
        if (!
is_resource($this->im))
        {
            return 
false;
        }

        
extract(imagecolorsforindex($this->imimagecolorat($this->im$x$y)));

        
$red base_convert($red1016);

        if (
strlen($red) < 2)
        {
            
$red '0' $red;
        }

        
$green base_convert($green1016);

        if (
strlen($green) < 2)
        {
            
$green '0' $green;
        }

        
$blue base_convert($blue1016);

        if (
strlen($blue) < 2)
        {
            
$blue '0' $blue;
        }

        return 
strtoupper($red $green $blue);
    }

    function 
Info($image$flag null)
    {
        list(
$width$height$type) = getimagesize($image);

        if (!empty(
$flag))
        {
            
$flag strtolower(trim($flag));

            if (
$flag == 'width')
            {
                return 
$width;
            }

            else if (
$flag == 'height')
            {
                return 
$height;
            }

            else if (
$flag == 'type')
            {
                switch (
$type)
                {
                    case 
1:
                        return 
'gif';
                    break;

                    case 
2:
                        return 
'jpg';
                    break;

                    case 
3:
                        return 
'png';
                    break;
                }
            }

            return 
false;
        }

        switch (
$type)
        {
            case 
1:
                
$type 'gif';
            break;

            case 
2:
                
$type 'jpg';
            break;

            case 
3:
                
$type 'png';
            break;
        }

        
$result = array
        (
            
'width' => $width,
            
'height' => $height,
            
'type' => $type
        
);

        return 
$result;
    }

    function 
Read($tag$image$background_vector 'FFFFFF')
    {
        
$image_info $this->Info($image);

        
$tag strtolower($tag);

        if (!
is_dir('OCR/' $tag '/'))
        {
            return 
false;
        }

        
$characters = array();

        
$handle opendir('OCR/' $tag '/patterns/');

        while ((
$file readdir($handle)) !== false)
        {
            if ((
$file != '.') && ($file != '..'))
            {
                if (!
is_dir('OCR/' $tag '/patterns/' $file))
                {
                    
$file str_replace('.ocr'''$file);

                    
$characters[$file] = $file;
                }
            }
        }

        
closedir($handle);

        
$ignore_hash '';

        if (!empty(
$background_vector))
        {
            if (
strlen($background_vector) == 6)
            {
                
$ignore_hash md5(str_repeat($background_vector$image_info['height']));
            }

            else
            {
                
$ignore_hash md5($background_vector);
            }
        }

        if (
$image_info['type'] == 'gif')
        {
            
$this->im imagecreatefromgif($image);
        }

        else if (
$image_info['type'] == 'jpg')
        {
            
$this->im imagecreatefromjpeg($image);
        }

        else if (
$image_info['type'] == 'png')
        {
            
$this->im imagecreatefrompng($image);
        }

        
$test = array();
        
$column 0;
        
$candidates = array();
        
$result '';

        for (
$i 0$i $image_info['width']; $i++)
        {
            
$test[$column] = '';

            for (
$j 0$j $image_info['height']; $j++)
            {
                
$test[$column] .= $this->Color($i$j);
            }

            
$test[$column] = md5($test[$column]);

            if (
$test[$column] == $ignore_hash)
            {
                
$test = array();
                
$column 0;
            }

            else
            {
                if (empty(
$candidates[$i $column]))
                {
                    foreach (
$characters as $character)
                    {
                        
$pattern unserialize(file_get_contents('OCR/' $tag '/patterns/' $character '.ocr'));

                        if (
$test[$column] == $pattern[$column])
                        {
                            
$candidates[$i $column][] = $character;
                        }
                    }
                }

                else
                {
                    foreach (
$candidates[$i $column] as $character)
                    {
                        
$pattern unserialize(file_get_contents('OCR/' $tag '/patterns/' $character '.ocr'));

                        if (
$test[$column] != $pattern[$column])
                        {
                            
$key array_search($character$candidates[$i $column]);

                            unset(
$candidates[$i $column][$key]);
                        }
                    }
                }

                if (
count($candidates[$i $column]) == 1)
                {
                    
$candidates[$i $column] = array_values($candidates[$i $column]);
                    
$result .= $candidates[$i $column][0];

                    
$character_width $this->Info('OCR/' $tag '/source/' $candidates[$i $column][0] . '.' $image_info['type'], 'width');

                    
$i $i $column $character_width 1;
                    
$column 0;
                }

                else
                {
                    
$column++;
                }
            }
        }

        return 
$result;
    }

    function 
Train($tag$character$image)
    {
        if (!
file_exists($image))
        {
            return 
false;
        }

        else
        {
            
$image_info $this->Info($image);
        }

        
$tag strtolower($tag);

        if (!
is_dir('OCR/' $tag '/'))
        {
            
mkdir('OCR/' $tag '/');
        }

        if (!
is_dir('OCR/' $tag '/source/'))
        {
            
mkdir('OCR/' $tag '/source/');
        }

        if (
file_exists('OCR/' $tag '/source/' $character '.' $image_info['type']))
        {
            @
unlink('OCR/' $tag '/source/' $character '.' $image_info['type']);
        }

        @
copy($image'OCR/' $tag '/source/' $character '.' $image_info['type']);

        
$image 'OCR/' $tag '/source/' $character '.' $image_info['type'];

        if (
$image_info['type'] == 'gif')
        {
            
$this->im imagecreatefromgif($image);
        }

        else if (
$image_info['type'] == 'jpg')
        {
            
$this->im imagecreatefromjpeg($image);
        }

        else if (
$image_info['type'] == 'png')
        {
            
$this->im imagecreatefrompng($image);
        }

        
$result = array();

        for (
$i 0$i $image_info['width']; $i++)
        {
            
$result[$i] = '';

            for (
$j 0$j $image_info['height']; $j++)
            {
                
$result[$i] .= $this->Color($i$j);
            }

            
$result[$i] = md5($result[$i]);
        }

        if (!
is_dir('OCR/' $tag '/patterns/'))
        {
            
mkdir('OCR/' $tag '/patterns/');
        }

        if (
file_exists('OCR/' $tag '/patterns/' $character '.ocr'))
        {
            @
unlink('OCR/' $tag '/patterns/' $character '.ocr');
        }

        
$handle = @fopen('OCR/' $tag '/patterns/' $character '.ocr''wb');

        
flock($handleLOCK_EX);
        
fwrite($handleserialize($result));
        
flock($handleLOCK_UN);
        
fclose($handle);
    }
}

highlight_file(__FILE__);

?>