<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Developer Oracles &#187; image watermarking</title>
	<atom:link href="http://devoracles.com/tag/image-watermarking/feed" rel="self" type="application/rss+xml" />
	<link>http://devoracles.com</link>
	<description></description>
	<lastBuildDate>Sun, 21 Mar 2010 11:05:37 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Watermarking images using Imagick</title>
		<link>http://devoracles.com/watermarking-images-using-imagick/13</link>
		<comments>http://devoracles.com/watermarking-images-using-imagick/13#comments</comments>
		<pubDate>Sat, 12 Jul 2008 14:21:43 +0000</pubDate>
		<dc:creator>Gary Illyes</dc:creator>
				<category><![CDATA[IMagick]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[image watermarking]]></category>
		<category><![CDATA[imagick]]></category>
		<category><![CDATA[imagick watermarking]]></category>
		<category><![CDATA[watermark]]></category>

		<guid isPermaLink="false">http://devoracles.com/?p=13</guid>
		<description><![CDATA[This is a perfect day to show all how easy is to use php&#8217;s imagick library to watermark an image.
What you need is to have imagick installed and working correctly, a font you will use to write your website&#8217;s name on the image and of course an image you will put the text on.
I will [...]]]></description>
			<content:encoded><![CDATA[<p>This is a perfect day to show all how easy is to use php&#8217;s imagick library to watermark an image.<br />
What you need is to have imagick installed and working correctly, a font you will use to write your website&#8217;s name on the image and of course an image you will put the text on.</p>
<p>I will explain each step but for those who don&#8217;t want to read, here&#8217;s the script:</p>
<pre name="code" class="php">
&lt;?php
$img	= 'path/to/your/image.jpg';
$text	= 'devoracles.com';
$font	= 'path/to/your/yourfont.ttf'
$font_size	= '20';
$watermark	= array();
$image = new Imagick($img);
$image->setImageFormat("jpg");
$draw = new ImagickDraw();
$draw->setGravity(Imagick::GRAVITY_CENTER);
$draw->setFont($font);
$draw->setFontSize($font_size);
$textColor = new ImagickPixel("black");
$draw->setFillColor($textColor);
$im = new imagick();
$properties = $im->queryFontMetrics($draw,$text);
$watermark['w'] = intval($properties["textWidth"] + 5);
$watermark['h'] = intval($properties["textHeight"] + 5);
$im->newImage($watermark['w'],$watermark['h'],new ImagickPixel("transparent"));
$im->setImageFormat("jpg");
$im->annotateImage($draw, 0, 0, 0, $text);
$watermark = $im->clone();
$watermark->setImageBackgroundColor($textColor);
$watermark->shadowImage(80, 2, 2, 2);
$watermark->compositeImage($im, Imagick::COMPOSITE_OVER, 0, 0);
$image->compositeImage($watermark, Imagick::COMPOSITE_OVER, 0, 0);
header("Content-Type: image/jpg");
echo $image;
?&gt;
</pre>
<p>And the result is:<br />
<div class="wp-caption alignleft" style="width: 410px"><img alt="Watermark with Imagick" src="http://devoracles.com/imagick/wm.php" title="Watermark with Imagick" width="400" height="254" /><p class="wp-caption-text">Watermark with Imagick</p></div><br />
That was all, now a bit of explanation for almost all the lines. The first steps, as you see was to set up some variables which will be used in the script, I think the variable names talk for themselves so I don&#8217;t have to explain them. Do I?<br />
Next, I instructed imagick to read the image which will be worked with and specified the type of the image, in the above case a &#8220;jpg&#8221;.<br />
<code><br />
$image = new Imagick($img);<br />
$image->setImageFormat("jpg");<br />
</code><br />
By the way, if you want to write a script you will pass through all your images, thus all the images are watermarked, first assure that the image you work with is from your server, read, no one can use your script to watermark images on another server. The second thing, you can get the image&#8217;s extension using <strong>mime_content_type(&#8216;path/to/your/image.jpg&#8217;)</strong>. Not the best method I admit, but it&#8217;s simple and effective.<br />
Let&#8217;s proceed further. I fired up a new class, ImagickDraw(), this will let Imagick know what font to use and also the text&#8217;s properties like font size, font color, etc. &#8220;Imagick::GRAVITY_CENTER&#8221; basically means to center the text on it&#8217;s, say, layer. You can play with it and try to set other parameters too, like &#8220;Imagick::GRAVITY_SOUTH&#8221; which means to place the text on the bottom of the layer, &#8220;Imagick::GRAVITY_WEST&#8221; to put it on the left side, you get the point.<br />
<code><br />
$draw = new ImagickDraw();<br />
$draw->setGravity(Imagick::GRAVITY_CENTER);<br />
$draw->setFont($font);<br />
$draw->setFontSize($f_size);<br />
$textColor = new ImagickPixel("black");<br />
$draw->setFillColor($textColor);<br />
</code></p>
<p>We get then the parameters of the text we want to put on the image and we add an additional 5 pixels to it, both horizontal and vertical axis. The 5 pixels will be needed later to have place for the drop-down shadow of the text. Then we simply create the canvas and with <strong>annotateImage()</strong> we put the text on the image.<br />
<code><br />
$im = new imagick();<br />
$properties = $im->queryFontMetrics($draw,$text);<br />
$watermark['w'] = intval($properties["textWidth"] + 5);<br />
$watermark['h'] = intval($properties["textHeight"] + 5);<br />
$im->newImage($watermark['w'],$watermark['h'],new ImagickPixel("transparent"));<br />
$im->setImageFormat("jpg");<br />
$im->annotateImage($draw, 0, 0, 0, $text);<br />
</code></p>
<p>OK, now let&#8217;s play a bit with the shadow. First we clone the $im object, which is basically the text we&#8217;ll put on the image.  The color of the image will be the same as the text&#8217;s. And now the interesting stuff: <strong>shadowImage()</strong>. This will create the drop-down shadow and the parameters tells Imagick how to create that shadow. The usage is:<br />
<code><br />
shadowImage  ( float $opacity  , float $sigma  , int $x  , int $y  )<br />
</code><br />
So the first parameter sets how opaque the shadow has to be, the last two sets where to drop that shadow. Don&#8217;t ask me the second what does, i have no idea <img src='http://devoracles.com/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' /><br />
OK, lastly let&#8217;s put our shadow on the image with <strong>compositeImage()</strong>.<br />
<code><br />
$watermark = $im->clone();<br />
$watermark->setImageBackgroundColor($textColor);<br />
$watermark->shadowImage(80, 2, 2, 2);<br />
$watermark->compositeImage($im, Imagick::COMPOSITE_OVER, 0, 0);<br />
</code><br />
The last steps are to put the watermark on the image, set the correct content-type header image/jpg in our case and echo the data. Why do we have to set the content-type header? Well, if I&#8217;m not mistaken, this is a php script and the browsers, if not overridden expects text/html, which in our case is not good at all.<br />
<code><br />
$image->compositeImage($watermark, Imagick::COMPOSITE_OVER, 0, 0);<br />
header("Content-Type: image/jpg");<br />
echo $image;<br />
</code></p>
<p>That&#8217;s all. It was a bit complicated, but I&#8217;d rate its difficulty as &#8216;medium&#8217; but it&#8217;s extremely useful in many cases. This is not JavaScript, so the watermark will be in place whatever the user does. If you are at least a bit familiar with object oriented PHP, I say give it a go and try it.</p>
<p>If something is not clear, the comments are open, just ask and will try to answer as soon as possible.</p>
]]></content:encoded>
			<wfw:commentRss>http://devoracles.com/watermarking-images-using-imagick/13/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
