承接 lea-reift/ocr-space 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

lea-reift/ocr-space

Composer 安装命令:

composer require lea-reift/ocr-space

包简介

OCR Space Api wrapper library for PHP

README 文档

README

A simple wrapper for OCR Space API

MIT License Build Status

Installation

This wrapper requires:

  • PHP ^8.2
  • CURL extension enabled

You can easily install the wrapper with Composer:

  composer require lea-reift/ocr-space

Usage

The only required parameter for the Client is your apikey (this will be used in the requests). Then, you can chain the desired options in helper methods for the accepted request parameters.

require_once 'vendor/autoload.php'; // Don't forget the autoload

use LeaReift\OcrSpace\Client;

$response = (new Client('your-apikey'))
    ->fromUrl('https://dl.a9t9.com/ocr/solarcell.jpg')
    ->get();

API Reference

The Client class provides an interface to interact with the OCR.space API, enabling optical character recognition on images from different sources. Check OCR Space Documentation to dig deeper with the accepted values, but the library also counts with typed parameters methods, so use it won't be to difficult.

Constructor

public function __construct(string $apiKey, string $baseUrl = "https://api.ocr.space", string $endpoint = "/parse/image")

Creates a new instance of the OCR Space client.

Parameters:

  • $apiKey - The API key for authenticating with the OCR.space service
  • $baseUrl - The base URL of the API (optional, defaults to "https://api.ocr.space")
  • $endpoint - The API endpoint (optional, defaults to "/parse/image")
use LeaReift\OcrSpace\Client;

$client = new Client('your-api-key-here');

Data Input Methods

fromUrl

public function fromUrl(string $url): self

Sets an image URL as input for OCR processing.

Parameters:

  • $url - Valid URL of the image to be processed
$client->fromUrl('https://example.com/image.jpg');

fromFile

public function fromFile(string|CURLFile $file): self

Sets a local file as input for OCR processing.

Parameters:

  • $file - File path as string or CURLFile instance
// Using string
$client->fromFile('/path/to/image.png');

// Using CURLFile
$curlFile = new CURLFile('/path/to/image.png');
$client->fromFile($curlFile);

fromBase64String

public function fromBase64String(string $base64String): self

Sets a base64 string representing an image as input for OCR processing.

Parameters:

  • $base64String - Base64 string representing an image
$base64Image = base64_encode(file_get_contents('image.jpg'));
$client->fromBase64String($base64Image);

Configuration Methods

language

public function language(string|LanguageCodeEnum $language): self

Sets the language for OCR recognition.

Parameters:

  • $language - Language code as string or LanguageCodeEnum instance
use LeaReift\OcrSpace\Enums\LanguageCodeEnum;

// Using enum
$client->language(LanguageCodeEnum::ENGLISH);

// Using string
$client->language('eng');

isOverlayRequired

public function isOverlayRequired(bool $isRequired): self

Determines if overlay is required on the processed image.

Parameters:

  • $isRequired - Boolean indicating if overlay is required
$client->isOverlayRequired(true);

detectOrientation

public function detectOrientation(bool $detectOrientation): self

Configures whether to automatically detect the image orientation.

Parameters:

  • $detectOrientation - Boolean indicating if orientation should be detected
$client->detectOrientation(true);

fileType

public function fileType(string|FileTypeEnum $fileType): self

Sets the file type of the input image.

Parameters:

  • $fileType - File type as string or FileTypeEnum instance
use LeaReift\OcrSpace\Enums\FileTypeEnum;

// Using enum
$client->fileType(FileTypeEnum::PDF);

// Using string
$client->fileType('pdf');

isCreateSearchablePdf

public function isCreateSearchablePdf(bool $isCreateSearchablePdf): self

Determines if a searchable PDF should be created.

Parameters:

  • $isCreateSearchablePdf - Boolean indicating if a searchable PDF should be created
$client->isCreateSearchablePdf(true);

isSearchablePdfHideTextLayer

public function isSearchablePdfHideTextLayer(bool $isSearchablePdfHideTextLayer): self

Determines if the text layer should be hidden in the searchable PDF.

Parameters:

  • $isSearchablePdfHideTextLayer - Boolean indicating if the text layer should be hidden
$client->isSearchablePdfHideTextLayer(false);

scale

public function scale(bool $scale): self

Determines if the image should be scaled to improve OCR recognition.

Parameters:

  • $scale - Boolean indicating if the image should be scaled
$client->scale(true);

isTable

public function isTable(bool $isTable): self

Configures if the image contains data in table format.

Parameters:

  • $isTable - Boolean indicating if the image contains tables
$client->isTable(true);

engine

public function engine(int $engine = 1): self

Sets the OCR engine to use.

Parameters:

  • $engine - Integer representing the OCR engine (defaults to 1)
$client->engine(2);

options

public function options(): array

Returns an array with all currently configured options.

$options = $client->options();
print_r($options);

Complete Usage Example

use LeaReift\OcrSpace\Client;
use LeaReift\OcrSpace\Enums\LanguageCodeEnum;

// Create client
$client = new Client('your-api-key-here');

// Configure options
$response = $client
    ->fromUrl('https://example.com/invoice.jpg')
    ->language(LanguageCodeEnum::ENGLISH)
    ->detectOrientation(true)
    ->isTable(true)
    ->engine(2)
    ->get();

// Process the response
if (!$response->is_errored_on_processing) {
    echo "Recognized text count: " . $response->parsed_results->count();
} else {
    echo "Error: " . $response->error_message;
}

Contributing

Contributions are always welcome! Just open a Pull Request with your changes and I'll review it as soon as I can!

Credits

OCR.space is a service of a9t9 software GmbH. They are also in github

This package is an independent development that is in no way linked to a9t9 software. I want to acknowledge a9t9 for put this OCR service online.

License

This wrapper is made available under the MIT License (MIT). Please see License File for more information.

lea-reift/ocr-space 适用场景与选型建议

lea-reift/ocr-space 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 6 次下载、GitHub Stars 达 1, 最近一次更新时间为 2025 年 04 月 25 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 lea-reift/ocr-space 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 lea-reift/ocr-space 我们能提供哪些服务?
定制开发 / 二次开发

基于 lea-reift/ocr-space 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 6
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 16
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 1
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-04-25