- Implemented `get_ocr_result` method in the `ocr_space` class to interact with the OCR Space API. - Added `process_ocr` method to `image_processor_t` for handling OCR logic. - Updated interfaces (`ocr_space_i` and `image_processor_i`) to support OCR functionality. - Introduced `moduleScannerRoute` with endpoints for testing and license plate recognition workflows. - Included data validation and exception handling for OCR processing.
102 lines
2.9 KiB
PHP
102 lines
2.9 KiB
PHP
<?php
|
|
|
|
namespace traits;
|
|
|
|
use classes\image_processor;
|
|
use classes\ocr_space;
|
|
|
|
trait image_processor_t
|
|
{
|
|
/**
|
|
* The image base64 encoded string.
|
|
* This property is used to store the base64 encoded representation of an image.
|
|
* @see image_processor::set_image_base64(), image_processor::get_image_base64()
|
|
* @var string|null
|
|
* @example data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUA...
|
|
*/
|
|
public ?string $base64_image = null;
|
|
/**
|
|
* Validates the base64 encoded image string.
|
|
* This method checks if the provided base64 string is a valid image format.
|
|
*
|
|
* @param string $base64 The base64 encoded image string to validate.
|
|
* @return bool Returns true if the base64 string is valid, false otherwise.
|
|
*/
|
|
function validate_image_base64(string $base64): bool
|
|
{
|
|
// Check if the base64 string is valid
|
|
if (empty($base64)) {
|
|
return false;
|
|
}
|
|
|
|
// Check if the base64 string starts with a valid data URI scheme
|
|
if (!preg_match('/^data:image\/[a-zA-Z]+;base64,/', $base64)) {
|
|
return false;
|
|
}
|
|
|
|
// Validate the base64 encoding
|
|
$data = explode(',', $base64, 2);
|
|
if (count($data) !== 2 || !base64_decode($data[1], true)) {
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
/**
|
|
* @inheritDoc
|
|
*/
|
|
public function set_image_base64(string $base64): void
|
|
{
|
|
if ($this->validate_image_base64($base64)) {
|
|
$this->base64_image = $base64;
|
|
} else {
|
|
throw new \InvalidArgumentException("Invalid base64 image string provided.");
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @inheritDoc
|
|
*/
|
|
function get_image_base64(): string
|
|
{
|
|
if ($this->base64_image === null) {
|
|
throw new \RuntimeException("No base64 image string set.");
|
|
}
|
|
return $this->base64_image;
|
|
}
|
|
|
|
/**
|
|
* @inheritDoc
|
|
*/
|
|
abstract function process_image(array $params): bool;
|
|
|
|
/**
|
|
* @inheritDoc
|
|
*/
|
|
public function get_image_format(): string
|
|
{
|
|
if ($this->base64_image === null) {
|
|
throw new \RuntimeException("No base64 image string set.");
|
|
}
|
|
|
|
// Extract the format from the base64 string
|
|
if (preg_match('/^data:image\/([a-zA-Z]+);base64,/', $this->base64_image, $matches)) {
|
|
return $matches[1];
|
|
} else {
|
|
throw new \RuntimeException("Invalid base64 image format.");
|
|
}
|
|
}
|
|
/**
|
|
* @inheritDoc
|
|
*/
|
|
public function process_ocr(): array
|
|
{
|
|
if ($this->base64_image === null) {
|
|
throw new \RuntimeException("No base64 image string set for OCR processing.");
|
|
}
|
|
// Check if this exact image has already been processed
|
|
// Return the OCR result using the ocr_space class
|
|
return (new ocr_space())->get_ocr_result($this->base64_image);
|
|
}
|
|
|
|
} |