3

This is how I'm trying to use image intervention package:

public function saveImage(string $path, $image)
{
        $file = $image;

        $filename = Str::random(20) . $file->getClientOriginalName();

        $imgResize = Image::make($image);
        $imgResize->heighten(300);

        //dd(is_writable(storage_path('app/public/'.$path)));

        $imgResize->save(storage_path('app/public/'.$path));


        return $destination_path = Storage::disk('local')->put('/'.$path, $image);    
}

The image is saved successfully without using image intervention. When I try to save() it it will throw me this error:

Intervention\Image\Exception\NotWritableException: Can't write image data to path.

I have already changed the permissions of the folder, but still not working.

Any idea what is causing this?

Edit:

I did earlier php artisan storage:link and that's why it works without using image intervention.

I did dd(storage_path('app/public/'.$path)); which results in the same folder in which the normal images are saved successfully.

I also checked if is_writable and the result is true.

Just for testing purposes I already changed the permissions to 777.

I don't know what else to do to make this work.

Marinario Agalliu
  • 989
  • 10
  • 25
  • what's in the $path ? – N69S Jan 21 '21 at 10:38
  • The name of the folder : `review_images` – Marinario Agalliu Jan 21 '21 at 10:47
  • If I try to save it to somewhere else like : `$imgResize->save(storage_path('app/public/'.$filename));` it works. If I try to save it to `$path` it will throw the same error again. The problem is that I'm using this method inside a service which works for many other services/controllers and I get the `$path` dynamically so I cannot do this: `$imgResize->save(storage_path('app/public/folder_name'));` – Marinario Agalliu Jan 21 '21 at 10:57

2 Answers2

3

add the file name in the save() method

$imgResize->save(storage_path('app/public/'.$path.'/'.$filename));

here some examples from the documentation of intervention site

// open and resize an image file $img = Image::make('public/foo.jpg')->resize(300, 200);

// save file as jpg with medium quality $img->save('public/bar.jpg', 60);

// save the same file as jpg with default quality $img->save('public/baz.jpg');

// save the file in png format $img->save('public/bar.png');

// save the image jpg format defined by third parameter $img->save('public/foo', 80, 'jpg');

N69S
  • 16,110
  • 3
  • 22
  • 36
0

the save method actually needs absolute path or path relative to the file in which the method is used. so give a filename, with all parent folders already created. use storage_path(), public_path(), or base_path() (or other method laravel provides). or any method from core php.

rajeshtva
  • 410
  • 6
  • 18