Ejemplo n.º 1
0
Archivo: User.php Proyecto: elberd/maoh
 public static function edit($request)
 {
     $validator = Validator::make($request->all(), ['name' => 'required|max:50', 'phone' => 'required|max:50', 'email' => 'required|email', 'city' => 'required|integer', 'image' => 'image']);
     if (!$validator->fails()) {
         Auth::user()->name = $request->input('name');
         Auth::user()->phone = str_replace([' ', '(', ')', '-'], '', $request->input('phone'));
         Auth::user()->email = $request->input('email');
         Auth::user()->city_id = $request->input('city');
         if (null != Request::input('password')) {
             Auth::user()->password = $request->input('password');
         }
         if (null != Input::file('image')) {
             $file = Input::file('image');
             if ($file->isValid()) {
                 $image = Image::make($file);
                 $notFullPath = 'avatars/' . date("Y") . '/' . date('m') . '/' . date('d') . '/';
                 $path = 'upload/' . $notFullPath;
                 $extension = File::extension($file->getClientOriginalName());
                 $filename = uniqid() . '.' . $extension;
                 if (!is_dir($path)) {
                     mkdir(public_path($path), 0777, true);
                 }
                 $path .= '/';
                 $image->fit(140, 140);
                 $image->save($path . $filename);
                 $avatar = Avatar::where('user_id', Auth::user()->id)->first();
                 if (is_null($avatar)) {
                     $avatar = new Avatar();
                     $avatar->user_id = Auth::user()->id;
                 } else {
                     File::delete('upload/' . $avatar->path);
                 }
                 $avatar->path = $notFullPath . $filename;
                 $avatar->save();
             } else {
                 echo 'Invalid file';
             }
         }
         Auth::user()->save();
     }
 }
Ejemplo n.º 2
0
 /**
  * @param $userId
  * @return mixed
  */
 public function getAvatar($userId)
 {
     $avatar = Avatar::where('user_id', '=', (int) $userId)->firstOrFail();
     $file = Storage::disk('local')->get(env('UPLOAD_AVATARS') . DIRECTORY_SEPARATOR . $avatar->uri);
     return response($file, 200)->header('Content-Type', $avatar->mime);
 }