/**
  * 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();
 }