0
votes

I am having the hardest time resizing an image with Laravel Image Intervention. I am able to name and save images normally, but when I add image intervention it won't save the new file created to the folder.

Here is what I have in my controller

 //This all works
        $title = str_slug(request('title'));
        $filenameWithExt = $request->file('cover_image')->getClientOriginalName();
        $filename = pathinfo($filenameWithExt, PATHINFO_FILENAME);
        $extension = $request->file('cover_image')->getClientOriginalExtension();
        $fileNameToStore= $title.'.'.$extension;
        $thumbnailpic= 'thumb'.'-'.$fileNameToStore;
//store image
        $path = $request->file('cover_image')->storeAs('public/cover_images', $fileNameToStore);

//Here is where I am trying to resize and it breaks

        $file = Input::file('cover_image');
        Image::make( $file->getRealPath() )->fit(340, 340)->save('public/cover_images/' . $thumbnailpic);

This is the error I get

"Can't write image data to path (public/cover_images/thumb-imagename.png)"

If I remove the two lines of resize code everything works perfectly. I am running this locally and have made everything open completely for permissions. Not sure what else to do. Thanks!

2
Does the directory cover_images exist?Rwd
it does. Laravel itself has no trouble creating the folder and saving there, it seems like image intervention is having the issueChris Grim
->save('public/cover_images/' . $thumbnailpic), for Intervention I believe this path starts in the Linux root.user2094178
What does that mean?Chris Grim

2 Answers

0
votes

Problem seems related save path, Can you try give full path like this,

$targetPath = storage_path().'/app/public/cover_images/';
...
->save($targetPath . $thumbnailpic);

I hope this will help

0
votes

So I finally figured it out. I was saving to the public_path which image intervention obviously didn't like. So I used the code below

    $source = storage_path().'/app/public/cover_images/'.$fileNameToStore;
    $target = storage_path().'/app/public/cover_images/' . $thumbnailpic;


    Image::make($source)->fit(140, 140)->save($target);

and it worked!