From 3d5740ae4ca6a8aa5a9c80a0c8c3ea27964fbc32 Mon Sep 17 00:00:00 2001 From: Lupu Gheorghe Date: Thu, 2 May 2024 12:46:27 +0300 Subject: [PATCH] fix gallery --- app/Http/Resources/Project/ShowProjectResource.php | 2 ++ app/Services/ProjectService.php | 11 +++++++++-- resources/js/Components/Gallery.vue | 1 + 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/app/Http/Resources/Project/ShowProjectResource.php b/app/Http/Resources/Project/ShowProjectResource.php index cc9cf397..33d40577 100644 --- a/app/Http/Resources/Project/ShowProjectResource.php +++ b/app/Http/Resources/Project/ShowProjectResource.php @@ -51,6 +51,8 @@ public function toArray(Request $request): array return [ 'src' => $media->getFullUrl(), 'thumbnail' => $media->getFullUrl('preview'), + 'width' => $media->getCustomProperty('width') ?? '100%', + 'height' => $media->getCustomProperty('height') ?? '100%', ]; })->toArray(), 'is_active' => $this->is_active, diff --git a/app/Services/ProjectService.php b/app/Services/ProjectService.php index 53dd1ac4..3b584e64 100644 --- a/app/Services/ProjectService.php +++ b/app/Services/ProjectService.php @@ -41,9 +41,16 @@ private static function updateGallery(Project $project, ?array $value): void $media->delete(); } }); - collect($value)->filter(fn ($image) => ! \is_array($image)) - ->map(fn ($image) => $project->addMedia($image)->toMediaCollection('gallery')); + ->map(function ($image) use ($project) { + $tmpImage = Image::load($image->getPathname()); + + $width = $tmpImage->getWidth(); + $height = $tmpImage->getHeight(); + $project->addMedia($image) + ->withCustomProperties($width || $height ? compact('width', 'height') : []) + ->toMediaCollection('gallery'); + }); } public function create(array $data): Project|GalaProject diff --git a/resources/js/Components/Gallery.vue b/resources/js/Components/Gallery.vue index 7799cd14..91a7ff1b 100644 --- a/resources/js/Components/Gallery.vue +++ b/resources/js/Components/Gallery.vue @@ -10,6 +10,7 @@ rel="noreferrer" > + {{ image }}