diff --git a/app/Controllers/HomeController.php b/app/Controllers/HomeController.php index d49c393..82592b2 100644 --- a/app/Controllers/HomeController.php +++ b/app/Controllers/HomeController.php @@ -1,10 +1,116 @@ processPdf($filePath, $startPage, $endPage, $language, $text, $message); + } else { + $this->processImage($filePath, $language, $text, $message); + } + } else { + $message = 'Failed to upload file'; + } + } else { + $message = 'No file uploaded or upload error'; + } + } + + return view('welcome', [ + 'message' => $message, + 'text' => $text, + 'startPage' => $startPage, + 'endPage' => $endPage, + 'language' => $language, + 'book' => $book, + 'is_speechable' => true + ]); + } + + private function processImage($filePath, $language, &$text, &$message) { + try { + $ocr = new TesseractOCR($filePath); + $text = $ocr->lang($language)->run(); + + $message = 'Image processing completed successfully.'; + + unlink($filePath); + } catch (\Exception $e) { + error_log('OCR Processing Failed: ' . $e->getMessage()); + $text = 'Error: ' . $e->getMessage(); + $message = 'Image processing failed'; + } + } + + private function processPdf($filePath, $startPage, $endPage, $language, &$text, &$message) { + try { + $pdf = new Pdf($filePath); + $pages = $pdf->pageCount(); + $text = ''; + + if (($endPage > 0) && ($endPage <= $pages)) { + $pages = $endPage; + } + + if ($startPage > $pages) { + $message = "Start page exceeds total pages in the PDF."; + return; + } + + for ($page = $startPage; $page <= $pages; $page++) { + $imagePath = 'books/page_' . $page . '.jpg'; + $pdf->selectPage($page)->save($imagePath); + + $ocr = new TesseractOCR($imagePath); + $text .= $ocr->lang($language)->run(); + + unlink($imagePath); + } + + $message = 'PDF processing completed successfully.'; + unlink($filePath); + } catch (\Exception $e) { + error_log('PDF Processing Failed: ' . $e->getMessage()); + $text = 'Error: ' . $e->getMessage(); + $message = 'PDF processing failed'; + } + } } diff --git a/composer.json b/composer.json index 007557b..5d5a1bb 100644 --- a/composer.json +++ b/composer.json @@ -15,5 +15,9 @@ "name": "Jakiur Rahman", "email": "70786208+Jakiur1234@users.noreply.github.com" } - ] + ], + "require": { + "thiagoalessio/tesseract_ocr": "^2.13", + "spatie/pdf-to-image": "^3.1" + } } diff --git a/composer.lock b/composer.lock index 2091d19..01c0134 100644 --- a/composer.lock +++ b/composer.lock @@ -4,8 +4,128 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "d3ba7525ce39bcc30db4be3be7ab9756", - "packages": [], + "content-hash": "239a802a0b1a43e39f9315992a137ac4", + "packages": [ + { + "name": "spatie/pdf-to-image", + "version": "3.1.0", + "source": { + "type": "git", + "url": "https://github.com/spatie/pdf-to-image.git", + "reference": "acc31c5236180e475c09c1a5c68412876f0bd89a" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/spatie/pdf-to-image/zipball/acc31c5236180e475c09c1a5c68412876f0bd89a", + "reference": "acc31c5236180e475c09c1a5c68412876f0bd89a", + "shasum": "" + }, + "require": { + "ext-imagick": "*", + "php": "^8.2" + }, + "require-dev": { + "pestphp/pest": "^2.34" + }, + "type": "library", + "autoload": { + "psr-4": { + "Spatie\\PdfToImage\\": "src" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Freek Van der Herten", + "email": "freek@spatie.be", + "homepage": "https://spatie.be", + "role": "Developer" + }, + { + "name": "Patrick Organ", + "email": "patrick@permafrost.dev", + "homepage": "https://permafrost.dev", + "role": "Developer" + } + ], + "description": "Convert a pdf to an image", + "homepage": "https://github.com/spatie/pdf-to-image", + "keywords": [ + "convert", + "image", + "pdf", + "pdf-to-image", + "spatie" + ], + "support": { + "issues": "https://github.com/spatie/pdf-to-image/issues", + "source": "https://github.com/spatie/pdf-to-image/tree/3.1.0" + }, + "funding": [ + { + "url": "https://spatie.be/open-source/support-us", + "type": "custom" + }, + { + "url": "https://github.com/spatie", + "type": "github" + } + ], + "time": "2024-09-14T17:02:32+00:00" + }, + { + "name": "thiagoalessio/tesseract_ocr", + "version": "2.13.0", + "source": { + "type": "git", + "url": "https://github.com/thiagoalessio/tesseract-ocr-for-php.git", + "reference": "232a8cb9d571992f9bd1e263f2f6909cf6c173a1" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/thiagoalessio/tesseract-ocr-for-php/zipball/232a8cb9d571992f9bd1e263f2f6909cf6c173a1", + "reference": "232a8cb9d571992f9bd1e263f2f6909cf6c173a1", + "shasum": "" + }, + "require": { + "php": "^5.3 || ^7.0 || ^8.0" + }, + "require-dev": { + "phpunit/php-code-coverage": "^2.2.4 || ^9.0.0" + }, + "type": "library", + "autoload": { + "psr-4": { + "thiagoalessio\\TesseractOCR\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "thiagoalessio", + "email": "thiagoalessio@me.com" + } + ], + "description": "A wrapper to work with Tesseract OCR inside PHP.", + "keywords": [ + "OCR", + "Tesseract", + "text recognition" + ], + "support": { + "irc": "irc://irc.freenode.net/tesseract-ocr-for-php", + "issues": "https://github.com/thiagoalessio/tesseract-ocr-for-php/issues", + "source": "https://github.com/thiagoalessio/tesseract-ocr-for-php" + }, + "time": "2023-10-05T21:14:48+00:00" + } + ], "packages-dev": [], "aliases": [], "minimum-stability": "stable", diff --git a/resources/views/partials/alert.php b/resources/views/partials/alert.php new file mode 100644 index 0000000..54cc208 --- /dev/null +++ b/resources/views/partials/alert.php @@ -0,0 +1,11 @@ +
+ +
diff --git a/resources/views/partials/form.php b/resources/views/partials/form.php new file mode 100644 index 0000000..23d3757 --- /dev/null +++ b/resources/views/partials/form.php @@ -0,0 +1,41 @@ +
+
+
+
+ + +
+
+ + +
+
+ + +
+
+ +
+
+ +
+
+
+
+ +
+
diff --git a/resources/views/partials/speech.php b/resources/views/partials/speech.php new file mode 100644 index 0000000..8998b78 --- /dev/null +++ b/resources/views/partials/speech.php @@ -0,0 +1,9 @@ + + +Download Speech + diff --git a/resources/views/welcome.php b/resources/views/welcome.php index 65cbc04..ba7ff3f 100644 --- a/resources/views/welcome.php +++ b/resources/views/welcome.php @@ -5,9 +5,18 @@ Welcome + -

Welcome

+
+ $message]); + } ?> + isset($text) ? $text : '']); ?> + $text ]); + }?> +
diff --git a/routes/router.php b/routes/router.php index 4866ce9..f41d452 100644 --- a/routes/router.php +++ b/routes/router.php @@ -10,6 +10,14 @@ public static function get( $route, $controller ) } } + public static function post( $route, $controller ) + { + if( $_SERVER['REQUEST_METHOD'] === 'POST' && $_SERVER['REQUEST_URI'] === $route ) + { + (new static)->callController( $controller ); + } + } + private function callController($data) { $controller = ''; diff --git a/routes/web.php b/routes/web.php index d29a69e..a2d91ae 100644 --- a/routes/web.php +++ b/routes/web.php @@ -3,3 +3,4 @@ use App\Controllers\HomeController; Route::get('/', [ HomeController::class, 'index' ] ); +Route::post('/', [ HomeController::class, 'process' ] );