<?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>Marco Pracucci &#187; watermark</title>
	<atom:link href="http://blog.pracucci.com/tag/watermark/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.pracucci.com</link>
	<description>Marco Pracucci - Technology news and blog</description>
	<lastBuildDate>Thu, 25 Feb 2010 12:39:35 +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>Watermarks with PHP and IMagick</title>
		<link>http://blog.pracucci.com/2008/08/30/watermarks-with-php-and-imagick/</link>
		<comments>http://blog.pracucci.com/2008/08/30/watermarks-with-php-and-imagick/#comments</comments>
		<pubDate>Sat, 30 Aug 2008 16:52:10 +0000</pubDate>
		<dc:creator>Marco Pracucci</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[imagemagick]]></category>
		<category><![CDATA[imagick]]></category>
		<category><![CDATA[watermark]]></category>

		<guid isPermaLink="false">http://blog.pracucci.com/2008/08/30/automatic-selection-of-the-best-image-corner-where-apply-a-watermark-with-php-and-imagick/</guid>
		<description><![CDATA[There are a lot of web sites, online communities and social networks that apply a watermark over photos uploaded from users. This watermark, usually a logo or a text with the service name, should be applied in a region of the image where there are no important things, because no one likes to see a [...]]]></description>
			<content:encoded><![CDATA[<p>There are a lot of web sites, online communities and social networks that apply a <strong>watermark over photos uploaded from users</strong>. This watermark, usually a logo or a text with the service name, should be applied in a region of the image where there are no important things, because no one likes to see a text over his face.</p>
<p>This simple feature requires an algorithm to automatically select the best corner where apply the watermark.</p>
<h3>The algorithm</h3>
<p>The watermark should be placed in the corner with the lowest number of graphic details, in order to be as less invasive as possible. To select the best corner, the proposed algorithm <strong>counts the number of different colours inside each corner</strong> and applies the watermark to the <strong>corner with the lowest number of different colours</strong>.</p>
<p>This algorithm is very simple but effective, and can be described with the following steps:</p>
<ol>
<li>For each candidate image region (corners) calculate the number of different colours</li>
<li>Select the image region with the lowest number of different colours</li>
<li>Apply the watermark to the selected region</li>
</ol>
<h3>The code</h3>
<p>Here is the code the <strong>PHP</strong> code of a function that selects the best corner of an image and then apply the watermark.</p>
<pre class="prettyprint">
/**
 * Draw a watermark over an image (the watermark position is
 * selected automatically) and returns true. If the watermark
 * is bigger than the image, this method returns false.
 *
 * @param IMagick $image
 * @param IMagick $watermark
 * @param int $padding
 * @return bool
 */
private function drawWatermark($image, $watermark, $padding = 0)
{
	// Check if the watermark is bigger than the image
	$image_width 		= $image-&gt;getImageWidth();
	$image_height 		= $image-&gt;getImageHeight();
	$watermark_width 	= $watermark-&gt;getImageWidth();
	$watermark_height 	= $watermark-&gt;getImageHeight();

	if ($image_width &lt; $watermark_width + $padding || $image_height &lt; $watermark_height + $padding) {
		return false;
	}

	// Calculate each position
	$positions = array();
	$positions[] = array(0 + $padding, 0 + $padding);
	$positions[] = array($image_width - $watermark_width - $padding, 0 + $padding);
	$positions[] = array($image_width - $watermark_width - $padding, $image_height - $watermark_height - $padding);
	$positions[] = array(0 + $padding, $image_height - $watermark_height - $padding);

	// Initialization
	$min 		= null;
	$min_colors = 0;

	// Calculate the number of colors inside each region
	// and retrieve the minimum
	foreach($positions as $position)
	{
		$colors = $image-&gt;getImageRegion(
			$watermark_width,
			$watermark_height,
			$position[0],
			$position[1])-&gt;getImageColors();

		if ($min === null || $colors &lt;= $min_colors)
		{
			$min 		= $position;
			$min_colors = $colors;
		}
	}

	// Draw the watermark
	$image-&gt;compositeImage(
		$watermark,
		Imagick::COMPOSITE_OVER,
		$min[0],
		$min[1]);

	return true;
}</pre>
<p><span id="more-104"></span></p>
<h3>Testing the algorithm</h3>
<p>I have taken three photos from <a href="http://www.tonight.eu" title="Tonight - European nightlife social network" target="_blank">www.tonight.eu</a> and tried to apply the watermark, using the proposed algorithm. For each photo there are two images:</p>
<ol>
<li>the first one shows the number of different colours in each candidate region</li>
<li>the second one shows the photo after the watermark has been applied.</li>
</ol>
<p>The results are quite good.</p>
<p><a href="http://milano.tonight.eu/locale/old_fashion_cafe" title="Milano tonight - Old fashion cafe" target="_blank"><img src="http://blog.pracucci.com/wp-content/uploads/2008/08/1a.jpg" alt="1a.jpg" /></a></p>
<p><a href="http://milano.tonight.eu/locale/old_fashion_cafe" title="Milano tonight - Old fashion cafe" target="_blank"><img src="http://blog.pracucci.com/wp-content/uploads/2008/08/1b.jpg" alt="1b.jpg" /></a></p>
<p><a href="http://milano.tonight.eu/locale/nottingham_forest" title="Milano tonight - Nottingham forest" target="_blank"><img src="http://blog.pracucci.com/wp-content/uploads/2008/08/2a.jpg" alt="2a.jpg" /></a></p>
<p><a href="http://milano.tonight.eu/locale/nottingham_forest" title="Milano tonight - Nottingham forest" target="_blank"><img src="http://blog.pracucci.com/wp-content/uploads/2008/08/2b.jpg" alt="2b.jpg" /></a></p>
<p><a href="http://milano.tonight.eu/locale/plastic" title="Milano tonight - Plastic" target="_blank"><img src="http://blog.pracucci.com/wp-content/uploads/2008/08/3a.jpg" alt="3a.jpg" /></a></p>
<p><a href="http://milano.tonight.eu/locale/plastic" title="Milano tonight - Plastic" target="_blank"><img src="http://blog.pracucci.com/wp-content/uploads/2008/08/3b.jpg" alt="3b.jpg" /></a></p>
<h3>Feedback</h3>
<p>Please, give me a feedback and leave a comment to this post.</p>
<h3>References</h3>
<ul>
<li><a href="http://www.php.net/manual/en/book.imagick.php" target="_blank" title="IMagick">IMagick </a></li>
<li><a href="http://www.tonight.eu" title="Tonight - European nightlife social network" target="_blank">Tonight.eu</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://blog.pracucci.com/2008/08/30/watermarks-with-php-and-imagick/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
