Example #1
0
 /**
  * Upload user profile image.
  *
  * @param User $user
  */
 protected function _uploadAvatar(User $user)
 {
     if (isset($this->_tmpImage) && file_exists($this->_tmpImage['tmp_name'])) {
         $imageInfo = pathinfo($this->_tmpImage['name']);
         $imageName = 'orig_' . $user->get('id') . '.' . $imageInfo['extension'];
         $folderPath = $this->_path->entityFolderPath($user->get('id'), DS);
         $imagePath = $folderPath . $imageName;
         $Folder = new Folder();
         //  Delete user folder if upload new image.
         if (is_dir($folderPath)) {
             $Folder->delete($folderPath);
         }
         if (!is_dir($folderPath)) {
             $Folder->create($folderPath);
         }
         if (is_dir($folderPath) && move_uploaded_file($this->_tmpImage['tmp_name'], $imagePath)) {
             $user->set('image', $imageName);
             $thumbName = 'thumb_' . $user->get('id') . '.' . $imageInfo['extension'];
             //  Resize original image.
             $Resize = new ImageThumbnail($imagePath);
             $Resize->sizeWidth(450);
             $Resize->save($imagePath);
             //  Resize and crop thumb.
             $Thumbnail = new ImageThumbnail($imagePath);
             $Thumbnail->setResize(false);
             $Thumbnail->setSize($this->_config['thumbWidth'], $this->_config['thumbHeight']);
             $Thumbnail->save($folderPath . $thumbName);
         }
     }
 }
Example #2
0
 /**
  * Remove avatar action.
  *
  * @param null $key
  * @return void|\Cake\Network\Response
  */
 public function removeAvatar($key = null)
 {
     if (!$this->Auth->user('id')) {
         $this->Flash->error(__d('community', 'You are not logged in'));
         $this->redirect($this->Auth->config('loginAction'));
     }
     $user = $this->Users->get($this->Auth->user('id'), ['contain' => []]);
     if (base64_decode($key) === 'user' . $user->get('id')) {
         $Path = EntityPath::getInstance('users');
         $fullPath = $Path->entityFolderPath($user->get('id'), DS);
         $Folder = new Folder($fullPath);
         $user->set('image', null);
         if ($Folder->delete() && $this->Users->save($user)) {
             $this->Flash->success(__d('community', '«{0}», you have deleted an image, please upload the new', sprintf('<strong>%s</strong>', $user->get('name'))));
         } else {
             $this->Flash->error(__d('community', '«{0}», an error has occurred, please try again', sprintf('<strong>%s</strong>', $user->get('name'))));
         }
     } else {
         $this->Flash->error(__d('community', '«{0}», you can not make this action', sprintf('<strong>%s</strong>', $user->get('name'))));
     }
     return $this->redirect(['action' => 'edit']);
 }