Skip to content

Commit 4852287

Browse files
committed
Create README.md
1 parent d5301f3 commit 4852287

File tree

1 file changed

+308
-0
lines changed

1 file changed

+308
-0
lines changed

README.md

Lines changed: 308 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,308 @@
1+
2+
# OCR Space API Wrapper
3+
4+
A simple wrapper for [OCR Space](https://ocr.space/) API
5+
6+
[![MIT License](https://img.shields.io/badge/License-MIT-green.svg)](https://choosealicense.com/licenses/mit/)
7+
[![Build Status](https://github.com/Lea-Reift/ocr-space/actions/workflows/ci.yml/badge.svg?branch=master)](https://github.com/Lea-Reift/ocr-space/actions?query=workflow%3A"PHP+Composer")
8+
9+
10+
## Installation
11+
12+
This wrapper requires:
13+
* PHP ^8.2
14+
* CURL extension enabled
15+
16+
You can easily install the wrapper with [Composer](https://getcomposer.org/):
17+
18+
```bash
19+
composer require lea-reift/ocr-space
20+
```
21+
## Usage
22+
23+
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.
24+
25+
```php
26+
require_once 'vendor/autoload.php'; // Don't forget the autoload
27+
28+
use LeaReift\OcrSpace\Client;
29+
30+
$response = (new Client('your-apikey'))
31+
->fromUrl('https://dl.a9t9.com/ocr/solarcell.jpg')
32+
->get();
33+
```
34+
35+
## API Reference
36+
37+
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](https://ocr.space/OCRAPI#PostParameters) to dig deeper with the accepted values, but the library also counts with typed parameters methods, so use it won't be to difficult.
38+
39+
### Constructor
40+
41+
```php
42+
public function __construct(string $apiKey, string $baseUrl = "https://api.ocr.space", string $endpoint = "/parse/image")
43+
```
44+
45+
Creates a new instance of the OCR Space client.
46+
47+
**Parameters:**
48+
- `$apiKey` - The API key for authenticating with the OCR.space service
49+
- `$baseUrl` - The base URL of the API (optional, defaults to "https://api.ocr.space")
50+
- `$endpoint` - The API endpoint (optional, defaults to "/parse/image")
51+
52+
```php
53+
use LeaReift\OcrSpace\Client;
54+
55+
$client = new Client('your-api-key-here');
56+
```
57+
58+
### Data Input Methods
59+
60+
#### fromUrl
61+
62+
```php
63+
public function fromUrl(string $url): self
64+
```
65+
66+
Sets an image URL as input for OCR processing.
67+
68+
**Parameters:**
69+
- `$url` - Valid URL of the image to be processed
70+
71+
```php
72+
$client->fromUrl('https://example.com/image.jpg');
73+
```
74+
75+
#### fromFile
76+
77+
```php
78+
public function fromFile(string|CURLFile $file): self
79+
```
80+
81+
Sets a local file as input for OCR processing.
82+
83+
**Parameters:**
84+
- `$file` - File path as string or CURLFile instance
85+
86+
```php
87+
// Using string
88+
$client->fromFile('/path/to/image.png');
89+
90+
// Using CURLFile
91+
$curlFile = new CURLFile('/path/to/image.png');
92+
$client->fromFile($curlFile);
93+
```
94+
95+
#### fromBase64String
96+
97+
```php
98+
public function fromBase64String(string $base64String): self
99+
```
100+
101+
Sets a base64 string representing an image as input for OCR processing.
102+
103+
**Parameters:**
104+
- `$base64String` - Base64 string representing an image
105+
106+
```php
107+
$base64Image = base64_encode(file_get_contents('image.jpg'));
108+
$client->fromBase64String($base64Image);
109+
```
110+
111+
## Configuration Methods
112+
113+
#### language
114+
115+
```php
116+
public function language(string|LanguageCodeEnum $language): self
117+
```
118+
119+
Sets the language for OCR recognition.
120+
121+
**Parameters:**
122+
- `$language` - Language code as string or LanguageCodeEnum instance
123+
124+
```php
125+
use LeaReift\OcrSpace\Enums\LanguageCodeEnum;
126+
127+
// Using enum
128+
$client->language(LanguageCodeEnum::ENGLISH);
129+
130+
// Using string
131+
$client->language('eng');
132+
```
133+
134+
#### isOverlayRequired
135+
136+
```php
137+
public function isOverlayRequired(bool $isRequired): self
138+
```
139+
140+
Determines if overlay is required on the processed image.
141+
142+
**Parameters:**
143+
- `$isRequired` - Boolean indicating if overlay is required
144+
145+
```php
146+
$client->isOverlayRequired(true);
147+
```
148+
149+
#### detectOrientation
150+
151+
```php
152+
public function detectOrientation(bool $detectOrientation): self
153+
```
154+
155+
Configures whether to automatically detect the image orientation.
156+
157+
**Parameters:**
158+
- `$detectOrientation` - Boolean indicating if orientation should be detected
159+
160+
```php
161+
$client->detectOrientation(true);
162+
```
163+
164+
#### fileType
165+
166+
```php
167+
public function fileType(string|FileTypeEnum $fileType): self
168+
```
169+
170+
Sets the file type of the input image.
171+
172+
**Parameters:**
173+
- `$fileType` - File type as string or FileTypeEnum instance
174+
175+
```php
176+
use LeaReift\OcrSpace\Enums\FileTypeEnum;
177+
178+
// Using enum
179+
$client->fileType(FileTypeEnum::PDF);
180+
181+
// Using string
182+
$client->fileType('pdf');
183+
```
184+
185+
#### isCreateSearchablePdf
186+
187+
```php
188+
public function isCreateSearchablePdf(bool $isCreateSearchablePdf): self
189+
```
190+
191+
Determines if a searchable PDF should be created.
192+
193+
**Parameters:**
194+
- `$isCreateSearchablePdf` - Boolean indicating if a searchable PDF should be created
195+
196+
```php
197+
$client->isCreateSearchablePdf(true);
198+
```
199+
200+
#### isSearchablePdfHideTextLayer
201+
202+
```php
203+
public function isSearchablePdfHideTextLayer(bool $isSearchablePdfHideTextLayer): self
204+
```
205+
206+
Determines if the text layer should be hidden in the searchable PDF.
207+
208+
**Parameters:**
209+
- `$isSearchablePdfHideTextLayer` - Boolean indicating if the text layer should be hidden
210+
211+
```php
212+
$client->isSearchablePdfHideTextLayer(false);
213+
```
214+
215+
#### scale
216+
217+
```php
218+
public function scale(bool $scale): self
219+
```
220+
221+
Determines if the image should be scaled to improve OCR recognition.
222+
223+
**Parameters:**
224+
- `$scale` - Boolean indicating if the image should be scaled
225+
226+
```php
227+
$client->scale(true);
228+
```
229+
230+
#### isTable
231+
232+
```php
233+
public function isTable(bool $isTable): self
234+
```
235+
236+
Configures if the image contains data in table format.
237+
238+
**Parameters:**
239+
- `$isTable` - Boolean indicating if the image contains tables
240+
241+
```php
242+
$client->isTable(true);
243+
```
244+
245+
#### engine
246+
247+
```php
248+
public function engine(int $engine = 1): self
249+
```
250+
251+
Sets the OCR engine to use.
252+
253+
**Parameters:**
254+
- `$engine` - Integer representing the OCR engine (defaults to 1)
255+
256+
```php
257+
$client->engine(2);
258+
```
259+
260+
#### options
261+
262+
```php
263+
public function options(): array
264+
```
265+
266+
Returns an array with all currently configured options.
267+
268+
```php
269+
$options = $client->options();
270+
print_r($options);
271+
```
272+
273+
## Complete Usage Example
274+
275+
```php
276+
use LeaReift\OcrSpace\Client;
277+
use LeaReift\OcrSpace\Enums\LanguageCodeEnum;
278+
279+
// Create client
280+
$client = new Client('your-api-key-here');
281+
282+
// Configure options
283+
$response = $client
284+
->fromUrl('https://example.com/invoice.jpg')
285+
->language(LanguageCodeEnum::ENGLISH)
286+
->detectOrientation(true)
287+
->isTable(true)
288+
->engine(2)
289+
->get();
290+
291+
// Process the response
292+
if (!$response->is_errored_on_processing) {
293+
echo "Recognized text count: " . $response->parsed_results->count();
294+
} else {
295+
echo "Error: " . $response->error_message;
296+
}
297+
```
298+
299+
300+
## Contributing
301+
302+
Contributions are always welcome!
303+
Just open a Pull Request with your changes and I'll review it as soon as I can!
304+
305+
306+
## License
307+
308+
This wrapper is made available under the MIT License (MIT). Please see [License File](LICENSE.md) for more information.

0 commit comments

Comments
 (0)