cdsmths/laravel-ocr-space
Composer 安装命令:
composer require cdsmths/laravel-ocr-space
包简介
OCR PDF's and images with the OCR.Space API from Laravel
README 文档
README
Laravel OCR Space
Laravel OCR Space is a package that allows you to use the OCR.Space API in your Laravel application for Optical Character Recognition (OCR).
Installation
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"
Usage
Get a free Ocr.Space api key
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"
Parsing an Image file
use Codesmiths\LaravelOcrSpace\OcrSpaceOptions; use Codesmiths\LaravelOcrSpace\Facades\OcrSpace; $filePath = 'path/to/image.jpg'; $result = OcrSpace::parseImageFile( $filePath, OcrSpaceOptions::make(), ); dd($result);
Parsing an Image URL
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);
Parsing an base64 encoded image
use Codesmiths\LaravelOcrSpace\OcrSpaceOptions; use Codesmiths\LaravelOcrSpace\Facades\OcrSpace; $base64Image = 'base64 encoded image'; $result = OcrSpace::parseBase64Image( $base64Image, OcrSpaceOptions::make(), ); dd($result);
Parsing an binary image
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);
Parsing with parseImage method
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);
Options
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 Engines
OCR.Space provides three different OCR engines:
- Engine1: The original OCR engine, best for standard documents
- Engine2: Improved OCR engine with better accuracy for most use cases
- Engine3: The latest engine with significantly superior text recognition capabilities
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);
Response
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
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
License / Credits
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.
cdsmths/laravel-ocr-space 适用场景与选型建议
cdsmths/laravel-ocr-space 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 21.73k 次下载、GitHub Stars 达 123, 最近一次更新时间为 2025 年 01 月 12 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「OCR」 「laravel」 「laravel-ocr-space」 「ocr.space」 「Codesmiths」 「Thomas van der Westen」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 cdsmths/laravel-ocr-space 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 cdsmths/laravel-ocr-space 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 cdsmths/laravel-ocr-space 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
The Best Image Ocr SDK For BAT.
The Best Image Ocr SDK For BAT.
腾讯 OCR SDK
Integration of tesseract bridge via FFI and CLI
Alfabank REST API integration
Baidu OCR by smallerfan
统计信息
- 总下载量: 21.73k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 123
- 点击次数: 26
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-01-12