/** * Initialize dimensions available * * @since 5.0 * @access public * @param string * @return */ public function initDimensions($imagePath, $size = null) { $images = array(); // Ensure that the image really exists on the site. $exists = JFile::exists($imagePath); if (!$exists) { return EB::exception('Invalid file path provided to generate imagesets.', EASYBLOG_MSG_ERROR); } // Get the original image resource $original = EB::simpleimage(); $original->load($imagePath); // Get the original image file name $fileName = basename($imagePath); // Get the original image containing folder $folder = dirname($imagePath); // Determines if we should generate a single size or multiple sizes $sizes = $this->sizes; if (!is_null($size)) { $sizes = array($size); } // Determines if there's a specific size to generate foreach ($sizes as $size) { // Clone the original image to avoid original image width and height being modified $image = clone $original; $data = new stdClass(); $data->width = $this->config->get('main_image_' . $size . '_width'); $data->height = $this->config->get('main_image_' . $size . '_height'); $data->quality = $this->config->get('main_image_' . $size . '_quality'); $data->path = $folder . '/' . EBLOG_SYSTEM_VARIATION_PREFIX . '_' . $size . '_' . $fileName; // Everything should be resized using "resize within" method $resizeMode = 'resizeWithin'; // Resize the image $image->{$resizeMode}($data->width, $data->height); // Save the image $image->write($data->path, $data->quality); unset($image); $images[$size] = $data; } unset($original); return $images; }
/** * Create the blog image on the site * * @since 5.0 * @access public * @param string * @return */ public function createImage($params, $storage) { // Generate a thumbnail for each uploaded images $image = EB::simpleimage(); $image->load($this->original); $originalWidth = $image->getWidth(); $originalHeight = $image->getHeight(); // @TODO: Make this configurable in the future // Resize everything to be "resize within" by default $mode = 'within'; // If quality is not given, use default quality given in configuration if (!isset($params->quality)) { $params->quality = $this->config->get('main_image_quality'); } // If the resize method if ($mode == 'crop' && ($originalWidth < $params->width || $originaHeight < $params->height)) { $mode = 'fill'; } if ($mode == 'crop') { $image->crop($params->width, $params->height); } if ($mode == 'fit') { $image->resizeToFit($params->width, $params->height); } if ($mode == 'within') { $image->resizeWithin($params->width, $params->height); } if ($mode == 'fill') { $image->resizeToFill($params->width, $params->height); } // Save the image $image->save($storage, $image->type, $params->quality); return true; }
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 = EB::simpleimage(); $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); }
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 = EB::config(); $acl = EB::acl(); // 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 (!$acl->get('upload_cavatar')) { $url = 'index.php?option=com_easyblog&view=dashboard&layout=' . $layout_type; EB::info()->set(JText::_('COM_EASYBLOG_NO_PERMISSION_TO_UPLOAD_AVATAR'), 'warning'); $mainframe->redirect(EBR::_($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) { EB::info()->set(JText::_('COM_EASYBLOG_IMAGE_UPLOADER_FAILED_TO_CREATE_UPLOAD_FOLDER'), 'error'); $mainframe->redirect(EBR::_('index.php?option=com_easyblog&view=dashboard&layout=' . $layout_type, false)); } else { //from backend $mainframe->redirect(EBR::_('index.php?option=com_easyblog&view=' . $layout_type, false), JText::_('COM_EASYBLOG_IMAGE_UPLOADER_FAILED_TO_CREATE_UPLOAD_FOLDER'), 'error'); } return; } } //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; if (!EB::image()->canUpload($file, $error)) { if (!$isFromBackend) { EB::info()->set(JText::_($err), 'error'); $mainframe->redirect(EBR::_('index.php?option=com_easyblog&view=dashboard&layout=' . $layout_type, false)); } else { //from backend $mainframe->redirect(EBR::_('index.php?option=com_easyblog&view=' . $view_type, false), JText::_($err), 'error'); } return; } if (0 != (int) $file['error']) { if (!$isFromBackend) { EB::info()->set($file['error'], 'error'); $mainframe->redirect(EBR::_('index.php?option=com_easyblog&view=dashboard&layout=' . $layout_type, false)); } else { //from backend $mainframe->redirect(EBR::_('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) { EB::info()->set(JText::sprintf('ERROR.FILE_ALREADY_EXISTS', $relative_target_file), 'error'); $mainframe->redirect(EBR::_('index.php?option=com_easyblog&view=dashboard&layout=' . $layout_type, false)); } else { //from backend $mainframe->redirect(EBR::_('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)); EB::info()->set(JText::sprintf('ERROR.FOLDER_ALREADY_EXISTS', $relative_target_file), 'error'); $mainframe->redirect(EBR::_('index.php?option=com_easyblog&view=dashboard&layout=' . $layout_type, false)); } else { //from backend $mainframe->redirect(EBR::_('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 = EB::simpleimage(); $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; } }
/** * Creates a new variation on the site * * @since 5.0 * @access public * @param string * @return */ public function createVariation($uri, $name, $params) { $config = EB::config(); // Get the absolute path of the image file $filePath = EBMM::getPath($uri); // Get the file name of the image file $fileName = basename($uri); // Get the absolute path to the file's container $folderPath = dirname($filePath); // Get the uri of the folder $folderUri = dirname($uri); // Build target name, filename, path, uri, quality. $i = 0; do { // Determines if we should add a postfix count if the variation name already exists before $targetName = $name . (empty($i) ? '' : $i); // Generate the file name for this new variation $targetFileName = EBLOG_USER_VARIATION_PREFIX . '_' . $targetName . '_' . $fileName; $targetPath = $folderPath . '/' . $targetFileName; $i++; } while (JFile::exists($targetPath)); // Store the new target uri $targetUri = $folderUri . '/' . $targetFileName; // Determines the resize quality $quality = isset($params->quality) ? $params->quality : $config->get('main_image_quality'); // TODO: Reject if width/height exceeds // maxVariationWidth: $system->config->get( 'main_media_manager_image_panel_max_variation_image_width' ); // maxVariationHeight: $system->config->get( 'main_media_manager_image_panel_max_variation_image_height' ); // Resize image $image = EB::simpleimage(); $image->load($filePath); // Resize the image $image->resize($params->width, $params->height); $state = $image->save($targetPath, $image->type, $quality); // If it hits an error we should return the exception instead. if (!$state) { return EB::exception('COM_EASYBLOG_FAILED_TO_CREATE_VARIATION_PERMISSIONS'); } $item = $this->getItem($uri); return $item; }
/** * Uploads a user avatar * * @since 4.0 * @access public * @param string * @return */ public function upload($fileData, $userId = false) { jimport('joomla.filesystem.file'); jimport('joomla.filesystem.folder'); // Check if the user is allowed to upload avatar $acl = EB::acl(); // Ensure that the user really has access to upload avatar if (!$acl->get('upload_avatar')) { $this->setError('COM_EASYBLOG_NO_PERMISSION_TO_UPLOAD_AVATAR'); return false; } // Get the current user $user = JFactory::getUser(); // If there is userId passed, means this is from backend. // We cannot get the current logged in user because it will always be the admin. if ($userId) { $user = JFactory::getUser($userId); } $app = JFactory::getApplication(); $config = EB::config(); $path = $config->get('main_avatarpath'); $path = rtrim($path, '/'); $relativePath = $path; $absolutePath = JPATH_ROOT . '/' . $path; // If the absolute path does not exist, create it first if (!JFolder::exists($absolutePath)) { JFolder::create($absolutePath); // Copy the index.html file over to this new folder JFile::copy(JPATH_ROOT . '/components/com_easyblog/index.html', $absolutePath . '/index.html'); } // The file data should have a name if (!isset($fileData['name'])) { return false; } // Generate a better name for the file $fileData['name'] = $user->id . '_' . JFile::makeSafe($fileData['name']); // Get the relative path $relativeFile = $relativePath . '/' . $fileData['name']; // Get the absolute file path $absoluteFile = $absolutePath . '/' . $fileData['name']; // Test if the file is upload-able $message = ''; if (!EB::image()->canUpload($fileData, $message)) { $this->setError($message); return false; } // Determines if the web server is generating errors if ($fileData['error'] != 0) { $this->setError($fileData['error']); return false; } // We need to delete the old avatar $profile = EB::user($user->id); // Get the old avatar $oldAvatar = $profile->avatar; $isNew = false; // Delete the old avatar first if ($oldAvatar != 'default.png' && $oldAvatar != 'default_blogger.png') { $session = JFactory::getSession(); $sessionId = $session->getToken(); $oldAvatarPath = $absolutePath . '/' . $oldAvatar; if (JFile::exists($oldAvatarPath)) { JFile::delete($oldAvatarPath); } } else { $isNew = true; } $width = EBLOG_AVATAR_LARGE_WIDTH; $height = EBLOG_AVATAR_LARGE_HEIGHT; $image = EB::simpleimage(); $image->load($fileData['tmp_name']); $image->resizeToFill($width, $height); $image->save($absoluteFile, $image->type); if ($isNew && $config->get('main_jomsocial_userpoint')) { EB::jomsocial()->assignPoints('com_easyblog.avatar.upload', $user->id); } return $fileData['name']; }
public function createVariation($uri, $name, $params) { $config = EB::config(); // Filepath, filename, folderpath, folderuri $filepath = EasyBlogMediaManager::getPath($uri); $filename = basename($uri); $folderpath = dirname($filepath); $folderuri = dirname($uri); // Build target name, filename, path, uri, quality. $i = 0; do { $target_name = $name . (empty($i) ? '' : $i); $target_filename = EBLOG_USER_VARIATION_PREFIX . '_' . $target_name . '-' . $this->serializeParam($params) . '_' . $filename; $target_path = $folderpath . '/' . $target_filename; $i++; } while (JFile::exists($target_path)); $target_uri = $foldeuri . '/' . $target_filename; $target_quality = isset($params->quality) ? $params->quality : $config->get('main_image_quality'); // TODO: Reject if width/height exceeds // maxVariationWidth: $system->config->get( 'main_media_manager_image_panel_max_variation_image_width' ); // maxVariationHeight: $system->config->get( 'main_media_manager_image_panel_max_variation_image_height' ); // Resize image $image = EB::simpleimage(); $image->load($filepath); $image->resize($params->width, $params->height, $params->x, $params->y); $state = $image->save($target_path, $image->image_type, $target_quality); if (!$state) { // TODO: Language return EB::exception('COM_EASYBLOG_FAILED_TO_CREATE_VARIATION_PERMISSIONS'); } // Create variation object $variation = new stdClass(); $variation->name = $target_name; $variation->type = 'user'; $variation->url = $this->getUrl($target_uri); $variation->width = $params->width; $variation->height = $params->height; return $variation; }
/** * Uploads a team avatar * * @since 4.0 * @access public * @param string * @return */ public function uploadAvatar($file) { $config = EB::config(); $acl = EB::acl(); // Default avatar file name $default = 'default_team.png'; // Construct the storage path of the avatar $path = rtrim($config->get('main_teamavatarpath'), '/'); // Set the relative path $relativePath = $path; // Set the absolute path $absolutePath = JPATH_ROOT . '/' . $path; // Check if the folder exists EB::makeFolder($absolutePath); // Generate a proper file name for the file $fileName = md5($file['name'] . JFactory::getDate()->toSql()); $fileName .= EB::image()->getExtension($file['tmp_name']); // Reassign the image name $file['name'] = $fileName; if (!isset($file['name'])) { return $default; } // Check if the file upload itself contains errors. if ($file['error'] != 0) { $this->setError($file['error']); return false; } // Construct the relative and absolute file paths $relativeFile = $relativePath . '/' . $file['name']; $absoluteFile = $absolutePath . '/' . $file['name']; // Determine if the user can really upload this file $error = ''; $state = EB::image()->canUpload($file, $error); // If user is not allowed to upload image, return proper error if (!$state) { $this->setError($err); return false; } // Get the old avatar first $oldAvatar = $this->avatar; $width = EBLOG_AVATAR_LARGE_WIDTH; $height = EBLOG_AVATAR_LARGE_HEIGHT; // Load up the simple image library $image = EB::simpleimage(); $image->load($file['tmp_name']); // Resize the avatar to our specified width / height $image->resizeToFill($width, $height); // Store the file now $image->save($absoluteFile, $image->image_type); $this->avatar = $file['name']; // Save the team again $state = $this->store(); // If the team has avatar already, remove it if ($oldAvatar && $oldAvatar != $default) { $existingAbsolutePath = $absolutePath . '/' . $oldAvatar; // Test if the file exists before deleting it $exists = JFile::exists($existingAbsolutePath); if ($exists) { JFile::delete($existingAbsolutePath); } } return $state; }