Files
api/services/nginx/app/classes/licenseplaterecognizer.php
T
Jeppe Bundgaard 81aa406fb6 Add License Plate Recognizer integration and vehicle product suggestion route
- Implemented `licensePlateRecognizer` module with configuration classes (`enabled`, `api_key`) and API handling.
- Introduced `moduleConfigRoute` endpoints for fetching and updating `licensePlateRecognizer` configurations.
- Added `vehicleProductSuggestionRoute` with logic to fetch product suggestions based on vehicle plates.
- Extended `moduleScannerRoute` with license plate recognition test and exception handling.
- Updated `index.php` to include `licensePlateRecognizer` class.
2025-09-01 10:11:37 +02:00

89 lines
2.7 KiB
PHP

<?php
namespace classes;
require_once WD . '/modules/licenseplaterecognizer/licenseplaterecognizer_c.php';
use Exception;
use interfaces\licenseplaterecognizer_i;
use licenseplaterecognizer\licenseplaterecognizer_c;
class licenseplaterecognizer implements licenseplaterecognizer_i
{
/**
* The configuration of the module
* @var licenseplaterecognizer_c
*/
public licenseplaterecognizer_c $config;
/**
* API URL
* @var string
*/
private string $api_url = 'https://api.platerecognizer.com/v1/plate-reader/';
public function __construct()
{
$this->config = new licenseplaterecognizer_c();
}
/**
* @inheritDoc
*/
public function requireModuleEnabled(): void
{
if (!(bool)$this->config->enabled->getVariableValue()) {
throw new Exception('licenseplaterecognizer module is not enabled.');
}
}
/**
* @inheritDoc
* @throws Exception If the module is not enabled or if there is an error in the API request
* @throws Exception If the API response cannot be parsed
*/
public function licenseplaterecognizer(string $base64_image): array
{
$image_processor = new image_processor();
//ADD PARAMETER IN REQUEST LIKE regions
$data = array(
'upload' => $base64_image,
'regions' => 'us-ca' // Optional
);
// Prepare new cURL resource
$ch = curl_init('https://api.platerecognizer.com/v1/plate-reader/');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_2TLS);
// Set HTTP Header for POST request
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"Authorization: Token " . $this->config->api_key->getVariableValue()
)
);
// Submit the POST request and close cURL session handle
$result = curl_exec($ch);
curl_close($ch);
$response_data = json_decode($result, true);
if (isset($response_data['results']) && count($response_data['results']) > 0) {
return [
'success' => true,
'license_plate_number' => $response_data['results'][0]['plate'] ?? null,
'confidence' => $response_data['results'][0]['score'] ?? null,
'raw_response' => $response_data,
];
} else {
return [
'success' => false,
'message' => 'No license plate detected.',
'raw_response' => $response_data,
];
}
}
}