Advertisement
Guest User

Untitled

a guest
Oct 4th, 2016
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.10 KB | None | 0 0
  1. <?php
  2.  
  3. namespace App\Http\Controllers;
  4.  
  5. use Illuminate\Support\Facades\Input;
  6. use App\Http\Requests;
  7. use Illuminate\Http\Request;
  8. use Auth;
  9. use App\User;
  10. use Storage;
  11. use Image;
  12.  
  13. class ProfileController extends Controller
  14. {
  15.     /**
  16.     * Create a new controller instance.
  17.     *
  18.     * @return void
  19.     */
  20.     public function __construct()
  21.     {
  22.         $this->middleware('auth');
  23.     }
  24.  
  25.     protected function validator(array $data, $id)
  26.     {
  27.         return User::edit_validator($data, $id);
  28.     }
  29.  
  30.     /**
  31.     * Show the application dashboard.
  32.     *
  33.     * @return \Illuminate\Http\Response
  34.     */
  35.     public function show($id)
  36.     {
  37.         $profile = User::find($id);
  38.  
  39.         $this->authorize('show', $profile);
  40.  
  41.         return view('user.show', array(
  42.             'user' => $profile,
  43.             'articles' => $profile->articles,
  44.         ));
  45.     }
  46.  
  47.     public function edit($id)
  48.     {
  49.         $profile = User::find($id);
  50.         $this->authorize('update', $profile);
  51.  
  52.         return view('user.edit', array('user' => $profile));
  53.     }
  54.  
  55.     public function update(Request $request, $id)
  56.     {
  57.         $user = User::find($id);
  58.         $this->authorize('update', $user);
  59.         $validator = $this->validator($request->all(), $id);
  60.         if($validator->fails())
  61.         {
  62.             return redirect()
  63.                 ->action('ProfileController@edit', ['id' => $id])
  64.                 ->withErrors($validator)
  65.                 ->withInput();
  66.         }
  67.         else
  68.         {
  69.             $user->name = $request->name;
  70.             $user->email = $request->email;
  71.             $user->password = User::encryptPassword($request->password);
  72.  
  73.             if(Input::file('image')) {
  74.                 $imageFiletype = 'png';
  75.                 $image = $this->processProfileImage(Input::file('image'), $imageFiletype);
  76.                 $filename = $this->profileImageFilepath($user, $image, $imageFiletype);
  77.                 Storage::put('public/'.$filename, $image);
  78.                 $user->image_path = 'storage/'.$filename;
  79.             }
  80.  
  81.             $user->save();
  82.  
  83.             return redirect()->action('ProfileController@show', ['id' => $id]);
  84.         }
  85.     }
  86.  
  87.     private function processProfileImage($file, $filetype) {
  88.         $img = Image::make($file);
  89.  
  90.         $img->fit(100, 100, function ($constraint) {
  91.             $constraint->upsize();
  92.         });
  93.  
  94.         $width = $img->width();
  95.         $height = $img->height();
  96.  
  97.         $mask = Image::canvas($width, $height, '#000000');
  98.         $mask->circle($width, $width/2, $height/2, function($draw) {
  99.             $draw->background('#ffffff');
  100.         });
  101.  
  102.         $img->mask($mask);
  103.         $img->text('AwesomeCMS', 50, $height-20, function($font) {
  104.             $font->file(3);
  105.             $font->color('rgba(255, 255, 255, 0.5)');
  106.             $font->align('center');
  107.         });
  108.         $img->stream($filetype);
  109.  
  110.         return $img;
  111.     }
  112.  
  113.     private function profileImageFilepath($user, $img, $extension) {
  114.         return 'profile/' . sha1($user->id . $img->filesize() . time()) . '.' . $extension;
  115.     }
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement