Как реализовать регистрацию пользователя с загрузкой фото?

Здравствуйте. Подскажите как можно реализовать регистрацию нового пользователя на сайте с загрузкой фото к своему профилю? Обратил сначала внимание на компонент Login, но там такого функционала я не обнаружил. Может есть какое готовое решение к этому или другому компоненту регистрации, расширяющее функционал и добавляющее возможность загружать свое фото при регистрации? Или лучше приобрести Office?
Константин
02 апреля 2018, 17:46
modx.pro
1 251
0

Комментарии: 3

Андрей
02 апреля 2018, 22:08
0
Обратил сначала внимание на компонент Login, но там такого функционала я не обнаружил.

В Login такого и нет если не ошибаюсь, вроде были какие то решения добавляющие такую возможность, надо гуглить. Можно купить Office, или UserAvatar.
    Stan Ezersky
    03 апреля 2018, 09:35
    0
    Использую Login на демке
    Прикручивается руками к странице редактирования профиля



    Попробовать самому
      Владимир Бабусенко
      03 апреля 2018, 12:46
      0
      В форме регистрации

      enctype="multipart/form-data" добавить
      
      <div class="form-group">
                  <label>Логотип</label>
                  <div>
                      <div id="image_preview"></div>
                      <div id="selectImage">
                          <input type="file" name="photo" id="file" accept="image/jpeg,image/jpg,image/png"/>
                      </div>
                  </div>
              </div>
      В сниппет регистрации добавляем
      [[!Register?
          ......
          
          &preHooks = `photo.preHook`
          &postHooks = `photo.postHook`
          
      ]]
      photo.preHook (только папки создай)
      <?php
      $output = true;
      $fields = $hook->getValues();
      /* User's photo */
      if (!empty($fields['photo'])) {
          // valid extensions
          $extArray = array('jpg', 'jpeg', 'gif', 'png');
          // create temporary path for this form submission
          $uploadPath = 'assets/uploads/temp/';
          $targetPath = $hook->modx->config['base_path'] . $uploadPath;
          // get uploaded file names:
          $submittedFiles = array_keys($_FILES);
          // loop through files
          foreach ($submittedFiles as $sf) {
              // Get Filename and make sure its good.
              $filename = basename($_FILES[$sf]['name']);
              // Get file's extension
              $ext = pathinfo($filename, PATHINFO_EXTENSION);
              
              // case insensitive
              $ext = mb_strtolower($ext);
              
              // is the file name empty (no file uploaded)
              if ($filename != '') {
                  // is this the right type of file?
                  if (in_array($ext, $extArray)) {
                      //create file called the user name
                      $filename = mb_strtolower($filename);
                      // full path to new file
                      $uploadFilePath = $targetPath . $filename;
                      // create directory to move file into if it doesn't exist
                      @mkdir($targetPath, 0755, true);
                      if (file_exists($uploadFilePath)) {
                          // Change the file permissions if allowed
                          chmod($uploadFilePath, 0755);
                          // remove the file
                          unlink($uploadFilePath);
                      }
                      // is the file moved to the proper folder successfully?
                      if (move_uploaded_file($_FILES[$sf]['tmp_name'], $uploadFilePath)) {
                          $hook->setValue($sf, $uploadPath . $filename);
                          if (!chmod($uploadFilePath, 0644)) {
                              /* some debug function */
                          }
                      } else {
                          
                          // File not uploaded
                          $errorMsg = 'There was a problem uploading the file.';
                          $hook->addError($sf, $errorMsg);
                          
                          // generate submission error
                          $output = false; 
                      }
                  } else {
                      
                      // File type not allowed
                      $errorMsg = 'Type of file not allowed.';
                      $hook->addError($sf, $errorMsg);
                      
                      // generate submission error
                      $output = false;
                  }
              } else {
                  
                  // if no file, don't give error, but just return blank
                  $hook->setValue($sf, '');
              }
          }
      }
      return $output;

      photo.postHook
      <?php
      /** @var modUser $user */
      $user = & $fields['register.user'];
      $userId = $user->get('id');
      /** @var modUserProfile $profile  */
      $profile = & $fields['register.profile'];
      if (!empty($fields['photo'])) {
          $photo = array();
          $photo['temp'] = $fields['photo'];
          $photo['basename'] = basename($photo['temp']);
          /***********************************************************************
           * XXX: IMPORTANT XXX
           *
           * Create unique path here for this profile updating.
           * You can change this as you wish.
           * The $userId variable comes from above initiation.
           *
           ***********************************************************************/
          $photo['newdir'] = 'assets/uploads/profiles/' . $userId . '/';
          $photo['newfilepath'] = $photo['newdir'] . $photo['basename'];
          $photo['target'] = $hook->modx->config['base_path'] . $photo['temp'];
          $photo['moved'] = $hook->modx->config['base_path'] . $photo['newfilepath'];
          // make the user's private directory
          mkdir($photo['newdir'], 0755, true);
          $photoUpdated = false;
          // move the photo from the temporary path to the new one
          if (!rename($photo['target'], $photo['moved'])) {
              // if "rename" function fails, try "copy" instead.
              if (!copy($photo['target'], $photo['moved'])) {
                  // just dump the log report to the MODX's error log,
                  // because both "rename" and "copy" functions fail
                  $hook->modx->log(modX::LOG_LEVEL_ERROR, __FILE__ . ' ');
                  $hook->modx->log(modX::LOG_LEVEL_ERROR, __LINE__ . ': $userId ' . $userId);
                  $hook->modx->log(modX::LOG_LEVEL_ERROR, __LINE__ . ': $photo ' . print_r($photo, 1));
              } else {
                  // if copy succeeded, delete the old temporary picture
                  unlink($photo['target']);
                  $photoUpdated = true;
              }
          } else {
              $photoUpdated = true;
          }
          if ($photoUpdated) {
              /**
              * Теперь мы обновляем профиля
               * Переменная $ профиль приходит сверху посвящения.
               */
              $profile->set('photo', $photo['newfilepath']);
              $profile->save();
              /**
               * Yeah! xPDO rocks! Simply like that!
               */
          }
      }
      return TRUE;
      js
      //Превью изображения
          $(document).on('change', '#file', function(evt){
      
              $('#image_preview').empty();
      
              var files = evt.target.files;
      
              for (var i = 0, f; f = files[i]; i++) {
                  // Only process image files.
                  if (!f.type.match('image.*')) {
                      continue;
                  }
                  var reader = new FileReader();
      
                  reader.onload = (function (theFile) {
                      return function (e) {
                          $('#image_preview').append('<img class="previewing img-thumbnail" src="'+e.target.result+'"  style="width:250px;"/>');
                      };
                  })(f);
      
                  reader.readAsDataURL(f);
              }
          });
        Авторизуйтесь или зарегистрируйтесь, чтобы оставлять комментарии.
        3