ProfileController::saveAvatars PHP Method

saveAvatars() private method

n* : The thumbnail-sized image, which is constrained and cropped according to Garden.Thumbnail.Size. Also deletes the old avatars.
private saveAvatars ( string $source, array $thumbOptions, Gdn_UploadImage | null $upload = null ) : boolean
$source string The path to the local copy of the image.
$thumbOptions array The options to save the thumbnail-sized avatar with.
$upload Gdn_UploadImage | null The upload object.
return boolean Whether the saves were successful.
    private function saveAvatars($source, $thumbOptions, $upload = null)
    {
        try {
            $ext = '';
            if (!$upload) {
                $upload = new Gdn_UploadImage();
                $ext = 'jpg';
            }
            // Generate the target image name
            $targetImage = $upload->generateTargetName(PATH_UPLOADS, $ext, true);
            $imageBaseName = pathinfo($targetImage, PATHINFO_BASENAME);
            $subdir = stringBeginsWith(dirname($targetImage), PATH_UPLOADS . '/', false, true);
            // Save the profile size image.
            $parts = Gdn_UploadImage::saveImageAs($source, self::AVATAR_FOLDER . "/{$subdir}/p{$imageBaseName}", c('Garden.Profile.MaxHeight', 1000), c('Garden.Profile.MaxWidth', 250), array('SaveGif' => c('Garden.Thumbnail.SaveGif')));
            $thumbnailSize = c('Garden.Thumbnail.Size', 40);
            // Save the thumbnail size image.
            Gdn_UploadImage::saveImageAs($source, self::AVATAR_FOLDER . "/{$subdir}/n{$imageBaseName}", $thumbnailSize, $thumbnailSize, $thumbOptions);
        } catch (Exception $ex) {
            $this->Form->addError($ex);
            return false;
        }
        $bak = $this->User->Photo;
        $userPhoto = sprintf($parts['SaveFormat'], self::AVATAR_FOLDER . "/{$subdir}/{$imageBaseName}");
        if (!$this->UserModel->save(array('UserID' => $this->User->UserID, 'Photo' => $userPhoto), array('CheckExisting' => true))) {
            $this->Form->setValidationResults($this->UserModel->validationResults());
        } else {
            $this->User->Photo = $userPhoto;
        }
        $this->deleteAvatars($bak);
        return $userPhoto;
    }