<?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"
	>

<channel>
	<title>Gear Adrift &#187; PHP</title>
	<atom:link href="http://www.gearadrift.com/category/coding/php/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.gearadrift.com</link>
	<description>Navy Humor, Navy Jokes, and Satire about Life in the Navy</description>
	<pubDate>Fri, 31 Oct 2008 12:08:46 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.6</generator>
	<language>en</language>
			<item>
		<title>Watermarking Images with PHP</title>
		<link>http://www.gearadrift.com/coding/watermarking-images-with-php/</link>
		<comments>http://www.gearadrift.com/coding/watermarking-images-with-php/#comments</comments>
		<pubDate>Wed, 11 Jul 2007 00:38:23 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
		
		<category><![CDATA[Coding]]></category>

		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://gearadrift.com/blog/2007/07/10/watermarking-images-with-php/</guid>
		<description><![CDATA[If you happen to have a website that displays original works of art or photographs.  Or, if you happen to want a quick way to copyright all of the images you are showing on your web page, then you can do this using php.

First, I create my copyright image:

As a point of fact, I&#8217;ve [...]]]></description>
			<content:encoded><![CDATA[<p>If you happen to have a website that displays original works of art or photographs.  Or, if you happen to want a quick way to copyright all of the images you are showing on your web page, then you can do this using php.<br />
<span id="more-31"></span><br />
First, I create my copyright image:</p>
<p><img src="http://www.gearadrift.com/wp-content/uploads/2007/07/copyright.png" alt="copyright.png" /></p>
<p>As a point of fact, I&#8217;ve saved this image as <strong>copyright.png</strong> using an 8 color palette.  I chose PNG8 because there are only 8 colors in the export.  This smaller pallete (as opposed to a JPEG with millions of colors) greatly reduces the chance of a <a href="http://en.wikipedia.org/wiki/Dithering">dithering effect</a> and hereby making the white of the image easier to eliminate, as you will see later.</p>
<p>Next, I take my original photograph.  In this case, me in Singapore.  I&#8217;ve named this file sing.jpg:</p>
<p><img src="http://www.gearadrift.com/wp-content/uploads/2007/07/sing.jpg" alt="sing.jpg" /></p>
<p>Now, I use php to call the images into a script:<br />
<code></p>
<p>Note the different imagecreate() statements.  Imagecreatefromjpeg calls a JPEG and imagecreatefrompng calls a PNG.</p>
<p>Now I want to copy my watermark (copyright.png) to my original photo (sing.png).  I do this by building onto the original code to get:<br />
<code></p>
<p></code></p>
<p>The imagecopy() statement is broken down as follows.  The <strong>first variable is the image you want watermarked</strong> and the <strong>second variable is the image of the watermark</strong>.  <strong>The third and fourth variables are the location in the destination image ($Image) where you want the image date to be copied.</strong>  They mark the upper left corner of the block, in this case (0, 0) marks the upper left-hand corner of the destination image (sing.jpg).</p>
<p>  <strong>The fifth and sixth variables are the position of the source image ($Copyright) to start copying from.</strong>  And the <strong>seventh and eighth variables mark the width and height of the image to be copied.</strong>  In this case, I want to copy the entire image and the dimensions of copyright.png are 400px wide by 100px high.</p>
<p>We use the following code to output the image</p>
<p><code></p>
<p></code><br />
And the output looks like this:<br />
<img src='http://www.gearadrift.com/wp-content/uploads/2007/07/first.jpg' alt='first.jpg' /></p>
<p>Now, this is hardly a watermark, so we have to make a few changes but at least we managed to copy the watermark image onto the original.</p>
<p>In order to make this look more like a watermark, we need to get rid of the white and adjust the opacity</p>
<p>To get rid of the white, after the line:</p>
<p><code><br />
$Copyright = imagecreatefrompng('copyright.png');<br />
</code></p>
<p>Add the following:</p>
<p><code><br />
$white = imagecolorexact($Copyright, 255, 255, 255);<br />
imagecolortransparent($Copyright, $white);<br />
</code></p>
<p>The imagecolorexact() function returns the color white.  The first parameter is the image with the color you want to identify and the second, third, and fourth parameters are the RGB values of the color you want.  The imagecolortransparent() function removes whatever value is specified in the second parameter from the image in the first parameter.</p>
<p>And we get this output:</p>
<p><img src='http://www.gearadrift.com/wp-content/uploads/2007/07/second.jpg' alt='second.jpg' /></p>
<p>Now this may be fine for most but I want to make it a little less obvious and more like a watermark.  This is accomplished by changing</p>
<p><code><br />
imagecopy($Image, $Copyright,0,0,0,0,400,100);<br />
</code></p>
<p>to read</p>
<p><code><br />
imagecopymerge($Image, $Copyright,0,0,0,0,400,100, 50);<br />
</code></p>
<p>Notice the change form the imagecopy() function to the imagecopymerge() function.  This new function allows for a ninth variable at the end of the parameters, in this case 50, that is opacity.</p>
<p>This change brings the final code to:</p>
<p><code></p>
<p></code></p>
<p>And the final image to:</p>
<p><img src='http://www.gearadrift.com/wp-content/uploads/2007/07/third.jpg' alt='third.jpg' /></p>
]]></content:encoded>
			<wfw:commentRss>http://www.gearadrift.com/coding/watermarking-images-with-php/feed/</wfw:commentRss>
		</item>
	</channel>
</rss>
