PHP code example of lea-reift / ocr-space

1. Go to this page and download the library: Download lea-reift/ocr-space 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/ */

    

lea-reift / ocr-space example snippets




use LeaReift\OcrSpace\Client;

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

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

use LeaReift\OcrSpace\Client;

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

public function fromUrl(string $url): self

$client->fromUrl('https://example.com/image.jpg');

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

// Using string
$client->fromFile('/path/to/image.png');

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

public function fromBase64String(string $base64String): self

$base64Image = base64_encode(file_get_contents('image.jpg'));
$client->fromBase64String($base64Image);

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

use LeaReift\OcrSpace\Enums\LanguageCodeEnum;

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

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

public function isOverlayRequired(bool $isRequired): self

$client->isOverlayRequired(true);

public function detectOrientation(bool $detectOrientation): self

$client->detectOrientation(true);

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

use LeaReift\OcrSpace\Enums\FileTypeEnum;

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

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

public function isCreateSearchablePdf(bool $isCreateSearchablePdf): self

$client->isCreateSearchablePdf(true);

public function isSearchablePdfHideTextLayer(bool $isSearchablePdfHideTextLayer): self

$client->isSearchablePdfHideTextLayer(false);

public function scale(bool $scale): self

$client->scale(true);

public function isTable(bool $isTable): self

$client->isTable(true);

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

$client->engine(2);

public function options(): array

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

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;
}