/** * Settings page for uploading, deleting and cropping the default avatar. * * @throws Exception */ public function defaultAvatar() { $this->permission('Garden.Community.Manage'); $this->addSideMenu('dashboard/settings/avatars'); $this->title(t('Default Avatar')); $this->addJsFile('avatars.js'); $validation = new Gdn_Validation(); $configurationModel = new Gdn_ConfigurationModel($validation); $this->Form->setModel($configurationModel); if (($avatar = c('Garden.DefaultAvatar')) && $this->isUploadedDefaultAvatar($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', UserModel::getDefaultAvatarUrl()); } 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('DefaultAvatar', false)) { // New upload $thumbOptions = array('Crop' => true, 'SaveGif' => c('Garden.Thumbnail.SaveGif')); $newAvatar = $this->saveDefaultAvatars($tmpAvatar, $thumbOptions); } 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->saveDefaultAvatars($tmpAvatar, $thumbOptions); } } if ($this->Form->errorCount() == 0) { if ($newAvatar) { $this->deleteDefaultAvatars($avatar); $avatar = c('Garden.DefaultAvatar'); $thumbnailSize = c('Garden.Thumbnail.Size', 40); // Update crop properties. $basename = changeBasename($avatar, "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($avatar, "n%s"))); $crop->setSourceImageUrl(Gdn_UploadImage::url(changeBasename($avatar, "p%s"))); $this->setData('crop', $crop); } } $this->informMessage(t("Your settings have been saved.")); } } $this->render(); }
/** * Create and display a thumbnail of an uploaded file. */ public function utilityController_mediaThumbnail_create($sender, $media_id) { // When it makes it into core, it will be available in // functions.general.php require 'generate_thumbnail.php'; $model = new Gdn_Model('Media'); $media = $model->getID($media_id, DATASET_TYPE_ARRAY); if (!$media) { throw notFoundException('File'); } // Get actual path to the file. $upload = new Gdn_UploadImage(); $local_path = $upload->copyLocal(val('Path', $media)); if (!file_exists($local_path)) { throw notFoundException('File'); } $file_extension = pathinfo($local_path, PATHINFO_EXTENSION); // Generate new path for thumbnail $thumb_path = $this->getBaseUploadDestinationDir() . '/' . 'thumb'; // Grab full path with filename, and validate it. $thumb_destination_path = $this->getAbsoluteDestinationFilePath($local_path, $file_extension, $thumb_path); // Create thumbnail, and grab debug data from whole process. $thumb_payload = generate_thumbnail($local_path, $thumb_destination_path, array('height' => c('Plugins.FileUpload.ThumbnailHeight', 128))); if ($thumb_payload['success'] === true) { // Thumbnail dimensions $thumb_height = round($thumb_payload['result_height']); $thumb_width = round($thumb_payload['result_width']); // Move the thumbnail to its proper location. Calling SaveAs with // cloudfiles enabled will trigger the move to cloudfiles, so use // same path for each arg in SaveAs. The file will be removed from the local filesystem. $parsed = Gdn_Upload::parse($thumb_destination_path); $target = $thumb_destination_path; // $parsed['Name']; $Upload = new Gdn_Upload(); $filepath_parsed = $Upload->saveAs($thumb_destination_path, $target, array('source' => 'content')); // Save thumbnail information to DB. $model->save(array('MediaID' => $media_id, 'ThumbWidth' => $thumb_width, 'ThumbHeight' => $thumb_height, 'ThumbPath' => $filepath_parsed['SaveName'])); // Remove cf scratch copy, typically in cftemp, if there was actually a file pulled in from CF. if (strpos($local_path, 'cftemp') !== false) { if (!unlink($local_path)) { // Maybe add logging for local cf copies not deleted. } } $url = $filepath_parsed['Url']; } else { // Fix the thumbnail information so this isn't requested again and again. $model->save(array('MediaID' => $media_id, 'ImageWidth' => 0, 'ImageHeight' => 0, 'ThumbPath' => '')); $url = asset('/plugins/FileUpload/images/file.png'); } redirect($url, 301); }
/** * Create and display a thumbnail of an uploaded file. * * @param UtilityController $Sender * @param array $Args */ public function utilityController_thumbnail_create($Sender, $Args = array()) { $MediaID = array_shift($Args); if (!is_numeric($MediaID)) { array_unshift($Args, $MediaID); } $SubPath = implode('/', $Args); // Fix mauling of protocol:// URLs. $SubPath = preg_replace('/:\\/{1}/', '://', $SubPath); $Name = $SubPath; $Parsed = Gdn_Upload::parse($Name); // Get actual path to the file. $upload = new Gdn_UploadImage(); $Path = $upload->copyLocal($SubPath); if (!file_exists($Path)) { throw NotFoundException('File'); } // Figure out the dimensions of the upload. $ImageSize = getimagesize($Path); $SHeight = $ImageSize[1]; $SWidth = $ImageSize[0]; if (!in_array($ImageSize[2], array(IMAGETYPE_GIF, IMAGETYPE_JPEG, IMAGETYPE_PNG))) { if (is_numeric($MediaID)) { // Fix the thumbnail information so this isn't requested again and again. $Model = new MediaModel(); $Media = array('MediaID' => $MediaID, 'ImageWidth' => 0, 'ImageHeight' => 0, 'ThumbPath' => null); $Model->save($Media); } $Url = Asset('/plugins/FileUpload/images/file.png'); Redirect($Url, 301); } $Options = array(); $ThumbHeight = MediaModel::thumbnailHeight(); $ThumbWidth = MediaModel::thumbnailWidth(); if (!$ThumbHeight || $SHeight < $ThumbHeight) { $Height = $SHeight; $Width = $SWidth; } else { $Height = $ThumbHeight; $Width = round($Height * $SWidth / $SHeight); } if ($ThumbWidth && $Width > $ThumbWidth) { $Width = $ThumbWidth; if (!$ThumbHeight) { $Height = round($Width * $SHeight / $SWidth); } else { $Options['Crop'] = true; } } $TargetPath = "thumbnails/{$Parsed['Name']}"; $ThumbParsed = Gdn_UploadImage::saveImageAs($Path, $TargetPath, $Height, $Width, $Options); // Cleanup if we're using a scratch copy if ($ThumbParsed['Type'] != '' || $Path != MediaModel::pathUploads() . '/' . $SubPath) { @unlink($Path); } if (is_numeric($MediaID)) { // Save the thumbnail information. $Model = new MediaModel(); $Media = array('MediaID' => $MediaID, 'ThumbWidth' => $ThumbParsed['Width'], 'ThumbHeight' => $ThumbParsed['Height'], 'ThumbPath' => $ThumbParsed['SaveName']); $Model->save($Media); } $Url = $ThumbParsed['Url']; redirect($Url, 301); }
/** * Set user's photo (avatar). * * @since 2.0.0 * @access public * * @param mixed $userReference Unique identifier, possible username or ID. * @param string $username The username. * @param string $userID The user's ID. * * @throws Exception * @throws Gdn_UserException */ 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'); }