public function Picture($UserReference = '', $Username = '') { $this->Permission('Garden.SignIn.Allow'); $Session = Gdn::Session(); if (!$Session->IsValid()) $this->Form->AddError('You must be authenticated in order to use this form.'); $ImageManipOk = FALSE; 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."))); } $this->GetUserInfo($UserReference, $Username); $this->Form->SetModel($this->UserModel); $this->Form->AddHidden('UserID', $this->User->UserID); if ($this->Form->AuthenticatedPostBack() === TRUE) { $UploadImage = new Gdn_UploadImage(); try { // Validate the upload $TmpImage = $UploadImage->ValidateUpload('Picture'); // Generate the target image name. $TargetImage = $UploadImage->GenerateTargetName(PATH_LOCAL_UPLOADS); $ImageBaseName = pathinfo($TargetImage, PATHINFO_BASENAME); // Delete any previously uploaded images. $UploadImage->Delete(ChangeBasename($this->User->Photo, 'p%s')); // Don't delete this one because it hangs around in activity streams: // @unlink(PATH_ROOT.'/uploads/'.ChangeBasename($this->User->Photo, 't%s')); $UploadImage->Delete(ChangeBasename($this->User->Photo, 'n%s')); // Save the uploaded image in profile size. $Props = $UploadImage->SaveImageAs( $TmpImage, 'userpics/p'.$ImageBaseName, Gdn::Config('Garden.Profile.MaxHeight', 1000), Gdn::Config('Garden.Profile.MaxWidth', 250) ); $UserPhoto = sprintf($Props['SaveFormat'], 'userpics/'.$ImageBaseName); // Save the uploaded image in preview size $UploadImage->SaveImageAs( $TmpImage, 'userpics/t'.$ImageBaseName, Gdn::Config('Garden.Preview.MaxHeight', 100), Gdn::Config('Garden.Preview.MaxWidth', 75) ); // Save the uploaded image in thumbnail size $ThumbSize = Gdn::Config('Garden.Thumbnail.Size', 50); $UploadImage->SaveImageAs( $TmpImage, 'userpics/n'.$ImageBaseName, $ThumbSize, $ThumbSize, TRUE ); } catch (Exception $Ex) { $this->Form->AddError($Ex); } // If there were no errors, associate the image with the user if ($this->Form->ErrorCount() == 0) { if (!$this->UserModel->Save(array('UserID' => $this->User->UserID, 'Photo' => $UserPhoto))) $this->Form->SetValidationResults($this->UserModel->ValidationResults()); } // If there were no problems, redirect back to the user account if ($this->Form->ErrorCount() == 0) Redirect('dashboard/profile/'.$this->ProfileUrl()); } if ($this->Form->ErrorCount() > 0) $this->DeliveryType(DELIVERY_TYPE_ALL); $this->Render(); }
/** * Set user's photo (avatar). * * @since 2.0.0 * @access public * @param mixed $UserReference Unique identifier, possible username or ID. * @param string $Username. */ public function Picture($UserReference = '', $Username = '', $UserID = '') { // Permission checks $this->Permission('Garden.Profiles.Edit'); $Session = Gdn::Session(); if (!$Session->IsValid()) { $this->Form->AddError('You must be authenticated in order to use this form.'); } // Check ability to manipulate image $ImageManipOk = FALSE; 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. $this->GetUserInfo($UserReference, $Username, $UserID, TRUE); $this->Form->SetModel($this->UserModel); $this->Form->AddHidden('UserID', $this->User->UserID); if ($this->Form->AuthenticatedPostBack() === TRUE) { $UploadImage = new Gdn_UploadImage(); try { // Validate the upload $TmpImage = $UploadImage->ValidateUpload('Picture'); // Generate the target image name. $TargetImage = $UploadImage->GenerateTargetName(PATH_LOCAL_UPLOADS, '', TRUE); $Basename = pathinfo($TargetImage, PATHINFO_BASENAME); $Subdir = StringBeginsWith(dirname($TargetImage), PATH_LOCAL_UPLOADS . '/', FALSE, TRUE); // Delete any previously uploaded image. $UploadImage->Delete(ChangeBasename($this->User->Photo, 'p%s')); // Save the uploaded image in profile size. $Props = $UploadImage->SaveImageAs($TmpImage, "userpics/{$Subdir}/p{$Basename}", C('Garden.Profile.MaxHeight', 1000), C('Garden.Profile.MaxWidth', 250), array('SaveGif' => C('Garden.Thumbnail.SaveGif'))); $UserPhoto = sprintf($Props['SaveFormat'], "userpics/{$Subdir}/{$Basename}"); // // Save the uploaded image in preview size // $UploadImage->SaveImageAs( // $TmpImage, // 'userpics/t'.$ImageBaseName, // Gdn::Config('Garden.Preview.MaxHeight', 100), // Gdn::Config('Garden.Preview.MaxWidth', 75) // ); // Save the uploaded image in thumbnail size $ThumbSize = Gdn::Config('Garden.Thumbnail.Size', 50); $UploadImage->SaveImageAs($TmpImage, "userpics/{$Subdir}/n{$Basename}", $ThumbSize, $ThumbSize, array('Crop' => TRUE, 'SaveGif' => C('Garden.Thumbnail.SaveGif'))); } catch (Exception $Ex) { $this->Form->AddError($Ex); } // If there were no errors, associate the image with the user if ($this->Form->ErrorCount() == 0) { if (!$this->UserModel->Save(array('UserID' => $this->User->UserID, 'Photo' => $UserPhoto), array('CheckExisting' => TRUE))) { $this->Form->SetValidationResults($this->UserModel->ValidationResults()); } } // If there were no problems, redirect back to the user account if ($this->Form->ErrorCount() == 0) { Redirect('dashboard/profile/' . $this->ProfileUrl()); } } if ($this->Form->ErrorCount() > 0) { $this->DeliveryType(DELIVERY_TYPE_ALL); } $this->Render(); }
/** * 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(); }
/** * Save an image from a field and delete any old image that's been uploaded. * * @param string $Field The name of the field. The image will be uploaded with the _New extension while the current image will be just the field name. * @param array $Options */ public function SaveImage($Field, $Options = array()) { $Upload = new Gdn_UploadImage(); $FileField = str_replace('.', '_', $Field); if (!GetValueR("{$FileField}_New.name", $_FILES)) { Trace("{$Field} not uploaded, returning."); return FALSE; } // First make sure the file is valid. try { $TmpName = $Upload->ValidateUpload($FileField . '_New', TRUE); if (!$TmpName) { return FALSE; } // no file uploaded. } catch (Exception $Ex) { $this->AddError($Ex); return FALSE; } // Get the file extension of the file. $Ext = GetValue('OutputType', $Options, trim($Upload->GetUploadedFileExtension(), '.')); if ($Ext == 'jpeg') { $Ext = 'jpg'; } Trace($Ext, 'Ext'); // The file is valid so let's come up with its new name. if (isset($Options['Name'])) { $Name = $Options['Name']; } elseif (isset($Options['Prefix'])) { $Name = $Options['Prefix'] . md5(microtime()) . '.' . $Ext; } else { $Name = md5(microtime()) . '.' . $Ext; } // We need to parse out the size. $Size = GetValue('Size', $Options); if ($Size) { if (is_numeric($Size)) { TouchValue('Width', $Options, $Size); TouchValue('Height', $Options, $Size); } elseif (preg_match('`(\\d+)x(\\d+)`i', $Size, $M)) { TouchValue('Width', $Options, $M[1]); TouchValue('Height', $Options, $M[2]); } } Trace($Options, "Saving image {$Name}."); try { $Parsed = $Upload->SaveImageAs($TmpName, $Name, GetValue('Height', $Options, ''), GetValue('Width', $Options, ''), $Options); Trace($Parsed, 'Saved Image'); $Current = $this->GetFormValue($Field); if ($Current && GetValue('DeleteOriginal', $Options, TRUE)) { // Delete the current image. Trace("Deleting original image: {$Current}."); if ($Current) { $Upload->Delete($Current); } } // Set the current value. $this->SetFormValue($Field, $Parsed['SaveName']); } catch (Exception $Ex) { $this->AddError($Ex); } }