Search code examples
laraveleloquentpostmanlaravel-8image-upload

error during upload and resize file in laravel when tested using postman


I'm using Postman ver 0.12.1 plugin in Visual Studio Code to test the uploading and resizing image file but I'm getting an error that I don't know how to fix. can anyone point me to the right direction?

this is the error message I got on Postman:

error message

this is the form-data I sent to test the API:

enter image description here

this is my GalleryRepository code:

    public function create(array $data)
    {
        try{
            return DB::transaction(function()use($data){
                // Define the maximum width and height
                $maxWidth = 1000;
                $maxHeight = 1000;
                // resize the image while maintaining the aspect ratio
                $data['name'] = request('name')->resize($maxWidth, $maxHeight, function($constraint) {
                    $constraint->aspectRatio();
                    $constraint->upsize(); // prevent the image from upscaling
                });

                $subfolder = date('Y') . '/' . date('m');
                $img_path = "assets/img/{$data['type']}/gallery/{$subfolder}";
                $data['name'] = $data['name']->store($img_path, 'public');
                return Gallery::create($data);
            });
        }catch(Exception $error){
            $message = $error->getMessage()??"INSERT_ERR";

            /**
             * known code error db
             * 23000 duplicate entry
             * @see https://dev.mysql.com/doc/mysql-errors/8.0/en/server-error-reference.html
             */
            if($error->getCode()==='23000'){
                $message = "ERR_DUPLICATE_ENTRY";
            }

            return ['status'=>'ERR','message'=>$error];
        }
    }

the StoreGalleryRequest file only have 1 rule right now:

    public function rules(): array
    {
        return [
            'name' => 'required|image|mimes:jpg,png',
        ];
    }

    protected function passedValidation(): void
    {
        $data = $this->validated();

        $this->replace($data);
    }

Solution

  • to call resize(), try to use library Intervention/Image

    composer require intervention/image

    and here is example code of GalleryRepository:

    try{
        //..
            $image = Image::make(request('name'));
    
            // resize the image while maintaining aspect ratio
            $image->resize($maxWidth, $maxHeight, function ($constraint) {
                $constraint->aspectRatio();
                $constraint->upsize(); // prevent the image from upscaling
            });
    
            $subfolder = date('Y') . '/' . date('m');
            $img_path = "assets/img/{$data['type']}/gallery/{$subfolder}";
            $imageName = uniqid() . '.jpg'; // generate a unique filename
            $image->save(public_path("$img_path/$imageName"));
    
            $data['name'] = "$img_path/$imageName";
    
            return Gallery::create($data);
        })
    }