Create and upload thumbnail on the fly in FIlament
Hello,
I am using laravel 10 and I have just installed Filament.
When creating a new Post, I would like to be able to upload an image, while creating a second smaller version of it on the fly (thumbnail). Is there an option to do this in Filament?
Here is my code for the single image Upload:
Yes, you can create a thumbnail on the fly in Filament using the imageResizeTargetWidth and imageResizeTargetHeight methods. You can simply add another FileUpload component for the thumbnail and set the target width and height accordingly. Here's an example:
```FileUpload::make('image')
->label('Feature Image')
->image()
->directory('blogs')
->nullable()
->columnSpan(12)
->saveUploadedFileUsing(function (TemporaryUploadedFile $file, $record) {
$handler = new class {
use \App\Traits\HandlesImageOptimization;
};
$result = $handler->handleAndOptimizeUpload($file, 'blogs');
if (is_array($result)) {
// Store it in static variable for use during saving
Blog::$tempImageData = $result;
return $result['image'];
}
return $result;
})```