Esempio n. 1
0
 /**
  * Store a newly created resource in storage.
  *
  * @param  \Illuminate\Http\Request  $request
  * @return \Illuminate\Http\Response
  */
 public function store(UploadImageRequest $request, $user)
 {
     if (!file_exists(public_path() . '/upload')) {
         mkdir(public_path() . '/upload');
     }
     if (!file_exists(public_path() . '/upload/images/')) {
         mkdir(public_path() . '/upload/images');
     }
     $id = Auth::user()->id;
     $auth = substr(md5(Auth::user()->id . Auth::user()->email), 0, 10);
     $resource = $request->except(['_token', 'image']);
     //dd($request->file('image'));
     if (!$request->get('album_id')) {
         $data_album = ['album_name' => 'Untitled', 'album_title' => 'Untitled album', 'user_id' => $id];
         if (!($album = Album::create($data_album))) {
             return redirect()->route('photo.create', $user)->withErrors('Unexpected error!');
         }
         $resource['album_id'] = $album->id;
     }
     if ($request->hasFile('image')) {
         $file = $request->file('image');
         $imageName = date('d-m-Y_h-i-s') . '_' . $id . '_' . $file->getClientOriginalName();
         $path = $this->path . '/' . $id . '_' . $auth;
         $destination = $path;
         $folder = public_path() . '/' . $path;
         if (!file_exists($folder)) {
             mkdir($folder);
         }
         $image = ['image_name' => $imageName, 'image_size' => $file->getSize(), 'user_id' => $id, 'fullsize_url' => $path . '/' . $imageName];
         if ($file->move($destination, $imageName) && Image::create(array_merge($resource, $image))) {
             return response()->json(['message' => 'Your photo has been upload!']);
         }
     }
     return response()->json(['message' => 'Unexpected errors']);
 }
Esempio n. 2
0
 public function actionUploadImage($id, Requests\UploadImageRequest $request)
 {
     $user = User::findOrFail($id);
     if (!$request->hasFile('image')) {
         \Session::flash('flash_message', 'No file selected.');
         return redirect('/user/' . $id);
     }
     if (!$request->file('image')->isValid()) {
         \Session::flash('flash_message', 'File is not valid.');
         return redirect('/user/' . $id);
     }
     $destinationPath = 'uploads';
     $extension = $request->file('image')->getClientOriginalExtension();
     $fileName = Uuid::generate(4) . '.' . $extension;
     $request->file('image')->move($destinationPath, $fileName);
     $user->image = $fileName;
     $user->save();
     return redirect('/user/' . $id);
 }
Esempio n. 3
0
 public function actionUploadImage($id, Requests\UploadImageRequest $request)
 {
     // Set logged in user into a variable.
     $authUser = Auth::user();
     // Find the user in the database.
     $user = User::findOrFail($id);
     // Check to see if user profile belongs to logged in user
     // If not, redirect with flash message.
     if ($authUser->id !== $user->id) {
         \Session::flash('flash_message', 'You are not authorized to edit this profile.');
         return redirect('/user/' . $id);
     }
     // Check to see if image is exists and is valid
     // If not, redirect with flash message.
     if (!$request->hasFile('image')) {
         \Session::flash('flash_message', 'No file selected.');
         return redirect('/user/' . $id);
     }
     if (!$request->file('image')->isValid()) {
         \Session::flash('flash_message', 'File is not valid.');
         return redirect('/user/' . $id);
     }
     // Set destination path.
     $destinationPath = 'uploads';
     // Set extension.
     $extension = $request->file('image')->getClientOriginalExtension();
     // Set file name.
     $fileName = Uuid::generate(4) . '.' . $extension;
     // Move file to destination path.
     $request->file('image')->move($destinationPath, $fileName);
     // Set file name as user's image.
     $user->image = $fileName;
     // Save user.
     $user->save();
     // Redirect with flash message.
     \Session::flash('flash_message', 'You have successfully uploaded a user photo.');
     return redirect('/user/' . $id);
 }
Esempio n. 4
0
 /**
  * Upload a new image to the server
  *
  * @param UploadImageRequest $request
  * @return \Illuminate\Http\JsonResponse
  */
 public function uploadImage(UploadImageRequest $request)
 {
     $file_path = \Resource::uploadResource($request->file('image'));
     return response()->json(['path' => $file_path]);
 }