A
A
Analka2019-11-07 23:17:03
Laravel
Analka, 2019-11-07 23:17:03

Process Image Blob?

I crop the image before uploading via cropper.js I send it to the server

cropResize() {
      let initialAvatarURL;
      let canvas;
      const that = this;
      if (this.cropper) {
        $.magnificPopup.close();
        canvas = this.cropper.getCroppedCanvas({
          width: 160,
          height: 160,
        });

        canvas.toBlob(function(blob) {
          const formData = new FormData();
          formData.append('image', blob);
          const config = {
            'content-type': 'multipart/form-data'
          }
          axios.post('/cabinet/user/avatar/upload', formData, config)
            .then((response) => {
              if (response.data.error === 0) {
                that.profile.avatar = '/storage/' + response.data.attributes.avatar;
                $('.img__avatar').css('background-image', 'url(/storage/' + response.data.attributes.avatar + ')');
                that.loading = false;
                toast.fire({
                  type: 'success',
                  title: response.data.message
                });
                that.loading = false;
              } else {
                toast.fire({
                  type: 'error',
                  title: response.data.message
                });
                that.loading = false;
              }
            })
            .catch(error => {});
        });
      }
    }

I get this in the controller
array:1 [
  "image" => UploadedFile {#463
    -test: false
    -originalName: "blob"
    -mimeType: "image/png"
    -error: 0
    #hashName: null
    path: "/tmp"
    filename: "phpT8KBGL"
    basename: "phpT8KBGL"
    pathname: "/tmp/phpT8KBGL"
    extension: ""
    realPath: "/tmp/phpT8KBGL"
    aTime: 2019-11-07 20:06:58
    mTime: 2019-11-07 20:06:58
    cTime: 2019-11-07 20:06:58
    inode: 925492
    size: 78858
    perms: 0100600
    owner: 525370
    group: 601
    type: "file"
    writable: true
    readable: true
    executable: false
    file: true
    dir: false
    link: false
  }
]

how do i save the image?
I try this but it doesn't work
public function uploadavatar(Request $request){
        if ($request->hasFile('image')){
            $image = $request->file('image');
            $height = Image::make($request->file('image'))->height();
            $width = Image::make($request->file('image'))->width();
            if ($height < 60 && $width < 60){
                return response()
                    ->json([
                        'message' => Lang::get('profile.size_avatar'),
                        'error' => 1
                    ], 200);
            }

            if (empty($image->getClientOriginalExtension())) $ext = 'jpg'; else $ext = $image->getClientOriginalExtension();

            $fileName = Auth::user()->id.'.'.$ext;
            if (!empty($fileName)){
                Storage::delete('uploads/avatars/'.Auth::user()->attributes->avatar);
                Storage::disk('public')->put('uploads/avatars/'.$fileName, file_get_contents($image));
            }
            $avatar  = 'uploads/avatars/'.$fileName;
        }else $avatar = Auth::user()->attributes->avatar;

        $attributes = UserAttribute::where('user_id',Auth::user()->id)->update([
            'avatar' => $avatar
        ]);

        if ($attributes) return response()
            ->json([
                'message' => Lang::get('profile.update_success'),
                'attributes' => UserAttribute::where('user_id',Auth::user()->id)->first(),
                'error' => 0,
            ], 200);
    }

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question