2

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.

Sham
  • 23
  • 9
  • There is a package which I recommend it a-lot it will help you to store more than conversion of the photo and there are a lot of features The name of the page is Laravel-medialibrary from Spatie and I will leave the link below https://spatie.be/docs/laravel-medialibrary/v9/introduction – Wael Khalifa Jan 31 '22 at 19:43
  • Take reference from question [Laravel 5.6: Create image thumbnails](https://stackoverflow.com/questions/50451911/laravel-5-6-create-image-thumbnails) – bhucho Feb 01 '22 at 05:43
  • 1
    Does this answer your question? [Laravel 5.6: Create image thumbnails](https://stackoverflow.com/questions/50451911/laravel-5-6-create-image-thumbnails) – bhucho Feb 01 '22 at 05:44

1 Answers1

0

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.

Humble Hermit
  • 125
  • 1
  • 8