model); $model = $model::find($request->id); $file = $request->file('file'); $fileName = pathinfo($file->getClientOriginalName(), PATHINFO_FILENAME); $extension = strtolower(pathinfo($file->getClientOriginalName(), PATHINFO_EXTENSION)); $isImage = in_array($extension, ['jpg', 'jpeg', 'png']); $tempName = md5($fileName . time()); $newFileName = $tempName . '.' . $extension; if ($isImage) { // File System stuff $img = Image::make($file->getRealPath()); $img->resize(1200, 1200, function ($constraint) { $constraint->aspectRatio(); $constraint->upsize(); }); $img->save($this->destinationPath.'images/'.$newFileName, 60, 'jpg'); $img->fit(600, 400); $img->save($this->destinationPath.'thumbnails/'.$newFileName, 60, 'jpg'); // DB stuff $upload = new Upload; $upload->type = 'img'; $upload->name = $fileName; $upload->category = $request->category; $upload->path = $this->destinationPath.'images/'.$newFileName; $upload->thumbnail = $this->destinationPath.'thumbnails/'.$newFileName; $model->images()->save($upload); return view('upload._image', compact('upload')); } else { $file->move($this->destinationPath.'documents/', $newFileName); $upload = new Upload; $upload->type = 'doc'; $upload->name = $file->getClientOriginalName(); $upload->category = $request->category; $upload->path = $this->destinationPath.'documents/'.$newFileName; $model->documents()->save($upload); return view('upload._document', compact('upload')); } } /** * Display the specified resource. * * @param int $id * @return \Illuminate\Http\Response */ public function show($id) { // } /** * Show the form for editing the specified resource. * * @param int $id * @return \Illuminate\Http\Response */ public function edit($id) { // } /** * Update the specified resource in storage. * * @param \Illuminate\Http\Request $request * @param int $id * @return \Illuminate\Http\Response */ public function update(Request $request, $id) { // } /** * Remove the specified resource from storage. * * @param int $id * @return \Illuminate\Http\Response */ public function destroy(Upload $upload) { $upload->delete(); } public function image(Request $request) { $file = $request->file('file'); $request->validate([ 'file' => [ 'required', 'file', 'mimes:jpg,jpeg,png', 'mimetypes:image/jpeg,image/pjpeg,image/png', 'max:51200', ], ]); $fileName = pathinfo($file->getClientOriginalName(), PATHINFO_FILENAME); $extension = strtolower(pathinfo($file->getClientOriginalName(), PATHINFO_EXTENSION)); $tempName = md5($fileName.time()); $newFileName = $tempName.'.'.$extension; // File System stuff $img = Image::make($file->getRealPath()); $img->resize(1200, 1200, function ($constraint) { $constraint->aspectRatio(); $constraint->upsize(); }); $img->save($this->destinationPath.'media/'.$newFileName, 60, 'jpg'); return response()->json(['location' => $newFileName]); } }