Home > database >  NotWritableException: Can't write image data to path
NotWritableException: Can't write image data to path

Time:09-29

I wrote this to upload an image to s3 and I only get this error on my test server, on localhost profile picture upload successfully without getting an error. php.ini files are same on both localhost and testing environments. Also AWS Credentials given right.

On test server code execute into if (!File::exists($folderPath)) { To my knowledge I wrote that part. Here's the code.

                if ((strpos($profilePic, 'data:image/png;base64,') !== false)
                    || (strpos($profilePic, 'data:image/jpeg;base64,') !== false)) {

                    $img = Image::make($profilePic)->orientate();
                } else {
                    
                    $img = Image::make(base64_decode($profilePic))->orientate();
                }

                $original = 'avater.png';
                $small = 'avater_small.png';
                $medium = 'avater_medium.png';

                $folderPath = 'uploads/profile_pictures/'.$userId;

                $fullPath = $folderPath.'/'.$original;
                $fullPathSmall = $folderPath.'/'.$small;
                $fullPathMedium = $folderPath.'/'.$medium;
                
                if (!File::exists($folderPath)) {
                    File::makeDirectory($folderPath, 0775, true);
                }

                $smallImg = clone $img;
                $mediumImg = clone $img;

                if ($img->height() > $img->width()) {
                    // resize the image to a width of 300 and constrain aspect ratio          
                    $smallImg->resize(400, null, function ($constraint) {
                        $constraint->aspectRatio();
                    });

                    $mediumImg->resize(800, null, function ($constraint) {
                        $constraint->aspectRatio();
                    });
                } else {
                    // resize the image to a height of 200 and constrain aspect ratio
                    $smallImg->resize(null, 400, function ($constraint) {
                        $constraint->aspectRatio();
                    });

                    $mediumImg->resize(null, 400, function ($constraint) {
                        $constraint->aspectRatio();
                    });
                }

                $img->save($fullPath);
                $smallImg->save($fullPathSmall);
                $mediumImg->save($fullPathMedium);
                Storage::disk('s3')->put($fullPath, file_get_contents($profilePic), 'public');
                Storage::disk('s3')->put($fullPathMedium, file_get_contents($profilePic), 'public');
                Storage::disk('s3')->put($fullPathSmall, file_get_contents($profilePic), 'public');

                $user_repository = \App::make('App\Repositories\Contracts\UserRepositoryInterface');

                // $data = ['profile_image' => asset('uploads/profile_pictures/'.$userId.'/'.$original)];
                $data = ['profile_image' => '/uploads/profile_pictures/'.$userId.'/'.$original];

                $item_image = $user_repository->updateUser($userId,$data);

                return true;

CodePudding user response:

there is permission issue for your upload folder on your testing server. Please verify that because before upload to s3 bucket you are storing your image to your upload folder first.

  • Related