53 lines
1.7 KiB
PHP
53 lines
1.7 KiB
PHP
<?php
|
|
|
|
namespace App\Services\Piantine;
|
|
|
|
use App\Models\TerritorioMappa;
|
|
use Illuminate\Support\Facades\Http;
|
|
use Illuminate\Support\Facades\Storage;
|
|
use RuntimeException;
|
|
|
|
/**
|
|
* Client del container `map-renderer`: gli passa la geometria di un
|
|
* territorio, riceve il PNG ad alta definizione e lo archivia sul disco
|
|
* pubblico accanto a PDF e miniature.
|
|
*/
|
|
class PiantinaRenderer
|
|
{
|
|
public function render(TerritorioMappa $mappa): string
|
|
{
|
|
$geometry = $mappa->geometria();
|
|
|
|
if ($geometry === null) {
|
|
throw new RuntimeException('La geometria salvata per questo territorio non è leggibile: ridisegna il poligono.');
|
|
}
|
|
|
|
$response = Http::timeout((int) config('piantine.renderer_timeout'))
|
|
->post(rtrim((string) config('piantine.renderer_url'), '/') . '/render', [
|
|
'geometry' => $geometry,
|
|
'marginRatio' => (float) config('piantine.margin_ratio'),
|
|
'maxSide' => (int) config('piantine.max_side'),
|
|
]);
|
|
|
|
if (! $response->successful()) {
|
|
$dettaglio = $response->json('error') ?? ('HTTP ' . $response->status());
|
|
throw new RuntimeException('Il renderer delle mappe ha risposto con un errore: ' . $dettaglio);
|
|
}
|
|
|
|
$png = $response->body();
|
|
if ($png === '' || ! str_starts_with($png, "\x89PNG")) {
|
|
throw new RuntimeException('Il renderer delle mappe non ha restituito un PNG valido.');
|
|
}
|
|
|
|
$path = 'territori-mappe/territorio-' . $mappa->territorio_id . '.png';
|
|
Storage::disk('public')->put($path, $png);
|
|
|
|
$mappa->update([
|
|
'image_path' => $path,
|
|
'rendered_at' => now(),
|
|
]);
|
|
|
|
return $path;
|
|
}
|
|
}
|