sham's avatar
Level 1

Saving Images in two size in Laravel Livewire

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.

0 likes
7 replies
sham's avatar
Level 1

@tykus I used intervention as below and it doesn't work. Meanwhile, I don't know how to save the original file as well with the same name as the thumbnail:

error:

Intervention\Image\Exception\NotWritableException
Can't write image data to path (thumbnails/2022-02-01 00:30:48.png)

Code:

foreach ($this->photos as $photo){
	$img = Image::make($photo);
    $img->resize(320, 240);
    $img->save('/thumbnails/'.now().'.png');
}


tykus's avatar

@sham Can't write image data to path (thumbnails)

Where is thumbnails relative to your project root directory? You can use public_path or storage_path helper methods to generate the correct path, e.g.:

$img->save(storage_path('/thumbnails/'.now().'.png'));
sham's avatar
Level 1

@tykus I use symlink and I get this error:

Intervention\Image\Exception\NotWritableException
Can't write image data to path (C:\xampp\htdocs\test\storage\/thumbnails/2022-02-01 00:51:57.png)
tykus's avatar

@sham does that directory (C:\xampp\htdocs\test\storage\/thumbnails/) exist; and is writable by the web server?

sham's avatar
Level 1

@tykus I changed it to below and it works:

            $img->save(storage_path('app/public/thumbnails/'.now().'.png'));

Now how can I save the files with the same name as thumbnails in the original folder where I store the photos without resizing.

I did something like below and it saved one image with different name in thumbnails and originals folders.

        foreach ($this->photos as $photo){

            $img2 = Image::make($photo);
            $img2->save(storage_path('app/public/originals /'.now().'.png'));

            $img = Image::make($photo);
            $img->resize(300,300);
            
            $img->save(storage_path('app/public/thumbnails/'.now().'.png'));
        }
tykus's avatar

@sham set the filename in a variable so that it is the same for both:

foreach ($this->photos as $photo){
    $filename = now();
    $img2 = Image::make($photo);
    $img2->save(storage_path('app/public/originals /'.$filename.'.png'));
    $img = Image::make($photo);
    $img->resize(300,300);
    $img->save(storage_path('app/public/thumbnails/'.$filename.'.png'));
}

Please or to participate in this conversation.