In this episode, we tackle the final step of our cropped profile photo feature: making sure that the image gets saved properly when you submit the main form. Up until now, we had the image cropping working, but we hadn't fully wired up the saving process.
We start by dispatching the cropped image from the cropper component back to the main parent form. This involves sending the temporary file path as an event, and then handling that event in our parent form so we can integrate the new profile photo with the rest of the user's data.
To make this work, we need to convert the URL or path of the cropped image back into a TemporaryUploadedFile
so that Livewire treats it as if it were just a normal uploaded photo in the form. There's a bit of fiddling here—pulling in namespaces, making sure Livewire's Blade quirks don't trip us up—but soon enough, we've got the cropped photo behaving just like a regular upload.
With that in place, we simply move the file into the correct directory (making it public so users can see their new profile photo), update the user's info, and save it all as you'd expect. We test the whole flow: pick a file, crop, save, and confirm that the updated profile photo is properly referenced and visible after a page refresh.
As a final note, we point out that if you're dealing with uploads not tied to a larger form, you can skip the whole dispatch and file reconstruction bit—this workaround is just so everything plays nicely in our parent form with Livewire.
Next up, we'll add a bit of polish to the workflow, and then we’ll be all set with profile photo uploads!