PHP code example of laravelsmartocr / laravel-smart-ocr
1. Go to this page and download the library: Download laravelsmartocr/laravel-smart-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/ */
laravelsmartocr / laravel-smart-ocr example snippets
// Works free, offline, zero config — start in 60 seconds
$result = SmartOCR::driver('tesseract')->read('/path/to/invoice.jpg');
echo $result->text();
// Switch providers with a single .env change — zero code changes
SMART_OCR_DRIVER=google # or aws, azure, claude, openai, mistral, pdf
final class Invoice {
public function __construct(
#[Field('Invoice number')] public string $number = '',
#[Field('Grand total, number only')] public float $total = 0.0,
#[Field('Due date, ISO format')] public ?string $dueDate = null,
) {}
}
$result = SmartOCR::from($file)->extract(Invoice::class);
echo $result->data->number; // "INV-2026-001"
echo $result->data->total; // 1250.0
$result = SmartOCR::from($file)->read();
echo $result->toMarkdown(); // tables and headings preserved
foreach ($result->chunks(maxTokens: 500) as $chunk) {
echo $chunk->text; // chunk text
echo $chunk->heading; // section heading
echo $chunk->startPage; // page number
}
use LaravelSmartOCR\Facades\SmartOCR;
// 60-second quick start — free, no API key needed
$result = SmartOCR::driver('tesseract')->read('/path/to/image.jpg');
echo $result->text();
// Full result object
echo $result->confidence() ?? 'n/a'; // float|null — null when provider does not expose a real score
$result->tables(); // normalized tables
$result->fields(); // key-value form fields
$result->toArray(); // complete structured data
$result = SmartOCR::driver('azure')->read('document.jpg');
// With language hint
$result = SmartOCR::driver('azure')->language('en')->read('scan.pdf');
echo $result->text();
foreach ($result->lines() as $line) {
$line['text']; // 'Invoice Total: $1,200'
$line['confidence']; // 0.98
$line['bounding_box']; // ['x'=>100,'y'=>200,'width'=>300,'height'=>40,'points'=>[...]]
}
$result = SmartOCR::driver('claude')->read('invoice.jpg');
echo $result->text();
echo $result->confidence(); // null — Claude does not expose a confidence score
$result = SmartOCR::driver('openai')->read('receipt.png');
echo $result->text();
echo $result->confidence(); // null — OpenAI does not expose a confidence score
$result = SmartOCR::driver('pdf')->read('digital-invoice.pdf');
echo $result->text();
echo $result->confidence(); // null — text extraction does not produce a confidence score
$result = SmartOCR::driver('google')->read($file);
// Text
$result->text(); // string — full extracted text
$result->confidence(); // float|null — normalized 0.0–1.0; null when provider does not expose a real score
$result->provider(); // string — 'google', 'aws', 'azure', etc.
$result->isSuccessful(); // bool
// Structure
$result->pages(); // array of pages with text, confidence, lines, blocks
$result->lines(); // array of lines with text, confidence, bounding_box
$result->words(); // array of words with text, confidence, bounding_box
$result->blocks(); // array of blocks (Google-style paragraph groups)
$result->tables(); // array of tables with headers and rows
$result->fields(); // array of key-value pairs (AWS Textract forms)
// Raw & meta
$result->raw(); // original provider response, unmodified
$result->metadata(); // engine, language, processing info
$result->errors(); // any non-fatal errors
// Full structured array
$result->toArray();
[
'x' => 100, // left edge in pixels
'y' => 200, // top edge in pixels
'width' => 300, // width in pixels
'height' => 40, // height in pixels
'points' => [ // four corner points
['x' => 100, 'y' => 200],
['x' => 400, 'y' => 200],
['x' => 400, 'y' => 240],
['x' => 100, 'y' => 240],
],
]
use LaravelSmartOCR\Extraction\Attributes\Field;
use LaravelSmartOCR\Extraction\Attributes\ListOf;
final class Invoice {
public function __construct(
#[Field('Invoice number')] public string $number = '',
#[Field('Grand total, number only')] public float $total = 0.0,
#[Field('Due date, ISO format')] public ?string $dueDate = null,
) {}
}
$result = SmartOCR::from($file)->extract(Invoice::class);
// $result->data is a typed Invoice object
echo $result->data->number; // "INV-2026-001"
echo $result->data->total; // 1250.0
// Per-field citations
$field = $result->field('total');
echo $field->confidence; // float or null
echo $field->citation->page; // 1
$result = SmartOCR::from($file)->read();
// Render as Markdown (tables, headings preserved)
echo $result->toMarkdown();
// Split into chunks for vector stores
foreach ($result->chunks(maxTokens: 500) as $chunk) {
echo $chunk->text; // chunk text
echo $chunk->heading; // section heading
echo $chunk->startPage; // page number
}
// Cheapest available driver first
SmartOCR::from($file)->cheapest()->read();
// Highest quality first
SmartOCR::from($file)->best()->read();
// Escalate if confidence is low
SmartOCR::from($file)
->using('tesseract')
->escalateTo('mistral', whenConfidenceBelow: 0.80)
->read();
// Cache result for 30 days (keyed by file hash + driver)
SmartOCR::from($file)->cache(ttl: now()->addDays(30))->read();
$result = SmartOCR::from($file)->read();
// Redact PII from text
$clean = $result->redact(['email', 'phone', 'card'])->text();
// Classify document type
$type = $result->classify(); // ['type' => 'invoice', 'confidence' => 0.75]
$result = SmartOCR::from($file)->extract(Invoice::class);
// Fields below 0.85 confidence go to the review queue
$reviews = $result->sendToReview(threshold: 0.85, documentHash: hash_file('sha256', $file));
// Later, approve or correct
$review->approve();
$review->correct('INV-2026-001');
echo $review->final(); // corrected value
// Language hint
$result = SmartOCR::driver('google')
->language('fr')
->read($file);
// Fallback to Tesseract if cloud fails
$result = SmartOCR::driver('azure')
->fallback('tesseract')
->read($file);
// Chain with fallback
$result = SmartOCR::driver('google')
->language('en')
->fallback('tesseract')
->read($request->file('invoice'));