onepointhub / laravel_ocr
Laravel Optical Character Reader (OCR) package using different OCR engines like Tesseract.
Requires
- php: ^8.2.0
- illuminate/console: ^11.0
- illuminate/filesystem: ^11.0
- illuminate/support: ^11.0
- illuminate/validation: ^11.0
- symfony/console: ^7.0
Requires (Dev)
- laravel/pint: ^1.18
- orchestra/testbench: ^9.5
- pestphp/pest: ^3.2
- phpstan/phpstan: ^1.10
README
Table of Contents
About
Laravel Optical Character Reader (OCR) package using ocr engines like Tesseract under the hood.
Inspired by LaraOCR
Features
- Read text from image using a Web Page or CLI.
- Can be used as a Laravel Facade
- Configurable and Extendable
Requirements
OCR Engine Tesseract must be installed in the system.
Follow Tesseract installation guide here.
Make sure from the command line you have the tesseract
command available.
Installation
Install the package
composer require onepointhub/laravel_ocr
Then publish this package
php artisan vendor:publish --provider=Ntoufoudis\\LaravelOcr\\LaravelOcrServiceProvider
Usage
Currently, the package only supports Tesseract so there is no need to alter anything in the config file.
Tesseract
Check if Tesseract is installed in your system
php artisan tesseract:check
Parse images via artisan command
php artisan image:parse {imagePaths}
The command accepts multiple absolute paths for images and outputs the parsed text.
Example:
php artisan image:parse /path/to/the/image/file.jpg Output: =================== Parsed Result ======================== __________________________________________________________ Example : Process Chocolate cake with vanilla/almond frosting provides a tasty treat for any occasion. Follow the directions for making any flavor of chocolate cake. While the cake bakes in the oven, prepare a box of vanilla frosting mix added with roasted and buttered almond slivers. Evenly coated in melted butter the almond slivers toast in a hot skillt for approximately ten minutes. Thoroughly toast the slivers without turning them crispy. Cool the toasted almonds and set aside a handful for the final touch. Fold the almonds into the vanilla frosting, and after covering the cake, sprinkle a handful of toasted almonds on the top. After serving this desert to your guests theny generally ask for the receipe of your chocolate/vanilla/almond cake. Now you have the opportunity to smile and... __________________________________________________________
Use in your project
You can access the Ocr Facade
to scan an image:
use Ntoufoudis\LaravelOcr\Facades\Ocr; Ocr::scan($imagePath);
Examples
Controller:
<?php namespace App\Http\Controllers; use App\Http\Controllers\Controller; use Ntoufoudis\LaravelOcr\Facades\Ocr; class OcrController extends Controller { public function index() { return view('upload_image'); } public function readImage() { $image = request('image'); if (isset($image) && $image->getPathName()) { $parsedText = Ocr::scan($image->getPathName()); return view('parsed_image', compact('parsedText')); } } }
Views:
//resources/views/upload_image.blade.php <div> <h2>Upload Image</h2> <form action="{{ route('read-image') }}" enctype="multipart/form-data" method="POST"> @csrf <input type="file" name="image" placeholder="Select Image"> <button type="submit">Parse Text</button> </form> </div>
//resources/views/parsed_image.blade.php <div> <h2>Output</h2> @if(!empty($parsedText)) <p>{{ $parsedText }}</p> @else <p>No Text Found</p> @endif <a href="{{ route('home') }}">Parse another image</a> </div>
Routes:
//routes/web.php Route::get('/upload_image', [App\Http\Controllers\OcrController::class, 'index'])->name('home'); Route::post('/upload_image', [App\Http\Controllers\OcrController::class, 'readImage'])->name('read-image');
Contributing
Please see CONTRIBUTING for details.
Security
If you discover any security related issues, please email info@ntoufoudis.com instead of using the issue tracker.
License
The MIT License (MIT). Please see License File for more information.