From d3c1e89a0394ae585baaaa1daa3ed2475637d6a0 Mon Sep 17 00:00:00 2001 From: philipp lang Date: Fri, 19 Jan 2024 18:16:57 +0100 Subject: [PATCH] Add media conversion for squared form header image format --- app/Form/Models/Form.php | 7 ++++++- app/Form/Resources/FormApiResource.php | 2 +- tests/Feature/Form/FormApiListActionTest.php | 2 +- 3 files changed, 8 insertions(+), 3 deletions(-) diff --git a/app/Form/Models/Form.php b/app/Form/Models/Form.php index 60eeccfb..d0f56e97 100644 --- a/app/Form/Models/Form.php +++ b/app/Form/Models/Form.php @@ -7,8 +7,10 @@ use Cviebrock\EloquentSluggable\Sluggable; use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; +use Spatie\Image\Manipulations; use Spatie\MediaLibrary\HasMedia; use Spatie\MediaLibrary\InteractsWithMedia; +use Spatie\MediaLibrary\MediaCollections\Models\Media; use Zoomyboy\MedialibraryHelper\DefersUploads; class Form extends Model implements HasMedia @@ -39,7 +41,10 @@ class Form extends Model implements HasMedia $this->addMediaCollection('headerImage') ->singleFile() ->maxWidth(fn () => 500) - ->forceFileName(fn (Form $model, string $name) => $model->slug); + ->forceFileName(fn (Form $model, string $name) => $model->slug) + ->registerMediaConversions(function (Media $media) { + $this->addMediaConversion('square')->fit(Manipulations::FIT_CROP, 400, 400); + }); } /** @var array */ diff --git a/app/Form/Resources/FormApiResource.php b/app/Form/Resources/FormApiResource.php index 843adac7..6de83073 100644 --- a/app/Form/Resources/FormApiResource.php +++ b/app/Form/Resources/FormApiResource.php @@ -31,7 +31,7 @@ class FormApiResource extends JsonResource 'config' => $this->config, 'slug' => $this->slug, 'dates' => $this->from->equalTo($this->to) ? $this->from->format('d.m.Y') : $this->from->format('d.m.Y') . ' - ' . $this->to->format('d.m.Y'), - 'image' => $this->getMedia('headerImage')->first()->getFullUrl(), + 'image' => $this->getMedia('headerImage')->first()->getFullUrl('square'), ]; } diff --git a/tests/Feature/Form/FormApiListActionTest.php b/tests/Feature/Form/FormApiListActionTest.php index eb935222..138eb89f 100644 --- a/tests/Feature/Form/FormApiListActionTest.php +++ b/tests/Feature/Form/FormApiListActionTest.php @@ -36,7 +36,7 @@ class FormApiListActionTest extends TestCase ->assertJsonPath('data.0.excerpt', 'fff') ->assertJsonPath('data.0.description', 'desc') ->assertJsonPath('data.0.slug', 'lala-2') - ->assertJsonPath('data.0.image', $form->getMedia('headerImage')->first()->getFullUrl()) + ->assertJsonPath('data.0.image', $form->getMedia('headerImage')->first()->getFullUrl('square')) ->assertJsonPath('data.0.dates', '05.05.2023 - 07.06.2023') ->assertJsonPath('data.0.from_human', '05.05.2023') ->assertJsonPath('data.0.to_human', '07.06.2023');