Add media conversion for squared form header image format

This commit is contained in:
philipp lang 2024-01-19 18:16:57 +01:00
parent 6ede944d85
commit d3c1e89a03
3 changed files with 8 additions and 3 deletions

View File

@ -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<int, string> */

View File

@ -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'),
];
}

View File

@ -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');