Manipulation on uploaded image before save
As a a bit advanced step (for me) in learning Filament I wanted to build a system which will a new article for a imaginary news website. Made few tables (articles and article_images), added what was needed in form builder, added upload field and everything worked fine (with a bit of tweeking ofc).
But the part I am stuck now is that image upload.
I want the system to be able to
1) resize image to size(s) I need (validation I think I can get on my own)
2) rename the file to in my case, title of the article
3) use that filename as a filename in table article_images
I tried some examples i found with ->beforeSave (completely not working for me), found in the manual that upload itself can do resizing (Cropping and resizing images) but I wanted to do some more complex things later. Any help, tutorial, article would be nice since I wasn't been able to find any, and this is my begining in Filament so have mercy π
7 Replies
2) you can generate custom file name -> https://filamentphp.com/docs/3.x/forms/fields/file-upload#generating-custom-file-names
using a package like media library can do a lot of the image processing for you
Thx Dan π but I have problem to figure out on how and where to call methods/packages
Like in my case I need to get the uploaded file, resize it to 2 different sizes, save both of them with slug name, get those names as vars and put them in database
Last night I did some reading and found out that I can use
mutateFormDataBeforeCreate
and mutateFormDataBeforeSave
one on crate page, other on edit. But still struggling on a bit. I will try to find out answers today from reading the manualsIf you read the media library package docs, you will see that you can register the conversions in the model based on a particular collection and everything else will happen automatically
Will try it today @Dan Harrin and if I fail π¦ will ask again
take a step back and think about easier ways to accomplish what you are doing
this package wont give you the exact result you described with article_images etc, but its probably a better path
You were thinking on Spatie Media Library ?