Optimizing / compressing image uploads before saving to disk (no media library)

Hi all I’ve got several optimisations I want to apply to images uploaded via the upload field before they’re saved to storage. I don’t need help with the optimisations themselves. What I need to figure out is how to apply things to the uploaded file before it’s saved from Livewire’s temporary storage to my S3 disk. Any ideas? I’m not using any media library plugins so I need to work with the files directly.
Solution:
GitHub
image-optimizer/src/Components/BaseFileUpload.php at 4d4ba6a6393153...
🎨 Optimize your Filament images before they reach your database. - joshembling/image-optimizer
Jump to solution
4 Replies
Lara Zeus
Lara Zeus2mo ago
maybe can look into this and how they doing it! #joshembling-image-optimizer
binaryfire
binaryfire2mo ago
@Lara Zeus Saw that one. It’s using Spatie Media Library. That’s lots of code I’d need to dig through to use it as a reference - including deep diving into the Spatie package and the Filament integration for it. I’m definitely not opposed to source diving, but it’s a huge amount of work for what (I think?) must have an easy solution. Eg some way to access the path of uploads while they’re still in Livewire’s tmp dir and process them before they are moved from there to the disk?
Solution
Lara Zeus
Lara Zeus2mo ago
I think its all starting here https://github.com/joshembling/image-optimizer/blob/4d4ba6a63931533a4b8a2b6291fbd8753e741d69/src/Components/BaseFileUpload.php#L189C16-L189C37 in saveUploadedFileUsing you can do whatever you need
if ($compressedImage) {
Storage::disk($component->getDiskName())->put(
$component->getDirectory() . '/' . $filename,
$compressedImage->getEncoded()
);

return $component->getDirectory() . '/' . $filename;
}
if ($compressedImage) {
Storage::disk($component->getDiskName())->put(
$component->getDirectory() . '/' . $filename,
$compressedImage->getEncoded()
);

return $component->getDirectory() . '/' . $filename;
}
btw the plugin dose works with native file upload 🙂
GitHub
image-optimizer/src/Components/BaseFileUpload.php at 4d4ba6a6393153...
🎨 Optimize your Filament images before they reach your database. - joshembling/image-optimizer
binaryfire
binaryfire2mo ago
Nice. saveUploadedFileUsing is what I needed. It’s undocumented, but now I know what I look for in the repo. Thanks!