Ability to optimize/replace the original image #3447
-
I've been using Laravel Media Library a lot and it's super useful. But, I noticed we can't optimize the original images we upload. Right now, we can only do stuff to the extra images, not the main one we upload. This can be a bit of a pain, especially with large images that take up lots of space and slow down loading times. What I'm Thinking: Why It's Cool: Websites would load faster, which is always great. |
Beta Was this translation helpful? Give feedback.
Replies: 5 comments 1 reply
-
Yes same issue here; adding conversions seems to only apply to new copies of the original image. So I cannot for instance tell spatie-media to save the original image as I guess you could do some pruning tasks behind the scenes that rescales them but it is kinda odd that you cannot define conversions for the original upload (before it is being stored). |
Beta Was this translation helpful? Give feedback.
-
To me it seems quite trivial to do these sorts of optimisations before adding the file to the media library. Image::load($pathToImage)->fit(Fit::Max, 2000, 2000)->optimize()->save(); See: |
Beta Was this translation helpful? Give feedback.
-
Ah I'm using the Filament wrapper here https://filamentphp.com/plugins/filament-spatie-media-library but it seems I only have access to the manipulations i.e. https://spatie.be/docs/laravel-medialibrary/v11/advanced-usage/storing-media-specific-manipulations. But these only apply to the "conversions" like thumb/sm/lg and not to the root file (I've tried the The filament wrapper is literally doing the following: // Spatie\MediaLibrary\MediaCollections\FileAdder
$mediaAdder = $record->addMediaFromString($file->get());
$filename = $component->getUploadedFileNameForStorage($file);
$media = $mediaAdder
->addCustomHeaders($component->getCustomHeaders())
->usingFileName($filename)
->usingName($component->getMediaName($file) ?? pathinfo($file->getClientOriginalName(), PATHINFO_FILENAME))
->storingConversionsOnDisk($component->getConversionsDisk() ?? '')
->withCustomProperties($component->getCustomProperties())
->withManipulations($component->getManipulations())
->withResponsiveImagesIf($component->hasResponsiveImages())
->withProperties($component->getProperties())
->toMediaCollection($component->getCollection(), $component->getDiskName()); @freekmurze Is there a way to make this work through the FileAdder or perhaps a method on an Eloquent model? My use case is to decrease the size of big items like 5000x5000 pixels. I could work around it by decreasing the file afterwards using an event hook (like |
Beta Was this translation helpful? Give feedback.
-
@freekmurze, you are right; optimizing images before adding them to the media library is straightforward. However, I find it slightly odd to have to do this everywhere. Your extension is amazing, and I believe a cleaner approach would be to configure those settings directly in the registerMediaCollections method. Letting the media library handle everything centrally will aid in the future maintenance of the code since everything is consolidated in one place. |
Beta Was this translation helpful? Give feedback.
-
How can you remove the original after saving without using crutches? |
Beta Was this translation helpful? Give feedback.
To me it seems quite trivial to do these sorts of optimisations before adding the file to the media library.
See:
https://spatie.be/docs/image/v3/image-manipulations/resizing-images
https://spatie.be/docs/image/v3/image-manipulations/optimizing-images