Ejemplo n.º 1
0
 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;
 }
Ejemplo n.º 2
0
 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;
     }
 }