From 2b048814bd67d6d1669be66d44c35fa65881f889 Mon Sep 17 00:00:00 2001 From: dhanabalan Date: Sat, 18 Oct 2025 11:13:39 +0530 Subject: [PATCH] alter logic in ocr for route --- routes/web.php | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/routes/web.php b/routes/web.php index 249e024..4c9954b 100644 --- a/routes/web.php +++ b/routes/web.php @@ -38,17 +38,17 @@ use thiagoalessio\TesseractOCR\TesseractOCR; return response()->json(['success' => false, 'error' => 'File not found']); } - try { - // $text = (new TesseractOCR($filePath))->lang('eng')->run(); - $text = (new TesseractOCR($filePath)) - ->executable('/usr/bin/tesseract') - ->env(['PATH' => '/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin']) - ->lang('eng') - ->run(); + $output = null; + $return_var = null; + // Call tesseract directly, capture stderr with 2>&1 + exec("/usr/bin/tesseract " . escapeshellarg($filePath) . " stdout -l eng 2>&1", $output, $return_var); + + if ($return_var === 0) { + $text = implode("\n", $output); return response()->json(['success' => true, 'text' => $text]); - } catch (\Exception $e) { - return response()->json(['success' => false, 'error' => $e->getMessage()]); + } else { + return response()->json(['success' => false, 'error' => implode("\n", $output)]); } });