Watermark

Soldato
Joined
29 Dec 2004
Posts
5,653
Location
Chatham, Kent
I had it on my old site but can't for the life of me find the code I need.

I have a file called empty.png and watermark.png and also a file called watermark.php that has the following code:

Code:
<?
$src = $_GET['src'];

header('Content-type: image/jpeg');

//this will prevent the watermark from showing up in the thumbnail images

$size = getimagesize($src);
if ($size[1] < 400) {
$watermark = imagecreatefrompng('empty.png');
} else {
$watermark = imagecreatefrompng('watermark.png');
}

$watermark_width = imagesx($watermark);
$watermark_height = imagesy($watermark);
$image = imagecreatetruecolor($watermark_width, $watermark_height);

if(eregi('.gif',$src)) {
$image = imagecreatefromgif($src);
}
elseif(eregi('.jpeg',$src)||eregi('.jpg',$src)) {
$image = imagecreatefromjpeg($src);
}
elseif(eregi('.png',$src)) {
$image = imagecreatefrompng($src);
}
else {
exit("Your image is not a gif, jpeg or png image. Sorry.");
}

$dest_x = ($size[0] - $watermark_width) / 2;
$dest_y = ($size[1] - $watermark_height) / 2;  
imagecolortransparent($watermark,imagecolorat($watermark,0,0));
imagecopyresampled($image, $watermark, $dest_x, $dest_y, 0, 0, $watermark_width, $watermark_height, $watermark_width, $watermark_height);

imagejpeg($image, "", 95);
imagedestroy($image);
imagedestroy($watermark);
?>

Now all I need to do is somehow include that code, how would I go about including my site to pull from that file?

Thanks,

Andy
 
All i need is a bit of code to request that file in php format.

I think i used to use mod_rewrite.

Andy
 
Last edited:
Maybe something like:

Code:
RewriteRule ^_watermarked/([^/]+)$ /watermark.php?src=$1

in your htaccess, then calling:

Code:
<img src="/_watermarked/my-image.jpg" />

should do the trick.
 
Maybe something like:

Code:
RewriteRule ^_watermarked/([^/]+)$ /watermark.php?src=$1

in your htaccess, then calling:

Code:
<img src="/_watermarked/my-image.jpg" />

should do the trick.

I've added the rewrite rule, but where would i place the img src bit?

the watermark.png is in my root, but where would i put the code? Into the header of my site?

I have found another way as i'm using vBulletin.
-------------------------------------------
Ok i found a different plugin which i think could be altered.

All i need is so that it only watermarks on images that are 400px wide or greater.

Here is the vBulletin code:

Code:
// begin variables

// the path to the PNG file that you want to overlay with
// must be on the local machine, not an http:// URL
$WATERMARK_PNG_FILE = '/home/xxxxxx/public_html/watermark.png';

// how you want to position the watermark enter either center or bottom_left
$WATERMARK_POSITION = 'center';

// user you want to see watermarking on - this is just for testing
// set to 0 to work for all users
$VB_USER_ID = 0;

// end variables - you shouldn't need to edit below here


// if there's no filepath to work with there's nothing we can do
// we also need the watermark file to exist
// if $VB_USER_ID is specified (for testing) show it just them
if(! empty($attachpath) && file_exists($WATERMARK_PNG_FILE) && ( $VB_USER_ID == 0 || $GLOBALS['vbulletin']->userinfo['userid'] == $VB_USER_ID )) {


	// derive output name
	$fo = preg_replace('/\.attach$/', '.marked', $attachpath);

	// image doesn't exist or thumbnail is newer than the cached file - create a watermarked version
	if( (! file_exists($fo) || filemtime($WATERMARK_PNG_FILE) > filemtime($fo) || true) && $fo != $attachpath ) {

		// decide what image type it is
		if( preg_match('/\.png$/i', $attachmentinfo['filename']) ) {
			$im = @imagecreatefrompng($attachpath);
		} elseif( preg_match('/\.jpg$/i', $attachmentinfo['filename']) ) {
			$im = @imagecreatefromjpeg($attachpath);
		
			// create an empty truecolor container
			$tempimage = @imagecreatetruecolor(@imagesx($im), @imagesy($im));
       
			// copy the 8-bit gif into the truecolor image
			@imagecopy($tempimage, $im,
				0, 0, 0, 0, 
				@imagesx($im), @imagesy($im)
			);
      
			// copy the source_id int
			$im = $tempimage;
		}

		// open the watermark image
		$wm = @imagecreatefrompng($WATERMARK_PNG_FILE);

		@imagealphablending($wm, false);
		@imagesavealpha($wm, true);

		// catch opening problems
		if($im && $wm) {

			if($WATERMARK_POSITION == 'center') {
				$pos_x = (imagesx($im) / 2) - (imagesx($wm) / 2);
				$pos_y = (imagesy($im) / 2) - (imagesy($wm) / 2);
			} else {
				$pos_x = imagesx($im) - imagesx($wm) - 10;
				$pos_y = imagesy($im) - imagesy($wm) - 10;
			}


			// merge the files together
			$copy_worked = @imagecopy($im,$wm,
     				$pos_x, $pos_y,
				0, 0, imagesx($wm), imagesy($wm)
			);
		}

		// write out the new image
		if($copy_worked) {

			// decide what image type it is
			if( preg_match('/\.png$/i', $attachmentinfo['filename']) ) {
				@imagepng($im,$fo);
			} elseif( preg_match('/\.jpg$/i', $attachmentinfo['filename']) ) {
				@imagejpeg($im,$fo);
			} elseif( preg_match('/\.gif$/i', $attachmentinfo['filename']) ) {
				@imagegif($im,$fo);
			}
		}
	}

	// check that the new file is there - if so open the file pointer to it
	if( @filesize($fo) > 0 && $fp2 = fopen($fo,'rb') ) {

		// re-send image size header
		header('Content-Length: '. filesize($fo));

		@fclose($fp);

		$fp = $fp2;

	}
}

Any ideas?

Thanks,

Andy
 
Back
Top Bottom