Blog

The Haycroft Media Blog covers all aspects of website design and development from the web browsers being used through to the standards that govern the world wide web. As a website design and development company, it is imperative that we keep abreast of all the latest news and happenings in our field, and here we present the major stories along with tutorials, tips and other web-programming related items.

Watermark images

Quivertree publications are an image library website and like most image library websites, they wanted their images watermarked

There are a lot of scripts floating about for adding watermarks to image as they are served up to the end user, but a lot of these made it too easy for the end user to ascertain the location of the original, ‘un-watermarked’ image, and as there was already a backend process involved in loading the images and their meta-data, it made sense to write code that generated the final watermarked image in advance. I used this fantastic tutorial on Watermarking Images on the Fly in PHP as a starting point and came up with this:

$dir = dirList ('temp_images');//function that returns a list of all images in the temp_images directory - these images were already uploaded to this location by the site admin
if($dir)://if there are images in the temp_images directory, then away we go!
	foreach($dir as $key => $value): //for each image $key = image number, $value = the image name (e.g myImage.jpg)
		$imgsrc = 'temp_images/'.$value; //source image without watermark
		$imgout = 'temp_watermark/'.$value; //where to save the image with the watermark
		watermark_img($imgsrc,$imgout);//this function is below
	endforeach;
else:
	echo 'You need to upload some images first';
endif;

Here is the watermark_img function:

function watermark_img($imgsrc,$saveas) {
		$watermark = imagecreatefrompng('images/watermark.png');  //location of my watermark
		$watermark_width = imagesx($watermark);
		$watermark_height = imagesy($watermark);
		$image = imagecreatetruecolor($watermark_width, $watermark_height);
		$image = imagecreatefromjpeg($imgsrc);
		$size = getimagesize($imgsrc);
		$mainimg_w_pos = $size[0] / 2; //get half of the main image
		$watermark_w_pos = $watermark_width / 2; //get half of the watermark
		$dest_x = $mainimg_w_pos - $watermark_w_pos; //watermark sits at half of width minus half its own width
		$mainimg_h_pos = $size[1] / 2; //get half of the main image
		$watermark_h_pos = $watermark_height / 2; //get half of the watermark
		$dest_y = $mainimg_h_pos - $watermark_h_pos; //watermark sits at half of width minus half its own width
		_ckdir($saveas);//function to test that save as location is available
		imagecopymerge($image, $watermark, $dest_x, $dest_y, 0, 0, $watermark_width, $watermark_height, 100);
		imagejpeg($image,$saveas,100);
	}
[ Back ]

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Facebook Comments