public function createImage($params, $storeTo) { // @task: Retrieve the configs. $config = EasyBlogHelper::getConfig(); require_once EBLOG_CLASSES . DIRECTORY_SEPARATOR . 'easysimpleimage.php'; // @rule: Generate a thumbnail for each uploaded images $image = new EasySimpleImage(); $image->load($this->original); $resizeType = isset($params->resize) ? $params->resize : EBLOG_IMAGE_DEFAULT_RESIZE; $originalWidth = $image->getWidth(); $originalHeight = $image->getHeight(); // If quality is not given, use default quality given in configuration if (!isset($params->quality)) { $params->quality = $config->get('main_image_quality'); } // TODO: This should be done in the crop function itself if ($resizeType == 'crop') { if ($originalWidth < $params->width || $originalHeight < $params->height) { $resizeType = 'fill'; } } switch ($resizeType) { case 'crop': $image->crop($params->width, $params->height); break; case 'fit': $image->resizeToFit($params->width, $params->height); break; case 'within': $image->resizeWithin($params->width, $params->height); break; case 'fill': default: $image->resizeToFill($params->width, $params->height); break; } $image->save($storeTo, $image->image_type, $params->quality); return true; }
public static function uploadMediaAvatar($mediaType, $mediaTable, $isFromBackend = false) { jimport('joomla.utilities.error'); jimport('joomla.filesystem.file'); jimport('joomla.filesystem.folder'); $my = JFactory::getUser(); $mainframe = JFactory::getApplication(); $config = EasyBlogHelper::getConfig(); $acl = EasyBlogACLHelper::getRuleSet(); // required params $layout_type = $mediaType == 'category' ? 'categories' : 'teamblogs'; $view_type = $mediaType == 'category' ? 'categories' : 'teamblogs'; $default_avatar_type = $mediaType == 'category' ? 'default_category.png' : 'default_team.png'; if (!$isFromBackend && $mediaType == 'category') { if (empty($acl->rules->upload_cavatar)) { $url = 'index.php?option=com_easyblog&view=dashboard&layout=' . $layout_type; EasyBlogHelper::setMessageQueue(JText::_('COM_EASYBLOG_NO_PERMISSION_TO_UPLOAD_AVATAR'), 'warning'); $mainframe->redirect(EasyBlogRouter::_($url, false)); } } $avatar_config_path = $mediaType == 'category' ? $config->get('main_categoryavatarpath') : $config->get('main_teamavatarpath'); $avatar_config_path = rtrim($avatar_config_path, '/'); $avatar_config_path = str_replace('/', DIRECTORY_SEPARATOR, $avatar_config_path); $upload_path = JPATH_ROOT . DIRECTORY_SEPARATOR . $avatar_config_path; $rel_upload_path = $avatar_config_path; $err = null; $file = JRequest::getVar('Filedata', '', 'files', 'array'); //check whether the upload folder exist or not. if not create it. if (!JFolder::exists($upload_path)) { if (!JFolder::create($upload_path)) { // Redirect if (!$isFromBackend) { EasyBlogHelper::setMessageQueue(JText::_('COM_EASYBLOG_IMAGE_UPLOADER_FAILED_TO_CREATE_UPLOAD_FOLDER'), 'error'); self::setRedirect(EasyBlogRouter::_('index.php?option=com_easyblog&view=dashboard&layout=' . $layout_type, false)); } else { //from backend self::setRedirect(EasyBlogRouter::_('index.php?option=com_easyblog&view=' . $layout_type, false), JText::_('COM_EASYBLOG_IMAGE_UPLOADER_FAILED_TO_CREATE_UPLOAD_FOLDER'), 'error'); } return; } else { // folder created. now copy index.html into this folder. if (!JFile::exists($upload_path . DIRECTORY_SEPARATOR . 'index.html')) { $targetFile = JPATH_ROOT . DIRECTORY_SEPARATOR . 'components' . DIRECTORY_SEPARATOR . 'com_easyblog' . DIRECTORY_SEPARATOR . 'index.html'; $destFile = $upload_path . DIRECTORY_SEPARATOR . 'index.html'; if (JFile::exists($targetFile)) { JFile::copy($targetFile, $destFile); } } } } //makesafe on the file $file['name'] = $mediaTable->id . '_' . JFile::makeSafe($file['name']); if (isset($file['name'])) { $target_file_path = $upload_path; $relative_target_file = $rel_upload_path . DIRECTORY_SEPARATOR . $file['name']; $target_file = JPath::clean($target_file_path . DIRECTORY_SEPARATOR . JFile::makeSafe($file['name'])); $isNew = false; //include_once(JPATH_ROOT.DIRECTORY_SEPARATOR.'components'.DIRECTORY_SEPARATOR.'com_easyblog'.DIRECTORY_SEPARATOR.'helpers'.DIRECTORY_SEPARATOR.'image.php'); require_once EBLOG_HELPERS . DIRECTORY_SEPARATOR . 'image.php'; require_once EBLOG_CLASSES . DIRECTORY_SEPARATOR . 'easysimpleimage.php'; if (!EasyImageHelper::canUpload($file, $err)) { if (!$isFromBackend) { EasyBlogHelper::setMessageQueue(JText::_($err), 'error'); $mainframe->redirect(EasyBlogRouter::_('index.php?option=com_easyblog&view=dashboard&layout=' . $layout_type, false)); } else { //from backend $mainframe->redirect(EasyBlogRouter::_('index.php?option=com_easyblog&view=' . $view_type, false), JText::_($err), 'error'); } return; } if (0 != (int) $file['error']) { if (!$isFromBackend) { EasyBlogHelper::setMessageQueue($file['error'], 'error'); $mainframe->redirect(EasyBlogRouter::_('index.php?option=com_easyblog&view=dashboard&layout=' . $layout_type, false)); } else { //from backend $mainframe->redirect(EasyBlogRouter::_('index.php?option=com_easyblog&view=' . $view_type, false), $file['error'], 'error'); } return; } //rename the file 1st. $oldAvatar = empty($mediaTable->avatar) ? $default_avatar_type : $mediaTable->avatar; $tempAvatar = ''; if ($oldAvatar != $default_avatar_type) { $session = JFactory::getSession(); $sessionId = $session->getToken(); $fileExt = JFile::getExt(JPath::clean($target_file_path . DIRECTORY_SEPARATOR . $oldAvatar)); $tempAvatar = JPath::clean($target_file_path . DIRECTORY_SEPARATOR . $sessionId . '.' . $fileExt); JFile::move($target_file_path . DIRECTORY_SEPARATOR . $oldAvatar, $tempAvatar); } else { $isNew = true; } if (JFile::exists($target_file)) { if ($oldAvatar != $default_avatar_type) { //rename back to the previous one. JFile::move($tempAvatar, $target_file_path . DIRECTORY_SEPARATOR . $oldAvatar); } if (!$isFromBackend) { EasyBlogHelper::setMessageQueue(JText::sprintf('ERROR.FILE_ALREADY_EXISTS', $relative_target_file), 'error'); $mainframe->redirect(EasyBlogRouter::_('index.php?option=com_easyblog&view=dashboard&layout=' . $layout_type, false)); } else { //from backend $mainframe->redirect(EasyBlogRouter::_('index.php?option=com_easyblog&view=' . $view_type, false), JText::sprintf('ERROR.FILE_ALREADY_EXISTS', $relative_target_file), 'error'); } return; } if (JFolder::exists($target_file)) { if ($oldAvatar != $default_avatar_type) { //rename back to the previous one. JFile::move($tempAvatar, $target_file_path . DIRECTORY_SEPARATOR . $oldAvatar); } if (!$isFromBackend) { //JError::raiseNotice(100, JText::sprintf('ERROR.FOLDER_ALREADY_EXISTS',$relative_target_file)); EasyBlogHelper::setMessageQueue(JText::sprintf('ERROR.FOLDER_ALREADY_EXISTS', $relative_target_file), 'error'); $mainframe->redirect(EasyBlogRouter::_('index.php?option=com_easyblog&view=dashboard&layout=' . $layout_type, false)); } else { //from backend $mainframe->redirect(EasyBlogRouter::_('index.php?option=com_easyblog&view=' . $view_type, false), JText::sprintf('ERROR.FILE_ALREADY_EXISTS', $relative_target_file), 'error'); } return; } $configImageWidth = EBLOG_AVATAR_LARGE_WIDTH; $configImageHeight = EBLOG_AVATAR_LARGE_HEIGHT; $image = new EasySimpleImage(); $image->load($file['tmp_name']); $image->resizeToFill($configImageWidth, $configImageHeight); $image->save($target_file, $image->image_type); //now we update the user avatar. If needed, we remove the old avatar. if ($oldAvatar != $default_avatar_type) { if (JFile::exists($tempAvatar)) { JFile::delete($tempAvatar); } } return JFile::makeSafe($file['name']); } else { return $default_avatar_type; } }
public static function upload($storagePath, $fileName, $fileItem) { require_once EBLOG_CLASSES . DIRECTORY_SEPARATOR . 'easysimpleimage.php'; $cfg = EasyBlogHelper::getConfig(); $user = JFactory::getUser(); // @task: Resize original image if necessary. Otherwise, just copy the file over. $originalStorage = JPath::clean($storagePath . DIRECTORY_SEPARATOR . $fileName); // check if folder not exists, then we need to create first or else the copy operation will fail. if (!JFolder::exists($storagePath)) { JFolder::create($storagePath); } $image = new EasySimpleImage(); $image->load($fileItem['tmp_name']); if ($cfg->get('main_resize_original_image')) { // @task: Get the maximum width / height for the original images. $maxWidth = $cfg->get('main_original_image_width'); $maxHeight = $cfg->get('main_original_image_height'); if ($image->getWidth() > $maxWidth || $image->getHeight() > $maxHeight) { $image->resizeWithin($maxWidth, $maxHeight); $state = $image->save($originalStorage, $image->image_type, $cfg->get('main_original_image_quality')); } else { $state = JFile::copy($fileItem['tmp_name'], $originalStorage); } } else { // @task: Since no resizing is required, just upload the whole file. if ($image->orientationFixed) { $state = $image->save($originalStorage, $image->image_type, $cfg->get('main_original_image_quality')); } else { $state = JFile::copy($fileItem['tmp_name'], $originalStorage); } } // @task: Once the original storage has been taken care off, we need to find the sizes that are required to be resized. require_once EBLOG_CLASSES . DIRECTORY_SEPARATOR . 'image.php'; $imageObj = new EasyBlogImage($fileName, $storagePath, ''); // @TODO: Cater for error checking here. $imageObj->initDefaultSizes(); if ($state === false) { return JText::_('COM_EASYBLOG_MM_UNABLE_TO_SAVE_FILE'); } return true; }
public function upload($folder, $filename, $file, $baseUri, $storagePath, $subfolder = '') { require_once EBLOG_CLASSES . DIRECTORY_SEPARATOR . 'easysimpleimage.php'; $config = EasyBlogHelper::getConfig(); $user = JFactory::getUser(); if (isset($file['name'])) { if ($config->get('main_resize_original_image')) { $maxWidth = $config->get('main_original_image_width'); $maxHeight = $config->get('main_original_image_height'); $image = new EasySimpleImage(); $image->load($file['tmp_name']); $image->resizeWithin($maxWidth, $maxHeight); $uploadStatus = $image->save($storagePath, $image->image_type, $config->get('main_original_image_quality')); } else { $uploadStatus = JFile::upload($file['tmp_name'], $storagePath); } if ($uploadStatus) { // $activity = new stdClass(); // $activity->actor_id = $user->id; // $activity->target_id = '0'; // $activity->context_type = 'photo'; // $activity->context_id = '0'; // $activity->verb = 'upload'; // EasyBlogHelper::activityLog( $activity ); } // @task: thumbnail's file name $storagePathThumb = JPath::clean($folder . DIRECTORY_SEPARATOR . EBLOG_MEDIA_THUMBNAIL_PREFIX . $filename); // @rule: Generate a thumbnail for each uploaded images $image = new EasySimpleImage(); $image->load($storagePath); $image->resizeWithin($config->get('main_thumbnail_width'), $config->get('main_thumbnail_height')); $image->save($storagePathThumb, $image->image_type, $config->get('main_thumbnail_quality')); if (!$uploadStatus) { return $this->getMessageObj(EBLOG_MEDIA_PERMISSION_ERROR, JText::_('COM_EASYBLOG_IMAGE_MANAGER_UPLOAD_ERROR')); } else { // file uploaded. Now we test if the index.html was there or not. // if not, copy from easyblog root into this folder if (!JFile::exists($folder . DIRECTORY_SEPARATOR . 'index.html')) { $targetFile = JPATH_ROOT . DIRECTORY_SEPARATOR . 'components' . DIRECTORY_SEPARATOR . 'com_easyblog' . DIRECTORY_SEPARATOR . 'index.html'; $destFile = $folder . DIRECTORY_SEPARATOR . 'index.html'; if (JFile::exists($targetFile)) { JFile::copy($targetFile, $destFile); } } return self::getMessageObj(EBLOG_MEDIA_UPLOAD_SUCCESS, JText::_('COM_EASYBLOG_IMAGE_MANAGER_UPLOAD_SUCCESS'), EasyBlogHelper::getHelper('ImageData')->getObject($folder, $filename, $baseUri, $subfolder)); } } else { return self::getMessageObj(EBLOG_MEDIA_TRANSPORT_ERROR, JText::_('COM_EASYBLOG_MEDIA_MANAGER_NO_UPLOAD_FILE')); } return $response; }
public static function getThumbnailImage($img) { $srcpattern = '/src=".*?"/'; preg_match($srcpattern, $img, $src); if (isset($src[0])) { $imagepath = trim(str_ireplace('src=', '', $src[0]), '"'); $segment = explode('/', $imagepath); $file = end($segment); $thumbnailpath = str_ireplace($file, 'thumb_' . $file, implode('/', $segment)); if (!JFile::exists($thumbnailpath)) { $image = new EasySimpleImage(); $image->load($imagepath); $image->resize(64, 64); $image->save($thumbnailpath); } $newSrc = 'src="' . $thumbnailpath . '"'; } else { return false; } $oldAttributes = array('src' => $srcpattern, 'width' => '/width=".*?"/', 'height' => '/height=".*?"/'); $newAttributes = array('src' => $newSrc, 'width' => '', 'height' => ''); return preg_replace($oldAttributes, $newAttributes, $img); }