porting https://github.com/BlueHerons/StatTracker/blob/master/code/OCR.class.php