/** * 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 $config = CFactory::getConfig(); $imgQuality = $config->get('output_image_quality'); $pngQuality = ($imgQuality - 100) / 11.111111; $pngQuality = round(abs($pngQuality)); $watermarkInfo = getimagesize($watermarkImagePath); $background = CImageHelper::open($backgroundImagePath, $destinationType); $watermark = CImageHelper::open($watermarkImagePath, $watermarkInfo['mime']); list($backgroundWidth, $backgroundHeight) = getimagesize($backgroundImagePath); // Get overlay image width and hight $watermarkWidth = imagesx($watermark); $watermarkHeight = imagesy($watermark); // Combine background image and watermark into a single output image imagecopy($background, $watermark, $positionX, $positionY, 0, 0, $watermarkWidth, $watermarkHeight); // 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); }
/** * 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) { $watermarkInfo = getimagesize($watermarkImagePath); $background = CImageHelper::open($backgroundImagePath, $destinationType); $watermark = CImageHelper::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); } elseif ($destinationType == 'image/gif') { imagegif($background); } else { imagejpeg($background, null, 80); } $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 static function resize($srcPath, $destPath, $destType, $destWidth, $destHeight, $sourceX = 0, $sourceY = 0, $currentWidth=0, $currentHeight=0) { require_once(JPATH_SITE.DS.'components'.DS.'com_community'.DS.'libraries'.DS.'core.php'); require_once(JPATH_SITE.DS.'components'.DS.'com_community'.DS.'helpers'.DS.'image.php'); // See if we can grab image transparency $image = CImageHelper::open( $srcPath , $destType ); $transparentIndex = imagecolortransparent( $image ); // Create new image resource $image_p = ImageCreateTrueColor( $destWidth , $destHeight ); $background = ImageColorAllocate( $image_p , 255, 255, 255 ); // test if memory is enough if($image_p == FALSE){ echo 'Image resize fail. Please increase PHP memory'; return false; } // Set the new image background width and height $resourceWidth = $destWidth; $resourceHeight = $destHeight; if(empty($currentHeight) && empty($currentWidth)) { list($currentWidth , $currentHeight) = getimagesize( $srcPath ); } // If image is smaller, just copy to the center $targetX = 0; $targetY = 0; // If the height and width is smaller, copy it to the center. if( $destType != 'image/jpg' && $destType != 'image/jpeg' && $destType != 'image/pjpeg' ) { if( ($currentHeight < $destHeight) && ($currentWidth < $destWidth) ) { $targetX = intval( ($destWidth - $currentWidth) / 2); $targetY = intval( ($destHeight - $currentHeight) / 2); // Since the $destWidth = $currentWidth; $destHeight = $currentHeight; } } // Resize GIF/PNG to handle transparency if( $destType == 'image/gif' ) { $colorTransparent = imagecolortransparent($image); imagepalettecopy($image, $image_p); imagefill($image_p, 0, 0, $colorTransparent); imagecolortransparent($image_p, $colorTransparent); imagetruecolortopalette($image_p, true, 256); imagecopyresized($image_p, $image, $targetX, $targetY, $sourceX, $sourceY, $destWidth , $destHeight , $currentWidth , $currentHeight ); } else if( $destType == 'image/png' || $destType == 'image/x-png') { // Disable alpha blending to keep the alpha channel imagealphablending( $image_p , false); imagesavealpha($image_p,true); $transparent = imagecolorallocatealpha($image_p, 255, 255, 255, 127); imagefilledrectangle($image_p, 0, 0, $resourceWidth, $resourceHeight, $transparent); imagecopyresampled($image_p , $image, $targetX, $targetY, $sourceX, $sourceY, $destWidth, $destHeight, $currentWidth, $currentHeight); } else { // Turn off alpha blending to keep the alpha channel imagealphablending( $image_p , false ); imagecopyresampled( $image_p , $image, $targetX, $targetY, $sourceX, $sourceY, $destWidth , $destHeight , $currentWidth , $currentHeight ); } // Output ob_start(); // Test if type is png if( $destType == 'image/png' || $destType == 'image/x-png' ) { imagepng( $image_p ); } elseif ( $destType == 'image/gif') { imagegif( $image_p ); } else { // We default to use jpeg imagejpeg($image_p, null, 80); } $output = ob_get_contents(); ob_end_clean(); // @todo, need to verify that the $output is indeed a proper image data return JFile::write( $destPath , $output ); }