Sunday, 21 December 2008

HOWTO: using Php to add watermark


makewatermark();

function makewatermark()
{
///////////////
// php imagemagick test harness
//
// create a watermarked image
///////////////

//xoffset of the watermark
//yoffset of the watermark

//the original image to be watermarked
$rawimage = NewMagickWand();
MagickReadImage( $rawimage, 'cyclops.gif' );
MagickWriteImage($rawimage, 'new_image.jpg' );
$originalimage='new_image.jpg';
//the watermark to apply
$watermarkimage = "sphinx.gif";

//top end opacity number (totally transparent)
$opacity0 = @MagickGetQuantumRange();

//bootom opacity (totally visible)
$opacity100 = 0;

//desired opacity percentage
// THIS IS THE ONE TO SET
$opacitypercent = 50;

//gather the actual opacity number
$opacity = $opacity0 - ($opacity0 * $opacitypercent/100 ) ;

//validate the opacity number
if ($opacity > $opacity0){
$opacity = $opacity0;
}elseif ($opacity <0){
$opacity = 0;
}

//initialize the wands
$sourceWand = NewMagickWand();
$compositeWand = NewMagickWand();

//read in the images
@MagickReadImage($compositeWand, $watermarkimage);
@MagickReadImage($sourceWand, $originalimage);

//setting the image index
MagickSetImageIndex($compositeWand, 0);
MagickSetImageType($compositeWand, MW_TrueColorMatteType);

//seting the opacity level
MagickEvaluateImage($compositeWand, MW_SubtractEvaluateOperator, $opacity, MW_OpacityChannel) ;

//combining the images
@MagickCompositeImage($sourceWand, $compositeWand, MW_ScreenCompositeOp, 0, 0);

//print out the image
MagickWriteImage( $sourceWand, 'new_image.jpg' );
//header("Content-Type: image/jpeg");
//MagickEchoImageBlob($sourceWand);
}
?>

1 comment:

UK Cross said...

In fedora, it's pretty easy, just install the magickwand from yum.
while in ubuntu, so far, it's killing:
1)No any packages about magickwand
2)Source of magickwand 1.0.7 requires imagemagick 6.3.8, but in the apt, only 6.3.5 available. (did not try testing repository and did not find its ppa on launchpad); while source of 1.0.6 keep compaining not found some head files, but I did see them in the source folder.


Anyway, in one word, as a development platform, fedora(f10) is better than ubuntu(8.10).

My photo
London, United Kingdom
twitter.com/zhengxin

Facebook & Twitter