public function changeProfilePicture($user, UpdateProfilePictureRequest $request)
 {
     if ($request->hasFile('image')) {
         $file = $request->file('image');
         $album = Album::where(['album_name' => 'Profile picture', 'user_id' => $this->user->id])->get()->first();
         if (count($album)) {
             $albumId = $album->id;
         } else {
             if ($newAlbum = Album::create(['album_name' => 'Profile picture', 'album_title' => 'Profile picture', 'album_description' => '', 'user_id' => $this->user->id])) {
                 $albumId = $newAlbum->id;
             } else {
                 return response('Unexpected error!', 422);
             }
         }
         $destination = public_path() . '/upload/images/' . $this->user->id . '_' . $this->folder . '/avatar';
         if (!file_exists(public_path() . '/upload/images/' . $this->user->id . '_' . $this->folder)) {
             mkdir(public_path() . '/upload/images/' . $this->user->id . '_' . $this->folder);
         }
         if (!file_exists($destination)) {
             mkdir($destination);
         }
         $imageName = date('d-m-Y_h-i-s') . '_' . $this->user->id . '_' . $file->getClientOriginalName();
         $imageUrl = 'upload/images/' . $this->user->id . '_' . $this->folder . '/avatar/' . $imageName;
         $imageArr = ['image_name' => $imageName, 'fullsize_url' => $imageUrl, 'image_size' => $file->getSize(), 'image_caption' => '', 'user_id' => $this->user->id, 'album_id' => $albumId, 'make_as_profile_picture' => 1];
         $changeImage = Image::find($this->hasProfilePicture($this->user->id));
         if (!is_null($changeImage)) {
             $changeImage->make_as_profile_picture = 0;
             $changeImage->save();
         }
         if ($file->move($destination, $imageName) && Image::create($imageArr)) {
             $data['errors'] = 0;
             $data['msg'] = 'Your profile picture has been change!';
             $data['imageUrl'] = url($imageUrl);
             return json_encode($data);
         }
         return response('Unexpected error while changging your profile picture!', 422);
     }
 }
示例#2
0
 public function updateProfilePicture(Requests\UpdateProfilePictureRequest $request)
 {
     flash()->overlay('Profile picture updated.', 'Success!');
     //id ni nakalogin na user
     $userID = \Auth::user()->id;
     DB::table('users')->where('id', $userID);
     //https://www.codetutorial.io/laravel-5-file-upload-storage-download/
     $fileName = $userID . '.' . $request->file('image')->getClientOriginalExtension();
     $path = base_path() . '/public/profile_pictures/' . $fileName;
     $request->file('image')->move(base_path() . '/public/profile_pictures/', $fileName);
     //dd($path);
     $path = '/public/profile_pictures/' . explode("/", $path)[sizeof(explode("/", $path)) - 1];
     //get value at last element
     //dd($path);
     DB::table('users')->where('id', $userID)->update(['profile_picture_path' => $path]);
     return redirect()->back()->withInput();
 }