ProfileController::picture PHP Method

picture() public method

Set user's photo (avatar).
Since: 2.0.0
public picture ( mixed $userReference = '', string $username = '', string $userID = '' )
$userReference mixed Unique identifier, possible username or ID.
$username string The username.
$userID string The user's ID.
    public function picture($userReference = '', $username = '', $userID = '')
    {
        $this->addJsFile('profile.js');
        if (!$this->CanEditPhotos) {
            throw forbiddenException('@Editing user photos has been disabled.');
        }
        // Permission checks
        $this->permission(array('Garden.Profiles.Edit', 'Moderation.Profiles.Edit', 'Garden.ProfilePicture.Edit'), false);
        $session = Gdn::session();
        if (!$session->isValid()) {
            $this->Form->addError('You must be authenticated in order to use this form.');
        }
        // Check ability to manipulate image
        if (function_exists('gd_info')) {
            $gdInfo = gd_info();
            $gdVersion = preg_replace('/[a-z ()]+/i', '', $gdInfo['GD Version']);
            if ($gdVersion < 2) {
                throw new Exception(sprintf(t("This installation of GD is too old (v%s). Vanilla requires at least version 2 or compatible."), $gdVersion));
            }
        } else {
            throw new Exception(sprintf(t("Unable to detect PHP GD installed on this system. Vanilla requires GD version 2 or better.")));
        }
        // Get user data & prep form.
        if ($this->Form->authenticatedPostBack() && $this->Form->getFormValue('UserID')) {
            $userID = $this->Form->getFormValue('UserID');
        }
        $this->getUserInfo($userReference, $username, $userID, true);
        $validation = new Gdn_Validation();
        $configurationModel = new Gdn_ConfigurationModel($validation);
        $this->Form->setModel($configurationModel);
        $avatar = $this->User->Photo;
        if ($avatar === null) {
            $avatar = UserModel::getDefaultAvatarUrl();
        }
        $source = '';
        $crop = null;
        if ($this->isUploadedAvatar($avatar)) {
            // Get the image source so we can manipulate it in the crop module.
            $upload = new Gdn_UploadImage();
            $thumbnailSize = c('Garden.Thumbnail.Size', 40);
            $basename = changeBasename($avatar, "p%s");
            $source = $upload->copyLocal($basename);
            // Set up cropping.
            $crop = new CropImageModule($this, $this->Form, $thumbnailSize, $thumbnailSize, $source);
            $crop->setExistingCropUrl(Gdn_UploadImage::url(changeBasename($avatar, "n%s")));
            $crop->setSourceImageUrl(Gdn_UploadImage::url(changeBasename($avatar, "p%s")));
            $this->setData('crop', $crop);
        } else {
            $this->setData('avatar', $avatar);
        }
        if (!$this->Form->authenticatedPostBack()) {
            $this->Form->setData($configurationModel->Data);
        } else {
            if ($this->Form->save() !== false) {
                $upload = new Gdn_UploadImage();
                $newAvatar = false;
                if ($tmpAvatar = $upload->validateUpload('Avatar', false)) {
                    // New upload
                    $thumbOptions = array('Crop' => true, 'SaveGif' => c('Garden.Thumbnail.SaveGif'));
                    $newAvatar = $this->saveAvatars($tmpAvatar, $thumbOptions, $upload);
                } else {
                    if ($avatar && $crop && $crop->isCropped()) {
                        // New thumbnail
                        $tmpAvatar = $source;
                        $thumbOptions = array('Crop' => true, 'SourceX' => $crop->getCropXValue(), 'SourceY' => $crop->getCropYValue(), 'SourceWidth' => $crop->getCropWidth(), 'SourceHeight' => $crop->getCropHeight());
                        $newAvatar = $this->saveAvatars($tmpAvatar, $thumbOptions);
                    }
                }
                if ($this->Form->errorCount() == 0) {
                    if ($newAvatar !== false) {
                        $thumbnailSize = c('Garden.Thumbnail.Size', 40);
                        // Update crop properties.
                        $basename = changeBasename($newAvatar, "p%s");
                        $source = $upload->copyLocal($basename);
                        $crop = new CropImageModule($this, $this->Form, $thumbnailSize, $thumbnailSize, $source);
                        $crop->setSize($thumbnailSize, $thumbnailSize);
                        $crop->setExistingCropUrl(Gdn_UploadImage::url(changeBasename($newAvatar, "n%s")));
                        $crop->setSourceImageUrl(Gdn_UploadImage::url(changeBasename($newAvatar, "p%s")));
                        $this->setData('crop', $crop);
                    }
                }
                if ($this->deliveryType() === DELIVERY_TYPE_VIEW) {
                    $this->jsonTarget('', '', 'Refresh');
                    $this->RedirectUrl = userUrl($this->User);
                }
                $this->informMessage(t("Your settings have been saved."));
            }
        }
        if (val('SideMenuModule', val('Panel', val('Assets', $this)))) {
            /** @var SideMenuModule $sidemenu */
            $sidemenu = $this->Assets['Panel']['SideMenuModule'];
            $sidemenu->highlightRoute('/profile/picture');
        }
        $this->title(t('Change Picture'));
        $this->_setBreadcrumbs(t('Change My Picture'), userUrl($this->User, '', 'picture'));
        $this->render('picture', 'profile', 'dashboard');
    }