OCR PDF's and images with the OCR.Space API from Laravel
cdsmths/laravel-ocr-space is a Laravel package for ocr pdf's and images with the ocr.space api from laravel.
It currently has 124 GitHub stars and 28.340 downloads on Packagist (latest version 1.0.6).
Install it with composer require cdsmths/laravel-ocr-space.
Discover more Laravel packages by cdsmths
or browse all Laravel packages to compare alternatives.
Last updated
Laravel OCR Space is a package that allows you to use the OCR.Space API in your Laravel application for Optical Character Recognition (OCR).
You can install the package via composer:
composer require cdsmths/laravel-ocr-space
You can publish the config file with:
php artisan vendor:publish --provider="Codesmiths\LaravelOcrSpace\LaravelOcrSpaceServiceProvider" --tag="laravel-ocr-space"
You can get a free api key from ocr.space. This key is required to use the package.
You should add this key to your .env:
OCR_SPACE_API_KEY="YOUR API KEY"
use Codesmiths\LaravelOcrSpace\OcrSpaceOptions;
use Codesmiths\LaravelOcrSpace\Facades\OcrSpace;
$filePath = 'path/to/image.jpg';
$result = OcrSpace::parseImageFile(
$filePath,
OcrSpaceOptions::make(),
);
dd($result);
use Codesmiths\LaravelOcrSpace\OcrSpaceOptions;
use Codesmiths\LaravelOcrSpace\Facades\OcrSpace;
$imageUrl = 'https://example.com/image.jpg';
$options = new \Codesmiths\LaravelOcrSpace\OcrSpaceOptions();
$result = OcrSpace::parseImageUrl(
$imageUrl,
OcrSpaceOptions::make(),
);
dd($result);
use Codesmiths\LaravelOcrSpace\OcrSpaceOptions;
use Codesmiths\LaravelOcrSpace\Facades\OcrSpace;
$base64Image = 'base64 encoded image';
$result = OcrSpace::parseBase64Image(
$base64Image,
OcrSpaceOptions::make(),
);
dd($result);
use Codesmiths\LaravelOcrSpace\OcrSpaceOptions;
use Codesmiths\LaravelOcrSpace\Facades\OcrSpace;
$binaryImage = file_get_contents('path/to/image.jpg');
// File type is required for binary images
$options = OcrSpaceOptions::make()
->fileType('image/jpg');
$result = OcrSpace::parseBinaryImage(
$binaryImage,
$options,
);
dd($result);
use Codesmiths\LaravelOcrSpace\OcrSpaceOptions;
use Codesmiths\LaravelOcrSpace\Facades\OcrSpace;
use Codesmiths\LaravelOcrSpace\Enums\InputType;
$filePath = 'path/to/image.jpg';
$result = OcrSpace::parseImage(
InputType::File
$filePath,
OcrSpaceOptions::make(),
);
dd($result);
You can pass options to the parseImageFile, parseImageUrl, parseBase64Image, parseBinaryImage and parseImage methods.
use Codesmiths\LaravelOcrSpace\OcrSpaceOptions;
use Codesmiths\LaravelOcrSpace\Enums\Language;
use Codesmiths\LaravelOcrSpace\Enums\OcrSpaceEngine;
// All possible options
$options = OcrSpaceOptions::make()
->language(Language::English)
->overlayRequired(true)
->fileType('image/png')
->detectOrientation(true)
->isCreateSearchablePdf(true)
->isSearchablePdfHideTextLayer(true)
->scale(true)
->isTable(true)
->OCREngine(OcrSpaceEngine::Engine1); // Engine1, Engine2, or Engine3
OCR.Space provides three different OCR engines:
You can specify which engine to use:
use Codesmiths\LaravelOcrSpace\Enums\OcrSpaceEngine;
// Use Engine 1
$options = OcrSpaceOptions::make()->ocrEngine(OcrSpaceEngine::Engine1);
// Use Engine 2
$options = OcrSpaceOptions::make()->ocrEngine(OcrSpaceEngine::Engine2);
// Use Engine 3 (recommended for best accuracy)
$options = OcrSpaceOptions::make()->ocrEngine(OcrSpaceEngine::Engine3);
All methods return an instance of Codesmiths\LaravelOcrSpace\OcrSpaceResponse which has the following methods:
$response->getParsedResults(); // Returns an Collection `ParsedResult`
$response->getOCRExitCode(); // Returns the exit code
$response->getIsErroredOnProcessing(); // Returns a boolean
$response->getErrorMessage(); // Returns the error message
$response->getErrorMessageDetails(); // Returns the error message details
$response->getProcessingTimeInMilliseconds(); // Returns the processing time in milliseconds
$response->getSearchablePdfUrl(); // Returns the searchable pdf url
$response->hasSearchablePdfUrl(); // Returns if the response has a searchable pdf url
$response->hasError(); // Returns if the response has an error
$response->hasParsedResults(); // Returns if the response has parsed results
If you want to get value from getParsedResults(), you can use the following methods:
$parsedResults = $response->getParsedResults();
$parsedResults->first()->getParsedText(), // Returns the parsed text from the first parsed result
$parsedResults->first()->getTextOverlay(), // Returns the text overlay from the first parsed result
$parsedResults->first()->getFileParseExitCode(), // Returns the file parse exit code from the first parsed result
$parsedResults->first()->getErrorMessage(), // Returns the error message from the first parsed result
$parsedResults->first()->getErrorDetails(), // Returns the error message details from the first parsed result
$parsedResults->first()->getSerializedParsedText(), // Returns the serialized parsed text from the first parsed result
This package our Codesmiths is not affiliated with OCR.Space and is not an official package. It is a wrapper around the OCR.Space API.
The MIT License (MIT). Please see License File for more information.