Пример #1
0
 /**
  * Try to download and parse remote avatar
  * @param string $url
  * @param int $userId
  */
 protected function parseAvatar($url, $userId)
 {
     // check if user is defined
     if ((int) $userId < 1) {
         return;
     }
     // check remote image extension
     $imageExtension = Str::lastIn($url, '.', true);
     if (!Arr::in($imageExtension, ['png', 'gif', 'jpg', 'jpeg'])) {
         return;
     }
     // try to get image binary data
     $imageContent = Url::download($url);
     if ($imageContent === null || Str::likeEmpty($imageContent)) {
         return;
     }
     // write image to filesystem
     $imagePath = '/upload/user/avatar/original/' . $userId . '.' . $imageExtension;
     $write = File::write($imagePath, $imageContent);
     if ($write === false) {
         return;
     }
     // try to write and resize file
     try {
         $fileObject = new FileObject(root . $imagePath);
         $avatarUpload = new FormAvatarUpload();
         $avatarUpload->resizeAndSave($fileObject, $userId, 'small');
         $avatarUpload->resizeAndSave($fileObject, $userId, 'medium');
         $avatarUpload->resizeAndSave($fileObject, $userId, 'big');
     } catch (\Exception $e) {
         if (App::$Debug) {
             App::$Debug->addException($e);
         }
     }
 }
Пример #2
0
 /**
  * User avatar management
  * @throws \Ffcms\Core\Exception\NativeException
  * @throws \Ffcms\Core\Exception\SyntaxException
  */
 public function actionAvatar()
 {
     if (!App::$User->isAuth()) {
         throw new ForbiddenException('You must be authorized user!');
     }
     // get user identity and model object
     $user = App::$User->identity();
     $model = new FormAvatarUpload(true);
     // validate model post data
     if ($model->send()) {
         if ($model->validate()) {
             $model->copyFile($user);
             App::$Session->getFlashBag()->add('success', __('Avatar is successful changed'));
         } else {
             App::$Session->getFlashBag()->add('error', __('File upload is failed!'));
         }
     }
     return $this->view->render('avatar', ['user' => $user, 'model' => $model]);
 }