Image Watermarks in WordPress

Here a quick and easy way to put watermarks in your images. Just copy the code in the function.php of the active theme and you can watermark your pictures in wordpress admin.

Features:
- Set watermarks for galleries
- Watermark button on gallery screen
- Set position of watermark in source code
- Pictures gets the watermark only once also if you click again
- Inserts watermarks only in the big version of the file (not in thumbnails)
- Watermarks are not removable from the images

Install and Requirements:
- copy the source code to the functions.php
- Image Magick (composite command) is required

 
add_filter('media_upload_form_url', 'bohuco_upload_gallery');
 
if(isset($_GET['tab']) && $_GET['tab'] == 'gallery') {
	if ($_POST['attachments']) {
		$uploadDir = wp_upload_dir();
		foreach($_POST['attachments'] as $id => $a) {
			$a = wp_get_attachment_metadata($id, true); // 'full'
			if($a['image_meta']['copyright'] != '_WATERMARK_') {
				ffs_image_modify($uploadDir['basedir'].'/'.$a['file']);
				$a['image_meta']['copyright'] = '_WATERMARK_';
				wp_update_attachment_metadata($id, $a);
			}
		}
	}
}
 
/**
 * adds the watermark button to the gallery dialog
 */
function bohuco_upload_gallery($var) {
	if(isset($_REQUEST['watermark']) && ! empty($_REQUEST['watermark'])) {
		$html = "<script type=\"text/javascript\">jQuery(function(){ jQuery('<tr><td colspan=2>Watermark added!</td></tr>').appendTo('#gallery-settings table'); }); </script>";	
	} else {
		$html = "<script type=\"text/javascript\">jQuery(function(){ jQuery('<tr><td><input type=\"submit\" class=\"button savebutton\" name=\"watermark\" id=\"save-all\" value=\"Insert watermark\"></td></tr>').appendTo('#gallery-settings table'); }); </script>";	
	}
	echo $html;	
	return $var;
}
 
/**
 * modify an image
 */
function bohuco_image_modify($file) {
	// watermark image ... use png or gif with alpha
	$watermark = dirname(__FILE__).'/images/watermark.png';
 
	// imagemagick command ... set gravity to NorthWest, North, NorthEast, West, Center, East, SouthWest, South, SouthEast
	$command = sprintf("composite -gravity Center %s %s %s", $watermark, $file, $file);
 
	exec($command, $return, $code);
	return $code;
}