Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot save image intervention image. NotWritableException in Image.php line 138

Im trying to save a manipulated image which i will them push to s3.

My code that works This code saves the image directly within the public folder*

public function store(Filesystem $filesystem)
{

    $request = Input::all();

    $validator = Validator::make($request, [
        'images' => 'image'
    ]);

    if ($validator->fails()) {
        return response()->json(['upload' => 'false']);
    }

    $postId = $request['id'];

    $files = $request['file'];

    $media = [];

    $watermark = Image::make(public_path('img/watermark.png'));

    foreach($files as $file) {

        $image = Image::make($file->getRealPath());
        $image->crop(730, 547);
        $image->insert($watermark, 'center');
        $image->save($file->getClientOriginalName());

    }

}

What i would like to achieve is to be able to save it within a folder of it's own. Firstly what is the best place to store an image for a blog post, within the storage of public folder? But anyway when i do this:

$image->save('blogpost/' . $postId . '/' . $file->getClientOriginalName());

// Or this

$image->save(storage_path('app/blogpost/' . $postId . '/' . $file->getClientOriginalName()));

I get the error:

folder within public

NotWritableException in Image.php line 138: Can't write image data to path (blogpost/146/cars/image.jpg)

or

storage path

NotWritableException in Image.php line 138: Can't write image data to path /code/websites/blog/storage/app/blogpost/146/image.jpg

I've tried

cd storage/app/  
chmod -R 755 blogpost

And it still wont work

Thank you for reading this

like image 721
arkhamDev Avatar asked Jul 03 '15 04:07

arkhamDev


4 Answers

Ok so here is how i solved it, I made the directory first before storing,

Storage::disk('local')->makeDirectory('blogpost/' . $postId);

Once the folder is created i then go on to store the manipulated images like so:

$image->save(storage_path('app/blogpost/' . $postId . '/' . $imageName));

And then pushing the image to S3

$filesystem->put('blogpost/' . $postId . '/' . $imageName, file_get_contents(storage_path('app/blogpost/' . $postId . '/' . $imageName)));

This worked

like image 65
arkhamDev Avatar answered Sep 30 '22 04:09

arkhamDev


You can solve it by casting the Intervation/Image variable to a data stream using function stream. Then use the Storage Laravel facade to save the image.

$img = Image::make('path-to-the-image.png')->crop(...)->insert->stream('jpg', 90)
Storage::put('where_I_want_the_image_to_be_stored.jpg', $img);
like image 28
Tomas Dohnal Avatar answered Sep 30 '22 04:09

Tomas Dohnal


In my case I migrated a project from Windows 10 to Parrot (Debian-Linux) and I had the same problem and turned out the slashes were backward slashes and Linux interpret them differently. Unlike Windows, it doesn't really matter.

//Windows Code: 
 $image_resize->save(public_path('\storage\Features/' .'Name'.".".'png'));

//Linux Code (Working):
 $image_resize->save(public_path('storage/Features/' .'Name'.".".'jpg'));
like image 20
Abdelsalam Shahlol Avatar answered Sep 30 '22 05:09

Abdelsalam Shahlol


Laravel 5 needs permission to write to entire Storage folder so try following,

sudo chmod 755 -R storage

if 755 dont work try 777.

like image 39
pinkal vansia Avatar answered Sep 30 '22 04:09

pinkal vansia