<?xml version="1.0" encoding="UTF-8"?><!-- generator="bbPress" -->

<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
>

<channel>
<title>PHP Thumb Topic: watermark plugin</title>
<link>http://phpthumb.gxdlabs.com/forums/</link>
<description>Help, Suggestions, and General Discussion</description>
<language>en</language>
<pubDate>Thu, 09 Sep 2010 04:52:26 +0000</pubDate>

<item>
<title>RB on "watermark plugin"</title>
<link>http://phpthumb.gxdlabs.com/forums/topic/watermark-plugin#post-115</link>
<pubDate>Thu, 01 Oct 2009 20:47:16 +0000</pubDate>
<dc:creator>RB</dc:creator>
<guid isPermaLink="false">115@http://phpthumb.gxdlabs.com/forums/</guid>
<description>&#60;p&#62;&#60;strong&#62;File name&#60;/strong&#62; gd_watermark.inc.php&#60;/p&#62;
&#60;p&#62;&#60;code&#62;&#38;lt;?php&#60;/p&#62;
&#60;p&#62;class GdWatermarkLib&#60;br /&#62;
{&#60;br /&#62;
	/**&#60;br /&#62;
	* Instance of GdThumb passed to this class&#60;br /&#62;
	*&#60;br /&#62;
	* @var GdThumb&#60;br /&#62;
	*/&#60;br /&#62;
		protected $parentInstance;&#60;br /&#62;
		protected $currentDimensions;&#60;br /&#62;
		protected $workingImage;&#60;br /&#62;
		protected $newImage;&#60;br /&#62;
		protected $options;&#60;/p&#62;
&#60;p&#62;		public function createWatermark ($mask_file, $mask_position='cc', $mask_padding=0, &#38;#38;$that)&#60;br /&#62;
		{&#60;br /&#62;
			// bring stuff from the parent class into this class...&#60;br /&#62;
			$this-&#38;gt;parentInstance           = $that;&#60;br /&#62;
			$this-&#38;gt;currentDimensions        = $this-&#38;gt;parentInstance-&#38;gt;getCurrentDimensions();&#60;br /&#62;
			$this-&#38;gt;workingImage             = $this-&#38;gt;parentInstance-&#38;gt;getWorkingImage();&#60;br /&#62;
			$this-&#38;gt;newImage                 = $this-&#38;gt;parentInstance-&#38;gt;getOldImage();&#60;br /&#62;
			$this-&#38;gt;options                  = $this-&#38;gt;parentInstance-&#38;gt;getOptions();&#60;/p&#62;
&#60;p&#62;			$this-&#38;gt;mask_file                = $mask_file;&#60;br /&#62;
			$this-&#38;gt;mask_position            = $mask_position;&#60;br /&#62;
			$this-&#38;gt;mask_padding             = $mask_padding;&#60;/p&#62;
&#60;p&#62;			$canvas_width				    = $this-&#38;gt;currentDimensions['width'];&#60;br /&#62;
			$canvas_height				    = $this-&#38;gt;currentDimensions['height'];&#60;/p&#62;
&#60;p&#62;			if ($canvas_width &#38;lt;= 200 &#124;&#124; $canvas_height &#38;lt;= 200) {&#60;br /&#62;
				return $that;&#60;br /&#62;
			}&#60;/p&#62;
&#60;p&#62;			list($stamp_width, $stamp_height, $stamp_type, $stamp_attr) = getimagesize($mask_file);&#60;/p&#62;
&#60;p&#62;			switch ($stamp_type) {&#60;br /&#62;
				case 1:&#60;br /&#62;
					$stamp_image = imagecreatefromgif($mask_file);&#60;br /&#62;
					break;&#60;br /&#62;
				case 2:&#60;br /&#62;
					@ini_set('gd.jpeg_ignore_warning', 1);&#60;br /&#62;
					$stamp_image = imagecreatefromjpeg($mask_file);&#60;br /&#62;
					break;&#60;br /&#62;
				case 3:&#60;br /&#62;
					$stamp_image = imagecreatefrompng($mask_file);&#60;br /&#62;
					break;&#60;br /&#62;
			}&#60;/p&#62;
&#60;p&#62;			imagealphablending($this-&#38;gt;workingImage, true);&#60;/p&#62;
&#60;p&#62;			if ($stamp_width &#38;gt; $canvas_width &#124;&#124; $stamp_height &#38;gt; $canvas_height) {&#60;br /&#62;
				// some simple resize math&#60;br /&#62;
				//$water_resize_factor  = round($canvas_width / $stamp_width);&#60;br /&#62;
				$water_resize_factor  = 0.5;&#60;br /&#62;
				$new_mask_width  = $stamp_width  * $water_resize_factor;&#60;br /&#62;
				$new_mask_height = $stamp_height * $water_resize_factor;&#60;br /&#62;
				$mask_padding    = $mask_padding * $water_resize_factor;&#60;br /&#62;
				// the new watermark creation takes place starting from here&#60;br /&#62;
				$new_mask_image = imagecreatetruecolor($new_mask_width , $new_mask_height);&#60;br /&#62;
				// imagealphablending is important in order to keep, our png image (the watewrmark) transparent&#60;br /&#62;
				imagealphablending($new_mask_image , false);&#60;br /&#62;
				imagecopyresampled(&#60;br /&#62;
					$new_mask_image , $stamp_image, 0, 0, 0, 0,&#60;br /&#62;
					$new_mask_width,  $new_mask_height,&#60;br /&#62;
					$stamp_width,     $stamp_height&#60;br /&#62;
				);&#60;br /&#62;
				// assign the new values to the old variables&#60;br /&#62;
				$stamp_width  = $new_mask_width;&#60;br /&#62;
				$stamp_height = $new_mask_height;&#60;br /&#62;
				$stamp_image   = $new_mask_image;&#60;br /&#62;
			}&#60;/p&#62;
&#60;p&#62;			switch($mask_position) {&#60;br /&#62;
				case 'cc':&#60;br /&#62;
					// Center&#60;br /&#62;
					$start_width 	= 	round(($canvas_width - $stamp_width) / 2);&#60;br /&#62;
					$start_height 	= 	round(($canvas_height - $stamp_height) / 2);&#60;br /&#62;
					break;&#60;br /&#62;
				case 'lt':&#60;br /&#62;
					// Left Top&#60;br /&#62;
					$start_width 	= 	$mask_padding;&#60;br /&#62;
					$start_height 	= 	$mask_padding;&#60;br /&#62;
					break;&#60;br /&#62;
				case 'rt':&#60;br /&#62;
					// Right Top&#60;br /&#62;
					$start_width 	= 	$canvas_width - $mask_padding - $stamp_width;&#60;br /&#62;
					$start_height 	= 	$mask_padding;&#60;br /&#62;
					break;&#60;br /&#62;
				case 'lb':&#60;br /&#62;
					// Left Bottom&#60;br /&#62;
					$start_width 	= 	$mask_padding;&#60;br /&#62;
					$start_height 	= 	$canvas_height - $mask_padding - $stamp_height;&#60;br /&#62;
					break;&#60;br /&#62;
				case 'rb':&#60;br /&#62;
					// Right Bottom&#60;br /&#62;
					$start_width 	= 	$canvas_width - $mask_padding - $stamp_width;&#60;br /&#62;
					$start_height 	= 	$canvas_height - $mask_padding - $stamp_height;&#60;br /&#62;
					break;&#60;br /&#62;
				case 'cb':&#60;br /&#62;
					// Center Bottom&#60;br /&#62;
					$start_width 	= 	round(($canvas_width - $stamp_width) / 2);&#60;br /&#62;
					$start_height 	= 	$canvas_height - $mask_padding - $stamp_height;&#60;br /&#62;
					break;&#60;br /&#62;
			}&#60;/p&#62;
&#60;p&#62;			imagecopy( $this-&#38;gt;workingImage, $stamp_image, $start_width, $start_height, 0, 0, $stamp_width, $stamp_height );&#60;br /&#62;
			imagedestroy( $stamp_image );&#60;/p&#62;
&#60;p&#62;			return $that;&#60;br /&#62;
        }&#60;br /&#62;
}&#60;/p&#62;
&#60;p&#62;$pt = PhpThumb::getInstance();&#60;br /&#62;
$pt-&#38;gt;registerPlugin('GdWatermarkLib', 'gd');&#60;br /&#62;
?&#38;gt;&#60;/code&#62;&#60;/p&#62;
&#60;p&#62;//------------------&#60;br /&#62;
$thumb = PhpThumbFactory::create($src);&#60;br /&#62;
$thumb-&#38;gt;adaptiveResize($width, $height);&#60;br /&#62;
$thumb-&#38;gt;createWatermark($mark_image, $position, $padding);&#60;br /&#62;
$thumb-&#38;gt;show();&#60;/p&#62;
&#60;p&#62;//------------------&#60;br /&#62;
$mark_image = 'path/to/mark.png'&#60;br /&#62;
$position =&#60;br /&#62;
&#60;ul&#62;&#60;li&#62;cc = center center&#60;/li&#62;&#60;li&#62;lt = left top&#60;/li&#62;&#60;li&#62;rt = right top&#60;/li&#62;&#60;li&#62;lb = left bottom&#60;/li&#62;&#60;li&#62;rb = right bottom&#60;/li&#62;&#60;li&#62;cb = center bottom&#60;/li&#62;&#60;br /&#62;
&#60;/ul&#62;&#60;br /&#62;
$padding = number like 10 = padding 10px
&#60;/p&#62;</description>
</item>

</channel>
</rss>
