1. Go to this page and download the library: Download mayaram/laravel-ocr library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
mayaram / laravel-ocr example snippets
use Mayaram\LaravelOcr\Facades\LaravelOcr;
// Extract from a local file path
$result = LaravelOcr::extract('/path/to/document.png');
echo $result['text'];
// "INVOICE #1001..."
// Extract from an UploadedFile
$result = LaravelOcr::extract(request()->file('document'));
// Extract a table
$tableResult = LaravelOcr::extractTable('/path/to/invoice.png');
foreach ($tableResult['table'] as $row) {
echo implode(' | ', $row) . "\n";
}
use Mayaram\LaravelOcr\Facades\LaravelOcr;
// Use Tesseract (default)
$result = LaravelOcr::driver('tesseract')->extract($document);
// Switch to AWS Textract for this request
$result = LaravelOcr::driver('aws_textract')->extract($document);
// Switch to Google Vision
$result = LaravelOcr::driver('google_vision')->extract($document);
use Mayaram\LaravelOcr\Models\ProcessedDocument;
// Query processed documents
$documents = ProcessedDocument::where('document_type', 'invoice')
->where('confidence_score', '>=', 0.7)
->latest()
->get();
foreach ($documents as $doc) {
// Get a specific field value
$invoiceNo = $doc->getFieldValue('invoice_number');
// Get all field values as a flat array
$allFields = $doc->getAllFieldValues();
// Check if the result is valid (status=completed & confidence >= 0.7)
if ($doc->isValid()) {
// Process the document
}
}
interface OCRDriver
{
public function extract($document, array $options = []): array;
public function extractTable($document, array $options = []): array;
public function extractBarcode($document, array $options = []): array;
public function extractQRCode($document, array $options = []): array;
public function getSupportedLanguages(): array;
public function getSupportedFormats(): array;
}
use Mayaram\LaravelOcr\Contracts\OCRDriver;
class MyCustomDriver implements OCRDriver
{
public function extract($document, array $options = []): array
{
// Your implementation
return [
'text' => 'extracted text',
'confidence' => 0.95,
'bounds' => [],
'metadata' => ['engine' => 'custom'],
];
}
public function extractTable($document, array $options = []): array { /* ... */ }
public function extractBarcode($document, array $options = []): array { /* ... */ }
public function extractQRCode($document, array $options = []): array { /* ... */ }
public function getSupportedLanguages(): array { return ['en' => 'English']; }
public function getSupportedFormats(): array { return ['jpg', 'png', 'pdf']; }
}