/**
  * Saves the default avatar to /uploads in three formats:
  *   The default image, which is not resized or cropped.
  *   p* : The profile-sized image, which is constrained by Garden.Profile.MaxWidth and Garden.Profile.MaxHeight.
  *   n* : The thumbnail-sized image, which is constrained and cropped according to Garden.Thumbnail.Size.
  *
  * @param string $source The path to the local copy of the image.
  * @param array $thumbOptions The options to save the thumbnail-sized avatar with.
  * @return bool Whether the saves were successful.
  */
 private function saveDefaultAvatars($source, $thumbOptions)
 {
     try {
         $upload = new Gdn_UploadImage();
         // Generate the target image name
         $targetImage = $upload->generateTargetName(PATH_UPLOADS);
         $imageBaseName = pathinfo($targetImage, PATHINFO_BASENAME);
         // Save the full size image.
         $parts = Gdn_UploadImage::saveImageAs($source, self::DEFAULT_AVATAR_FOLDER . '/' . $imageBaseName);
         // Save the profile size image.
         Gdn_UploadImage::saveImageAs($source, self::DEFAULT_AVATAR_FOLDER . "/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::DEFAULT_AVATAR_FOLDER . "/n{$imageBaseName}", $thumbnailSize, $thumbnailSize, $thumbOptions);
     } catch (Exception $ex) {
         $this->Form->addError($ex);
         return false;
     }
     $imageBaseName = $parts['SaveName'];
     saveToConfig('Garden.DefaultAvatar', $imageBaseName);
     return true;
 }
Пример #2
0
 /**
  * Form for adding an email image.
  * Exposes the Garden.EmailTemplate.Image setting.
  * Garden.EmailTemplate.Image must be an upload.
  *
  * Saves the image based on 2 config settings:
  * Garden.EmailTemplate.ImageMaxWidth (default 400px) and
  * Garden.EmailTemplate.ImageMaxHeight (default 300px)
  *
  * @throws Gdn_UserException
  */
 public function emailImage()
 {
     if (!Gdn::session()->checkPermission('Garden.Community.Manage')) {
         throw permissionException();
     }
     $this->addJsFile('email.js');
     $this->addSideMenu('dashboard/settings/email');
     $image = c('Garden.EmailTemplate.Image');
     $this->Form = new Gdn_Form();
     $validation = new Gdn_Validation();
     $configurationModel = new Gdn_ConfigurationModel($validation);
     // Set the model on the form.
     $this->Form->setModel($configurationModel);
     if ($this->Form->authenticatedPostBack() !== false) {
         try {
             $upload = new Gdn_UploadImage();
             // Validate the upload
             $tmpImage = $upload->validateUpload('EmailImage', false);
             if ($tmpImage) {
                 // Generate the target image name
                 $targetImage = $upload->generateTargetName(PATH_UPLOADS);
                 $imageBaseName = pathinfo($targetImage, PATHINFO_BASENAME);
                 // Delete any previously uploaded images.
                 if ($image) {
                     $upload->delete($image);
                 }
                 // Save the uploaded image
                 $parts = $upload->saveImageAs($tmpImage, $imageBaseName, c('Garden.EmailTemplate.ImageMaxWidth', 400), c('Garden.EmailTemplate.ImageMaxHeight', 300));
                 $imageBaseName = $parts['SaveName'];
                 saveToConfig('Garden.EmailTemplate.Image', $imageBaseName);
                 $this->setData('EmailImage', Gdn_UploadImage::url($imageBaseName));
             } else {
                 $this->Form->addError(t('There\'s been an error uploading the image. Your email logo can uploaded in one of the following filetypes: gif, jpg, png'));
             }
         } catch (Exception $ex) {
             $this->Form->addError($ex);
         }
     }
     $this->render();
 }
Пример #3
0
 /**
  * Save an icon image file.
  *
  * @param string $imageLocation Where to save the icon to file.
  * @return mixed
  * @throws Exception Unable to save icon or GD is not installed.
  */
 protected function saveIcon($imageLocation)
 {
     $uploadImage = new Gdn_UploadImage();
     // Generate the target image name
     $extension = val('extension', pathinfo($imageLocation), '');
     $targetLocation = $uploadImage->generateTargetName('addons/icons', $extension);
     // Save the uploaded icon
     $parsed = $uploadImage->saveImageAs($imageLocation, $targetLocation, 256, 256, false, false);
     return $parsed['SaveName'];
 }
Пример #4
0
 /**
  * Settings page for HTML email styling.
  *
  * Exposes config settings:
  * Garden.EmailTemplate.BackgroundColor
  * Garden.EmailTemplate.ButtonBackgroundColor
  * Garden.EmailTemplate.ButtonTextColor
  * Garden.EmailTemplate.Image
  *
  * Saves the image based on 2 config settings:
  * Garden.EmailTemplate.ImageMaxWidth (default 400px) and
  * Garden.EmailTemplate.ImageMaxHeight (default 300px)
  *
  * @throws Gdn_UserException
  */
 public function emailStyles()
 {
     // Set default colors
     if (!c('Garden.EmailTemplate.TextColor')) {
         saveToConfig('Garden.EmailTemplate.TextColor', EmailTemplate::DEFAULT_TEXT_COLOR, false);
     }
     if (!c('Garden.EmailTemplate.BackgroundColor')) {
         saveToConfig('Garden.EmailTemplate.BackgroundColor', EmailTemplate::DEFAULT_BACKGROUND_COLOR, false);
     }
     if (!c('Garden.EmailTemplate.ContainerBackgroundColor')) {
         saveToConfig('Garden.EmailTemplate.ContainerBackgroundColor', EmailTemplate::DEFAULT_CONTAINER_BACKGROUND_COLOR, false);
     }
     if (!c('Garden.EmailTemplate.ButtonTextColor')) {
         saveToConfig('Garden.EmailTemplate.ButtonTextColor', EmailTemplate::DEFAULT_BUTTON_TEXT_COLOR, false);
     }
     if (!c('Garden.EmailTemplate.ButtonBackgroundColor')) {
         saveToConfig('Garden.EmailTemplate.ButtonBackgroundColor', EmailTemplate::DEFAULT_BUTTON_BACKGROUND_COLOR, false);
     }
     $this->permission('Garden.Settings.Manage');
     $this->setHighlightRoute('dashboard/settings/emailstyles');
     $this->addJsFile('email.js');
     // Get the current logo.
     $image = c('Garden.EmailTemplate.Image');
     if ($image) {
         $image = ltrim($image, '/');
         $this->setData('EmailImage', Gdn_UploadImage::url($image));
     }
     $this->Form = new Gdn_Form();
     $validation = new Gdn_Validation();
     $configurationModel = new Gdn_ConfigurationModel($validation);
     $configurationModel->setField(array('Garden.EmailTemplate.TextColor', 'Garden.EmailTemplate.BackgroundColor', 'Garden.EmailTemplate.ContainerBackgroundColor', 'Garden.EmailTemplate.ButtonTextColor', 'Garden.EmailTemplate.ButtonBackgroundColor'));
     // Set the model on the form.
     $this->Form->setModel($configurationModel);
     // If seeing the form for the first time...
     if ($this->Form->authenticatedPostBack() === false) {
         // Apply the config settings to the form.
         $this->Form->setData($configurationModel->Data);
     } else {
         $image = c('Garden.EmailTemplate.Image');
         $upload = new Gdn_UploadImage();
         if ($upload->isUpload('EmailImage')) {
             try {
                 $tmpImage = $upload->validateUpload('EmailImage');
                 if ($tmpImage) {
                     // Generate the target image name
                     $targetImage = $upload->generateTargetName(PATH_UPLOADS);
                     $imageBaseName = pathinfo($targetImage, PATHINFO_BASENAME);
                     // Delete any previously uploaded images.
                     if ($image) {
                         $upload->delete($image);
                     }
                     // Save the uploaded image
                     $parts = $upload->saveImageAs($tmpImage, $imageBaseName, c('Garden.EmailTemplate.ImageMaxWidth', 400), c('Garden.EmailTemplate.ImageMaxHeight', 300));
                     $imageBaseName = $parts['SaveName'];
                     saveToConfig('Garden.EmailTemplate.Image', $imageBaseName);
                     $this->setData('EmailImage', Gdn_UploadImage::url($imageBaseName));
                 }
             } catch (Exception $ex) {
                 $this->Form->addError($ex);
             }
         }
         if ($this->Form->save() !== false) {
             $this->informMessage(t("Your settings have been saved."));
         }
     }
     $this->render();
 }
Пример #5
0
 /**
  * Set user's thumbnail (crop & center photo).
  *
  * @since 2.0.0
  * @access public
  * @param mixed $UserReference Unique identifier, possible username or ID.
  * @param string $Username .
  */
 public function thumbnail($UserReference = '', $Username = '')
 {
     if (!c('Garden.Profile.EditPhotos', true)) {
         throw forbiddenException('@Editing user photos has been disabled.');
     }
     // Initial permission checks (valid user)
     $this->permission('Garden.SignIn.Allow');
     $Session = Gdn::session();
     if (!$Session->isValid()) {
         $this->Form->addError('You must be authenticated in order to use this form.');
     }
     // Need some extra JS
     // jcrop update jan28, 2014 as jQuery upgrade to 1.10.2 no longer
     // supported browser()
     $this->addJsFile('jquery.jcrop.min.js');
     $this->addJsFile('profile.js');
     $this->getUserInfo($UserReference, $Username, '', true);
     // Permission check (correct user)
     if ($this->User->UserID != $Session->UserID && !checkPermission('Garden.Users.Edit') && !checkPermission('Moderation.Profiles.Edit')) {
         throw new Exception(t('You cannot edit the thumbnail of another member.'));
     }
     // Form prep
     $this->Form->setModel($this->UserModel);
     $this->Form->addHidden('UserID', $this->User->UserID);
     // Confirm we have a photo to manipulate
     if (!$this->User->Photo) {
         $this->Form->addError('You must first upload a picture before you can create a thumbnail.');
     }
     // Define the thumbnail size
     $this->ThumbSize = Gdn::config('Garden.Thumbnail.Size', 40);
     // Define the source (profile sized) picture & dimensions.
     $Basename = changeBasename($this->User->Photo, 'p%s');
     $Upload = new Gdn_UploadImage();
     $PhotoParsed = Gdn_Upload::Parse($Basename);
     $Source = $Upload->CopyLocal($Basename);
     if (!$Source) {
         $this->Form->addError('You cannot edit the thumbnail of an externally linked profile picture.');
     } else {
         $this->SourceSize = getimagesize($Source);
     }
     // We actually need to upload a new file to help with cdb ttls.
     $NewPhoto = $Upload->generateTargetName('userpics', trim(pathinfo($this->User->Photo, PATHINFO_EXTENSION), '.'), true);
     // Add some more hidden form fields for jcrop
     $this->Form->addHidden('x', '0');
     $this->Form->addHidden('y', '0');
     $this->Form->addHidden('w', $this->ThumbSize);
     $this->Form->addHidden('h', $this->ThumbSize);
     $this->Form->addHidden('HeightSource', $this->SourceSize[1]);
     $this->Form->addHidden('WidthSource', $this->SourceSize[0]);
     $this->Form->addHidden('ThumbSize', $this->ThumbSize);
     if ($this->Form->authenticatedPostBack() === true) {
         try {
             // Get the dimensions from the form.
             Gdn_UploadImage::SaveImageAs($Source, changeBasename($NewPhoto, 'n%s'), $this->ThumbSize, $this->ThumbSize, array('Crop' => true, 'SourceX' => $this->Form->getValue('x'), 'SourceY' => $this->Form->getValue('y'), 'SourceWidth' => $this->Form->getValue('w'), 'SourceHeight' => $this->Form->getValue('h')));
             // Save new profile picture.
             $Parsed = $Upload->SaveAs($Source, changeBasename($NewPhoto, 'p%s'));
             $UserPhoto = sprintf($Parsed['SaveFormat'], $NewPhoto);
             // Save the new photo info.
             Gdn::userModel()->setField($this->User->UserID, 'Photo', $UserPhoto);
             // Remove the old profile picture.
             @$Upload->delete($Basename);
         } catch (Exception $Ex) {
             $this->Form->addError($Ex);
         }
         // If there were no problems, redirect back to the user account
         if ($this->Form->errorCount() == 0) {
             redirect(userUrl($this->User, '', 'picture'));
             $this->informMessage(sprite('Check', 'InformSprite') . t('Your changes have been saved.'), 'Dismissable AutoDismiss HasSprite');
         }
     }
     // Delete the source image if it is externally hosted.
     if ($PhotoParsed['Type']) {
         @unlink($Source);
     }
     $this->title(t('Edit My Thumbnail'));
     $this->_setBreadcrumbs(t('Edit My Thumbnail'), '/profile/thumbnail');
     $this->render();
 }
Пример #6
0
 /**
  * Saves the avatar to /uploads in two sizes:
  *   p* : The profile-sized image, which is constrained by Garden.Profile.MaxWidth and Garden.Profile.MaxHeight.
  *   n* : The thumbnail-sized image, which is constrained and cropped according to Garden.Thumbnail.Size.
  * Also deletes the old avatars.
  *
  * @param string $source The path to the local copy of the image.
  * @param array $thumbOptions The options to save the thumbnail-sized avatar with.
  * @param Gdn_UploadImage|null $upload The upload object.
  * @return bool 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;
 }