/** * Delete image */ public function removeImage() { // Check for request forgeries. JSession::checkToken('get') or jexit(JText::_('JINVALID_TOKEN')); // Check for registered user $userId = JFactory::getUser()->get('id'); if (!$userId) { $redirectOptions = array('force_direction' => 'index.php?option=com_users&view=login'); $this->displayNotice(JText::_('COM_SOCIALCOMMUNITY_ERROR_NOT_LOG_IN'), $redirectOptions); return; } $redirectOptions = array('view' => 'form', 'layout' => 'avatar'); try { $params = JComponentHelper::getParams('com_socialcommunity'); $filesystemHelper = new Prism\Filesystem\Helper($params); jimport('Prism.libs.init'); $storageFilesystem = $filesystemHelper->getFilesystem(); $mediaFolder = $filesystemHelper->getMediaFolder($userId); $model = $this->getModel(); $model->removeImage($userId, $mediaFolder, $storageFilesystem); } catch (Exception $e) { JLog::add($e->getMessage()); throw new Exception(JText::_('COM_SOCIALCOMMUNITY_ERROR_SYSTEM')); } $this->displayMessage(JText::_('COM_SOCIALCOMMUNITY_IMAGE_DELETED'), $redirectOptions); }
/** * Pre-processor for $table->delete($pk) * * @param mixed $pk An optional primary key value to delete. If not set the instance property value is used. * * @return void * * @since 3.1.2 * @throws UnexpectedValueException */ public function onAfterDelete($pk) { $params = JComponentHelper::getParams('com_gamification'); /** @var $params Joomla\Registry\Registry */ $filesystemHelper = new Prism\Filesystem\Helper($params); $mediaFolder = $filesystemHelper->getMediaFolder(); if ($this->table->get('image') !== null and $this->table->get('image') !== '') { $file = JPath::clean(JPATH_ROOT . DIRECTORY_SEPARATOR . $mediaFolder . DIRECTORY_SEPARATOR . $this->table->get('image')); if (JFile::exists($file)) { JFile::delete($file); } } if ($this->table->get('image_small') !== null and $this->table->get('image_small') !== '') { $file = JPath::clean(JPATH_ROOT . DIRECTORY_SEPARATOR . $mediaFolder . DIRECTORY_SEPARATOR . $this->table->get('image_small')); if (JFile::exists($file)) { JFile::delete($file); } } if ($this->table->get('image_square') !== null and $this->table->get('image_square') !== '') { $file = JPath::clean(JPATH_ROOT . DIRECTORY_SEPARATOR . $mediaFolder . DIRECTORY_SEPARATOR . $this->table->get('image_square')); if (JFile::exists($file)) { JFile::delete($file); } } }
public function display($tpl = null) { $this->option = JFactory::getApplication()->input->get('option'); $this->state = $this->get('State'); $this->item = $this->get('Item'); $this->form = $this->get('Form'); // Load the component parameters. $params = JComponentHelper::getParams($this->option); $filesystemHelper = new Prism\Filesystem\Helper($params); $this->mediaFolder = $filesystemHelper->getMediaFolder(); // Prepare actions, behaviors, scripts and document $this->addToolbar(); $this->setDocument(); parent::display($tpl); }
public function display($tpl = null) { $this->option = JFactory::getApplication()->input->get('option'); $this->state = $this->get('State'); $this->item = $this->get('Item'); $this->form = $this->get('Form'); // Load the component parameters. $params = JComponentHelper::getParams($this->option); $filesystemHelper = new Prism\Filesystem\Helper($params); $this->mediaFolder = $filesystemHelper->getMediaFolder(); // Prepare contexts. $achievements = new Gamification\Achievement\Achievements(JFactory::getDbo()); $contexts = $achievements->getContexts(); $js = 'var gfyContexts = ' . json_encode($contexts) . ';'; $this->document->addScriptDeclaration($js); // Prepare actions, behaviors, scripts and document $this->addToolbar(); $this->setDocument(); parent::display($tpl); }
private function createProfile($userId, $name) { $data = array('user_id' => (int) $userId, 'name' => $name, 'alias' => $name, 'active' => Prism\Constants::ACTIVE); $profile = new Socialcommunity\Profile\Profile($this->db); $profile->bind($data); $profile->store(); $params = JComponentHelper::getParams('com_socialcommunity'); /** @var $params Joomla\Registry\Registry */ $filesystemHelper = new Prism\Filesystem\Helper($params); // If the filesystem is local, create a user folder. if ($filesystemHelper->isLocal()) { $mediaFolder = JPath::clean(JPATH_BASE . '/' . $filesystemHelper->getMediaFolder($userId), '/'); if (!JFolder::exists($mediaFolder)) { JFolder::create($mediaFolder); } } }
/** * Store the file in a folder of the extension. * * @param array $image * @param bool $resizeImage * * @throws \RuntimeException * @throws \Exception * @throws \InvalidArgumentException * * @return array */ public function uploadImage($image, $resizeImage = false) { $app = JFactory::getApplication(); /** @var $app JApplicationSite */ $uploadedFile = ArrayHelper::getValue($image, 'tmp_name'); $uploadedName = ArrayHelper::getValue($image, 'name'); $errorCode = ArrayHelper::getValue($image, 'error'); $params = JComponentHelper::getParams($this->option); /** @var $params Joomla\Registry\Registry */ $filesystemHelper = new Prism\Filesystem\Helper($params); $mediaFolder = $filesystemHelper->getMediaFolder(); $destinationFolder = JPath::clean(JPATH_ROOT . DIRECTORY_SEPARATOR . $mediaFolder); $temporaryFolder = $app->get('tmp_path'); // Joomla! media extension parameters $mediaParams = JComponentHelper::getParams('com_media'); /** @var $mediaParams Joomla\Registry\Registry */ $file = new Prism\File\File(); // Prepare size validator. $KB = 1024 * 1024; $fileSize = (int) $app->input->server->get('CONTENT_LENGTH'); $uploadMaxSize = $mediaParams->get('upload_maxsize') * $KB; // Prepare file validators. $sizeValidator = new Prism\File\Validator\Size($fileSize, $uploadMaxSize); $serverValidator = new Prism\File\Validator\Server($errorCode, array(UPLOAD_ERR_NO_FILE)); $imageValidator = new Prism\File\Validator\Image($uploadedFile, $uploadedName); // Get allowed mime types from media manager options $mimeTypes = explode(',', $mediaParams->get('upload_mime')); $imageValidator->setMimeTypes($mimeTypes); // Get allowed image extensions from media manager options $imageExtensions = explode(',', $mediaParams->get('image_extensions')); $imageValidator->setImageExtensions($imageExtensions); $file->addValidator($sizeValidator)->addValidator($serverValidator)->addValidator($imageValidator); // Validate the file if (!$file->isValid()) { throw new RuntimeException($file->getError()); } // Generate temporary file name $ext = strtolower(JFile::makeSafe(JFile::getExt($image['name']))); $generatedName = Prism\Utilities\StringHelper::generateRandomString(); $temporaryFile = $generatedName . '_reward.' . $ext; $temporaryDestination = JPath::clean($temporaryFolder . DIRECTORY_SEPARATOR . $temporaryFile); // Prepare uploader object. $uploader = new Prism\File\Uploader\Local($uploadedFile); $uploader->setDestination($temporaryDestination); // Upload temporary file $file->setUploader($uploader); $file->upload(); $temporaryFile = $file->getFile(); if (!is_file($temporaryFile)) { throw new Exception('COM_GAMIFICATION_ERROR_FILE_CANT_BE_UPLOADED'); } // Resize image $image = new JImage(); $image->loadFile($temporaryFile); if (!$image->isLoaded()) { throw new Exception(JText::sprintf('COM_GAMIFICATION_ERROR_FILE_NOT_FOUND', $temporaryDestination)); } $imageName = $generatedName . '_image.png'; $smallName = $generatedName . '_small.png'; $squareName = $generatedName . '_square.png'; $imageFile = $destinationFolder . DIRECTORY_SEPARATOR . $imageName; $smallFile = $destinationFolder . DIRECTORY_SEPARATOR . $smallName; $squareFile = $destinationFolder . DIRECTORY_SEPARATOR . $squareName; $scaleOption = $params->get('image_resizing_scale', JImage::SCALE_INSIDE); // Create main image if (!$resizeImage) { $image->toFile($imageFile, IMAGETYPE_PNG); } else { $width = $params->get('image_width', 200); $height = $params->get('image_height', 200); $image->resize($width, $height, false, $scaleOption); $image->toFile($imageFile, IMAGETYPE_PNG); } // Create small image $width = $params->get('image_small_width', 100); $height = $params->get('image_small_height', 100); $image->resize($width, $height, false, $scaleOption); $image->toFile($smallFile, IMAGETYPE_PNG); // Create square image $width = $params->get('image_square_width', 50); $height = $params->get('image_square_height', 50); $image->resize($width, $height, false, $scaleOption); $image->toFile($squareFile, IMAGETYPE_PNG); $names = array('image' => $imageName, 'image_small' => $smallName, 'image_square' => $squareName); // Remove the temporary file. if (JFile::exists($temporaryFile)) { JFile::delete($temporaryFile); } return $names; }
/** * Store the file in a folder of the extension. * * @param array $image * * @throws \InvalidArgumentException * @throws \Exception * @throws \UnexpectedValueException * @throws \RuntimeException * * @return string */ public function uploadImage($image) { $app = JFactory::getApplication(); /** @var $app JApplicationSite */ $uploadedFile = ArrayHelper::getValue($image, 'tmp_name'); $uploadedName = ArrayHelper::getValue($image, 'name'); $errorCode = ArrayHelper::getValue($image, 'error'); $params = JComponentHelper::getParams($this->option); /** @var $params Joomla\Registry\Registry */ $filesystemHelper = new Prism\Filesystem\Helper($params); $mediaFolder = $filesystemHelper->getMediaFolder(); $destinationFolder = JPath::clean(JPATH_ROOT . DIRECTORY_SEPARATOR . $mediaFolder); // Joomla! media extension parameters $mediaParams = JComponentHelper::getParams('com_media'); /** @var $mediaParams Joomla\Registry\Registry */ $file = new Prism\File\File(); // Prepare size validator. $KB = 1024 * 1024; $fileSize = (int) $app->input->server->get('CONTENT_LENGTH'); $uploadMaxSize = $mediaParams->get('upload_maxsize') * $KB; // Prepare file validators. $sizeValidator = new Prism\File\Validator\Size($fileSize, $uploadMaxSize); $serverValidator = new Prism\File\Validator\Server($errorCode, array(UPLOAD_ERR_NO_FILE)); $imageValidator = new Prism\File\Validator\Image($uploadedFile, $uploadedName); // Get allowed mime types from media manager options $mimeTypes = explode(',', $mediaParams->get('upload_mime')); $imageValidator->setMimeTypes($mimeTypes); // Get allowed image extensions from media manager options $imageExtensions = explode(',', $mediaParams->get('image_extensions')); $imageValidator->setImageExtensions($imageExtensions); $file->addValidator($sizeValidator)->addValidator($serverValidator)->addValidator($imageValidator); // Validate the file if (!$file->isValid()) { throw new RuntimeException($file->getError()); } // Generate temporary file name $ext = strtolower(JFile::makeSafe(JFile::getExt($image['name']))); $generatedName = Prism\Utilities\StringHelper::generateRandomString(16); $imageName = $generatedName . '_badge.' . $ext; $destination = JPath::clean($destinationFolder . DIRECTORY_SEPARATOR . $imageName); // Prepare uploader object. $uploader = new Prism\File\Uploader\Local($uploadedFile); $uploader->setDestination($destination); // Upload temporary file $file->setUploader($uploader); $file->upload(); $source = $file->getFile(); return basename($source); }
public function cropImage() { // Check for request forgeries. JSession::checkToken() or jexit(JText::_('JINVALID_TOKEN')); $app = JFactory::getApplication(); /** @var $app JApplicationSite */ $response = new Prism\Response\Json(); $userId = JFactory::getUser()->get('id'); if (!$userId) { $response->setTitle(JText::_('COM_SOCIALCOMMUNITY_FAILURE'))->setText(JText::_('COM_SOCIALCOMMUNITY_ERROR_NOT_LOG_IN'))->failure(); echo $response; $app->close(); } // Get the model $model = $this->getModel(); /** @var $model SocialcommunityModelAvatar */ $params = JComponentHelper::getParams('com_socialcommunity'); $filesystemHelper = new Prism\Filesystem\Helper($params); // Get the filename from the session. $fileName = basename($app->getUserState(Socialcommunity\Constants::TEMPORARY_IMAGE_CONTEXT)); $temporaryFolder = $filesystemHelper->getTemporaryMediaFolder(JPATH_BASE); $temporaryFile = JPath::clean($temporaryFolder . '/' . $fileName); if (!$fileName or !JFile::exists($temporaryFile)) { $response->setTitle(JText::_('COM_SOCIALCOMMUNITY_FAILURE'))->setText(JText::_('COM_SOCIALCOMMUNITY_ERROR_FILE_DOES_NOT_EXIST'))->failure(); echo $response; $app->close(); } $imageUrl = ''; try { // Get the folder where the images will be stored $params = JComponentHelper::getParams('com_socialcommunity'); $options = array('width' => $this->input->getFloat('width'), 'height' => $this->input->getFloat('height'), 'x' => $this->input->getFloat('x'), 'y' => $this->input->getFloat('y'), 'destination' => $temporaryFolder); // Resize the picture. $images = $model->cropImage($temporaryFile, $options, $params); jimport('Prism.libs.init'); $temporaryAdapter = new League\Flysystem\Adapter\Local($temporaryFolder); $temporaryFilesystem = new League\Flysystem\Filesystem($temporaryAdapter); $storageFilesystem = $filesystemHelper->getFilesystem(); $manager = new League\Flysystem\MountManager(['temporary' => $temporaryFilesystem, 'storage' => $storageFilesystem]); $mediaFolder = $filesystemHelper->getMediaFolder($userId); $model->moveImages($images, $mediaFolder, $manager); $model->storeImages($userId, $images, $mediaFolder, $storageFilesystem); // Prepare URL to the image. $imageName = basename(Joomla\Utilities\ArrayHelper::getValue($images, 'image_profile')); $imageUrl = $filesystemHelper->getMediaFolderUri($userId) . '/' . $imageName; $app->setUserState(Socialcommunity\Constants::TEMPORARY_IMAGE_CONTEXT, null); } catch (RuntimeException $e) { $response->setTitle(JText::_('COM_SOCIALCOMMUNITY_FAILURE'))->setText($e->getMessage())->failure(); echo $response; $app->close(); } catch (Exception $e) { JLog::add($e->getMessage(), JLog::DEBUG); $response->setTitle(JText::_('COM_SOCIALCOMMUNITY_FAILURE'))->setText(JText::_('COM_SOCIALCOMMUNITY_ERROR_SYSTEM'))->failure(); echo $response; $app->close(); } $response->setTitle(JText::_('COM_SOCIALCOMMUNITY_SUCCESS'))->setText(JText::_('COM_SOCIALCOMMUNITY_IMAGE_SAVED'))->setData($imageUrl)->success(); echo $response; $app->close(); }
/** * Delete image */ public function removeImage() { // Check for request forgeries. JSession::checkToken('get') or jexit(JText::_('JINVALID_TOKEN')); $itemId = $this->input->getInt('id', 0); $profile = new Socialcommunity\Profile\Profile(JFactory::getDbo()); $profile->load($itemId); $redirectOptions = array('view' => 'profile', 'id' => $itemId); if (!$profile->getId()) { $this->displayNotice(JText::_('COM_SOCIALCOMMUNITY_INVALID_PROFILE'), $redirectOptions); return; } try { $params = JComponentHelper::getParams('com_socialcommunity'); $filesystemHelper = new Prism\Filesystem\Helper($params); jimport('Prism.libs.init'); $storageFilesystem = $filesystemHelper->getFilesystem(); $mediaFolder = $filesystemHelper->getMediaFolder($profile->getUserId()); $model = $this->getModel(); $model->removeImage($itemId, $mediaFolder, $storageFilesystem); } catch (Exception $e) { JLog::add($e->getMessage()); throw new Exception(JText::_('COM_SOCIALCOMMUNITY_ERROR_SYSTEM')); } $this->displayMessage(JText::_('COM_SOCIALCOMMUNITY_IMAGE_DELETED'), $redirectOptions); }
/** * Prepare and sanitise the table prior to saving. * * @param SocialCommunityTableProfile $table * @param array $data * * @since 1.6 */ protected function prepareImages($table, $data) { // Prepare image if (!empty($data['image'])) { $params = JComponentHelper::getParams($this->option); /** @var $params Joomla\Registry\Registry */ jimport('Prism.libs.init'); $filesystemHelper = new Prism\Filesystem\Helper($params); $mediaFolder = $filesystemHelper->getMediaFolder($data['user_id']); $storageFilesystem = $filesystemHelper->getFilesystem(); // Delete old image if I upload a new one if ($table->get('image')) { $this->deleteImages($table, $mediaFolder, $storageFilesystem); } // Move the images from temporary to media folder. $this->moveImages($data, $mediaFolder, $storageFilesystem); $table->set('image', $data['image']); $table->set('image_small', $data['image_small']); $table->set('image_icon', $data['image_icon']); $table->set('image_square', $data['image_square']); } }