/**
  * [updatePhoto description]
  * @param  Integer Id do usuário
  * @return ??
  */
 public function cropPhotoEntidade($entidade, CropPhotoRequest $request)
 {
     if (!$entidade) {
         App::abort(500, 'Erro durante o processamento do crop');
     }
     $file = Input::file('image_file_upload');
     if ($file && $file->isValid()) {
         $widthCrop = $request->input('w');
         $heightCrop = $request->input('h');
         $xSuperior = $request->input('x');
         $ySuperior = $request->input('y');
         $destinationPath = public_path() . '/uploads/';
         $extension = Input::file('image_file_upload')->getClientOriginalExtension();
         // Pega o formato da imagem
         $fileName = self::formatFileNameWithUserAndTimestamps($file->getClientOriginalName()) . '.' . $extension;
         $file = \Image::make($file->getRealPath())->crop($widthCrop, $heightCrop, $xSuperior, $ySuperior);
         $upload_success = $file->save($destinationPath . $fileName);
         //Salvando imagem no avatar do usuario;
         if ($upload_success) {
             /* Settando tipo da foto atual para null, checando se existe antes */
             if ($entidade->avatar) {
                 $currentAvatar = $entidade->avatar;
                 $currentAvatar->tipo = null;
                 $currentAvatar->save();
             }
             $foto = new Foto(['path' => $fileName, 'tipo' => 'avatar']);
             $entidade->fotos()->save($foto);
             return true;
         } else {
             return false;
         }
     }
 }
Example #2
0
 public function postCropandsave($id = 0, CropPhotoRequest $request)
 {
     $file = Input::file('file');
     if ($file && $file->isValid()) {
         $entidade = Auth::user()->entidadeAtiva;
         $destinationPath = public_path() . '/uploads/';
         $extension = Input::file('file')->getClientOriginalExtension();
         // Pega o formato da imagem
         $widthCrop = round($request->input('w'));
         $heightCrop = round($request->input('h'));
         $xSuperior = round($request->input('x'));
         $ySuperior = round($request->input('y'));
         //Se tem owner, caso nao esteja pre criano uma foto para uma entidade
         $naoTemOwner = $request->input('NoOwner');
         // pega o tipo da foto (avatar, capa, etc)
         $tipo = $request->input('tipo');
         if (!$tipo) {
             $tipo = 'avatar';
         }
         $fileName = $this->formatFileNameWithUserAndTimestamps($file->getClientOriginalName()) . '.' . $extension;
         $file = \Image::make($file->getRealPath())->crop($widthCrop, $heightCrop, $xSuperior, $ySuperior);
         $upload_success = $file->save($destinationPath . $fileName);
         if ($upload_success) {
             //Se nao tiver owner, entao estou pre settando a foto de
             //avatar antes de criar a entidade.
             if ($naoTemOwner) {
                 $foto = Foto::create(['path' => $fileName, 'tipo' => $tipo]);
                 //Se tiver um owner, entao ja salvar a foto para a
                 //entidade ativa
             } else {
                 $foto = new Foto(['path' => $fileName, 'tipo' => $tipo]);
                 /* Settando tipo da foto atual para null, checando se existe antes */
                 if ($tipo == 'avatar' && $entidade->avatar) {
                     $avatares = $entidade->fotos()->where('tipo', 'avatar')->get();
                     foreach ($avatares as $currentAvatar) {
                         $currentAvatar->tipo = null;
                         $currentAvatar->save();
                     }
                 }
                 $entidade->fotos()->save($foto);
             }
             return $foto;
         } else {
             return false;
         }
     }
 }