public function recognize($filename, array $languages = null, $pageSegMode = self::PAGE_SEG_MODE_AUTOMATIC_OCR)
    {
        if ($pageSegMode < 0 || $pageSegMode > 10) {
            throw new \InvalidArgumentException(
                'Page seg mode must be between 0 and 10'
            );
        }
        
        $tempFile = tempnam(sys_get_temp_dir(), 'tesseract');
        
        $arguments = array(
            $filename,
            $tempFile,
            '-psm',
            $pageSegMode
        );
        
        if (null !== $languages) {
            $arguments[] = '-l';
            $arguments[] = implode('+', $languages);
        }
        
        $this->execute($arguments);

        $recognizedText = trim(\file_get_contents($tempFile . '.txt'));

        if (file_exists($tempFile)) {
            unlink($tempFile);
        }

        if (file_exists($tempFile . '.txt')) {
            unlink($tempFile . '.txt');
        }

        return $recognizedText;
    }