Saving Images in two size in Laravel Livewire

Viewed 284

I have a form where I upload images using Laravel Livewire.

In my class I can save the files in original size using:

$filenames = collect($this->photos)->map->store('posts');

The issue that I faced is that I don't know how to save the same images (with same name as above) in another folder (thumbnail folder) in another size. For instance, width 200px.

1 Answers

I have used this with Laravel 8 and it works. I can't see what you have before this code but you can iterate through your photos and save 2 images, one in the parent folder and one resized in a "thumbnail" folder;

$ext = $image->getClientOriginalExtension();
$date = new Carbon;
$folder = "photos";

// Save original file
$orig_filename = $date->format('YmdHisv') . '.' . $ext;
$img_url = $image->storeAs('public/' . $folder, $orig_filename);

// Create and store thumbnail
$thumbnail = $image->storeAs('public/' . $folder . '/thumbnails', $orig_filename);
$thumb_path = Storage::path('public/' . $folder . '/thumbnails/' . $orig_filename);
$thumb_img = Image::make($thumb_path)->resize(150, 93, function ($constraint) {
        $constraint->aspectRatio();
});
$thumb_img->save($path);

This is the package Intervention\Image and here is a great tutorial on that.

Related