public function uploadImage(Request $request, $id) { $project = Project::findOrFail($id); // Checking if there is a file in request if ($request->hasFile('imgFile') && $request->alt) { $image = $request->file('imgFile'); $imageNameOrigin = uniqid() . '-fullsize-' . $image->getClientOriginalName(); $imageNameThumb = uniqid() . '-thumb-' . $image->getClientOriginalName(); $projectPath = 'build/images/projects/' . $project->slug . '/'; $imageNameThousand = ''; if (!file_exists($projectPath)) { Storage::disk('public')->makeDirectory($projectPath); } $img = Image::make($image->getRealPath()); $image->move($projectPath, $imageNameOrigin); if ($img->width() > 980) { $imageNameThousand = uniqid() . '-thousand-' . $image->getClientOriginalName(); $img->resize(980, null, function ($constraint) { $constraint->aspectRatio(); })->save($projectPath . $imageNameThousand, 95); } $img->fit('400')->save($projectPath . $imageNameThumb); // save into database $projectImage = new ProjectImage(); $projectImage->project_id = $project->id; $projectImage->img_thumb = $imageNameThumb; $projectImage->img_origin = $imageNameOrigin; if ($imageNameThousand) { $projectImage->img_thousand = $imageNameThousand; } $projectImage->alt = $request->alt; $projectImage->save(); return ['name' => asset('build/images/projects') . '/' . $project->slug . '/' . $imageNameThumb, 'img' => 'projectImage']; } else { return 'error'; } }
/** * Update the specified resource in storage. * * @param \Illuminate\Http\Request $request * @param int $id * @return \Illuminate\Http\Response */ public function update(Request $request, $id) { $image = array(); $project = ProjectGalleries::where('id', '=', $id)->update(['title' => $request->get('title'), 'content' => $request->get('content'), 'date' => $request->get('date')]); $image = Input::file('image'); if (is_null($image)) { } else { foreach ($image as $value) { $filename = "MEI" . str_random(5) . "-" . $value->getClientOriginalName(); $project_image = new ProjectImage(array('image' => $filename, 'project_galleries_id' => $id)); $project_image->save(); $value->move(base_path() . '/public/admin/gallery/', $filename); } } Session::flash('flash_message', 'Project Updated!'); return redirect()->back(); }