/** * Method to add watermark on existing image. * * @param string $backgroundImagePath The path to the image that needs to be added with watermark. * @param string $destinationPath The path to the image output * @param string $destinationType The type of the output file * @param string $watermarkImagePath The path to the watermark image. * @param int $positionX The x position of where the watermark should be positioned. * @param int $positionY The y position of where the watermark should be positioned. * * @return bool True on sucess. **/ public static function addWatermark($backgroundImagePath, $destinationPath, $destinationType, $watermarkImagePath, $positionX = 0, $positionY = 0, $deleteBackgroundImage = true) { // Set output quality $imgQuality = 99; $pngQuality = ($imgQuality - 100) / 11.111111; $pngQuality = round(abs($pngQuality)); $watermarkInfo = getimagesize($watermarkImagePath); $background = JXImage::open($backgroundImagePath, $destinationType); $watermark = JXImage::open($watermarkImagePath, $watermarkInfo['mime']); list($backgroundWidth, $backgroundHeight) = getimagesize($backgroundImagePath); // Try to make the watermark image transparent imagecolortransparent($watermark, imagecolorat($watermark, 0, 0)); // Get overlay image width and hight $watermarkWidth = imagesx($watermark); $watermarkHeight = imagesy($watermark); // Combine background image and watermark into a single output image imagecopymerge($background, $watermark, $positionX, $positionY, 0, 0, $watermarkWidth, $watermarkHeight, 100); // Output ob_start(); // Test if type is png if ($destinationType == 'image/png' || $destinationType == 'image/x-png') { imagepng($background, null, $pngQuality); } elseif ($destinationType == 'image/gif') { imagegif($background); } else { imagejpeg($background, null, $imgQuality); } $output = ob_get_contents(); ob_end_clean(); // Delete old image if (JFile::exists($backgroundImagePath) && $deleteBackgroundImage) { JFile::delete($backgroundImagePath); } // Free any memory from the existing image resources imagedestroy($background); imagedestroy($watermark); return JFile::write($destinationPath, $output); }
public function manageTheme() { if ($_POST) { $configHelper = new JXConfig(); $file = JRequest::getVar('c_logo', '', 'FILES', 'array'); $mainframe = JFactory::getApplication(); if (!empty($file["tmp_name"])) { if (!JXImage::isValidType($file['type'])) { $mainframe = JFactory::getApplication(); $mainframe->redirect(JRoute::_('index.php?option=com_account&view=account&task=manageTheme', false), JText::_('COM_PROFILE_IMAGE_FILE_NOT_SUPPORTED'), 'error'); } //start image processing $logoMaxWidth = 200; $logoMaxHeight = 56; // Get a hash for the file name. $fileName = JUtility::getHash($file['tmp_name'] . time()); $hashFileName = JString::substr($fileName, 0, 24); //avatar store path $storage = JPATH_ROOT . DS . 'images'; $storageImage = $storage . DS . $hashFileName . JXImage::getExtension($file['type']); $image = 'images' . '/' . $hashFileName . JXImage::getExtension($file['type']); // Generate full image list($currentWidth, $currentHeight) = getimagesize($file['tmp_name']); // Calculate ratio based on height first as height got lower value $ratioToUse = intval($currentHeight / $logoMaxHeight); // If logoMaxwidth * ratio > original width, recalculate ratio based on width if ($logoMaxWidth * $ratioToUse > $currentWidth) { $ratioToUse = intval($currentWidth / $logoMaxWidth); } $maxWidth = $ratioToUse * $logoMaxWidth; $maxHeight = $logoMaxHeight * $ratioToUse; $sourceX = intval(($currentWidth - $maxWidth) / 2); $sourceY = intval(($currentHeight - $maxHeight) / 2); if (!JXImage::crop($file['tmp_name'], $storageImage, $maxWidth, $maxHeight, $sourceX, $sourceY, $logoMaxWidth, $logoMaxHeight)) { $mainframe->redirect(JRoute::_('index.php?option=com_account&view=account&task=manageTheme', false), JText::sprintf('COM_PROFILE_ERROR_MOVING_UPLOADED_FILE', $storageImage), 'error'); } else { // Remove previous logo $originalFilePath = $configHelper->getCompanyLogoPath(); $defaultLogo = basename(JXConfig::DEFAULT_LOGO); // Do not remove default logo if (JFile::exists($originalFilePath) && !stristr($originalFilePath, $defaultLogo)) { JFile::delete($originalFilePath); } $param['logo'] = $image; } } $postdata = JRequest::getVar('params'); $param[JXConfig::STYLE] = $postdata['style']; if (!$configHelper->saveConfig($param)) { /* Redirect to clear the previous post values */ $mainframe->redirect(JRoute::_('index.php?option=com_account&view=account&task=manageTheme', false), JText::_('COM_ACCOUNT_ACTION_SAVE_SETTING_FAIL'), 'error'); } /* Redirect to clear the previous post values */ $mainframe->redirect(JRoute::_('index.php?option=com_account&view=account&task=manageTheme', false), JText::_('COM_ACCOUNT_ACTION_SAVE_SETTING_SUCCESS')); } JRequest::setVar('view', 'theme'); parent::display(); }
public function ajaxSaveThumbnail() { $my = JXFactory::getUser(); $filePath = JPATH_ROOT . DS . $my->getAvatarPath(); $fileType = JXImage::getImageType($filePath); $thumbnailStore = $my->getThumbAvatarPath(); $originalFile = JXImage::getImageFileName(JPATH_ROOT . DS . $thumbnailStore); $originalFilePath = JPATH_ROOT . DS . $thumbnailStore; $newFileName = 'thumb_' . JXImage::getHashName($originalFile) . JXImage::getExtension($fileType); $thumbnailStore = str_replace($originalFile, $newFileName, $thumbnailStore); $xPos = JRequest::getInt('x_pos', 0); $yPos = JRequest::getInt('y_pos', 0); $imgWidth = JRequest::getInt('width', 0); $imgHeight = JRequest::getInt('height', 0); if (!JXImage::crop($filePath, JPATH_ROOT . DS . $thumbnailStore, $imgWidth, $imgHeight, $xPos, $yPos)) { $this->_showUploadError(true, '0', $my->getThumbAvatarURL()); exit; } $this->_deleteUserCurrentAvatar($my, false); $my->setParam('avatar_thumb', $thumbnailStore); $my->save(); $this->_showUploadError(false, '1', $my->getThumbAvatarURL()); exit; }