feat(attachment): refactor image cropping functionality to use range objects and update related tests

This commit is contained in:
2026-08-18 12:54:47 -03:00
parent 58cc35fd61
commit 9cfd49f233
16 changed files with 496 additions and 54 deletions

View File

@@ -29,12 +29,35 @@ class WebsiteExtraResource extends JsonResource
fn (Attachment $attachment): string => $attachment->key
),
'resolved_config' => $this->formatConfig(
$this->resolvedConfig(),
$this->resolvedAdminConfig(),
fn (Attachment $attachment): string => $attachment->getTemporaryUrl(1440)
),
];
}
private function resolvedAdminConfig(): mixed
{
$config = $this->resolvedConfig();
if (
$this->websiteTypeExtra->codigo !== 'heroConfig'
|| ! is_array($config)
|| ! ($config['background_image_id'] ?? null) instanceof Attachment
) {
return $config;
}
$attachment = $config['background_image_id'];
$fullRange = ['start_percentage' => 0.0, 'end_percentage' => 100.0];
$config['background_image_id'] = [
'url' => $attachment->getTemporaryUrl(1440),
'crop_horizontal' => $attachment->crop_horizontal ?? $fullRange,
'crop_vertical' => $attachment->crop_vertical ?? $fullRange,
];
return $config;
}
private function formatConfig(mixed $value, callable $formatAttachment): mixed
{
if ($value instanceof Attachment) {

View File

@@ -45,14 +45,44 @@ class WebsiteExtrasResource extends JsonResource
),
]),
'resolved_extras' => $websiteExtras->mapWithKeys(fn ($extra) => [
$extra->websiteTypeExtra->codigo => $this->formatConfig(
$extra->resolvedConfig(),
fn (Attachment $attachment): string => $attachment->getTemporaryUrl(1440)
),
$extra->websiteTypeExtra->codigo => $this->formatResolvedConfig($extra),
]),
];
}
private function formatResolvedConfig(mixed $extra): mixed
{
$config = $extra->resolvedConfig();
if (
$extra->websiteTypeExtra->codigo === 'heroConfig'
&& is_array($config)
&& ($config['background_image_id'] ?? null) instanceof Attachment
) {
$attachment = $config['background_image_id'];
$config['background_image_id'] = $this->formatHeroAttachment($attachment);
}
return $this->formatConfig(
$config,
fn (Attachment $attachment): string => $attachment->getTemporaryUrl(1440)
);
}
/**
* @return array{url: string, crop_horizontal: array<string, float>, crop_vertical: array<string, float>}
*/
private function formatHeroAttachment(Attachment $attachment): array
{
$fullRange = ['start_percentage' => 0.0, 'end_percentage' => 100.0];
return [
'url' => $attachment->getTemporaryUrl(1440),
'crop_horizontal' => $attachment->crop_horizontal ?? $fullRange,
'crop_vertical' => $attachment->crop_vertical ?? $fullRange,
];
}
private function formatConfig(mixed $value, callable $formatAttachment): mixed
{
if ($value instanceof Attachment) {

View File

@@ -98,7 +98,7 @@ class TenantResource extends JsonResource
private function formatExtraConfig(mixed $value): mixed
{
if ($value instanceof Attachment) {
return $value->getTemporaryUrl(1440);
return ($value->croppedAttachment ?? $value)->getTemporaryUrl(1440);
}
if (! is_array($value)) {

View File

@@ -81,6 +81,7 @@ class TenantInformationService
$attachments = Attachment::query()
->whereIn('id', $attachmentIds)
->with('croppedAttachment')
->get()
->keyBy('id');

View File

@@ -5,6 +5,7 @@ namespace App\Domains\Tenant\Services;
use App\Domains\Attachable\Enums\AttachmentType;
use App\Domains\Attachable\Models\Attachment;
use App\Domains\Attachable\Services\AttachmentService;
use App\Domains\Shared\Rules\CroppedImageOrBase64Rule;
use App\Domains\Shared\Rules\ImageOrBase64Rule;
use App\Domains\Tenant\Models\Tenant;
use App\Domains\Tenant\Models\WebsiteExtra;
@@ -222,9 +223,11 @@ class WebsiteExtraService
$compiled = is_string($rules) ? explode('|', $rules) : $rules;
return array_map(
fn (mixed $rule): mixed => $rule === 'image_or_base64'
? new ImageOrBase64Rule
: $rule,
fn (mixed $rule): mixed => match ($rule) {
'image_or_base64' => new ImageOrBase64Rule,
'cropped_image_or_base64' => new CroppedImageOrBase64Rule,
default => $rule,
},
$compiled
);
}
@@ -331,8 +334,12 @@ class WebsiteExtraService
);
}
if (is_string($value) && Str::isUuid($value)) {
$attachment = Attachment::query()->where('key', $value)->first();
$cropHorizontal = is_array($value) ? $value['crop_horizontal'] ?? null : null;
$cropVertical = is_array($value) ? $value['crop_vertical'] ?? null : null;
$image = is_array($value) ? $value['image'] ?? null : $value;
if (is_string($image) && Str::isUuid($image)) {
$attachment = Attachment::query()->where('key', $image)->first();
if (! $attachment) {
throw ValidationException::withMessages([
@@ -341,9 +348,24 @@ class WebsiteExtraService
],
]);
}
if (is_array($cropHorizontal) && is_array($cropVertical)) {
$attachment = $this->attachmentService->updateImageCrop(
$attachment,
$cropHorizontal,
$cropVertical,
);
}
} elseif (is_array($cropHorizontal) && is_array($cropVertical)) {
$attachment = $this->attachmentService->storeCroppedImage(
$image,
"tenants/{$tenant->codigo}/extras/{$definition->codigo}",
$cropHorizontal,
$cropVertical,
);
} else {
$attachment = $this->attachmentService->store(
$value,
$image,
"tenants/{$tenant->codigo}/extras/{$definition->codigo}"
);
}