public function commercialStore(Shop $shop, Request $request) { $this->validate($request, ['title' => 'required', 'url' => 'required', 'cropper_json' => 'required', 'inputImage' => 'required|image']); $user = Auth::user(); $file = $request->file('inputImage'); $input = $request->only('title', 'url'); $data = $request->input('cropper_json'); $data = json_decode(stripslashes($data)); $imageName = $shop->id . str_random(20) . '.' . $file->getClientOriginalExtension(); $file->move(public_path() . '/img/files/shop/' . $shop->id . '/', $imageName); $src = public_path() . '/img/files/shop/' . $shop->id . '/' . $imageName; $real_name = $file->getClientOriginalName(); $size = $file->getClientSize() / (1024 * 1024); //calculate the file size in MB $img = Image::make($src); $img->rotate($data->rotate); $img->crop(intval($data->width), intval($data->height), intval($data->x), intval($data->y)); $img->resize(420, null, function ($constraint) { $constraint->aspectRatio(); }); $img->save($src, 90); $user->usage->add(filesize(public_path() . '/img/files/shop/' . $shop->id . '/' . $imageName) / (1024 * 1024)); // storage add $commercial = $shop->commercials()->create($input); $commercial->file()->create(['user_id' => $user->id, 'real_name' => $real_name, 'name' => $shop->id . '/' . $imageName, 'size' => $size]); return redirect()->back(); }