Auto watermark for Wordpress.
How many of you have wanted to have a watermark on all of your images?
Lets say you are just starting your blog and you will just fire up Adobe Photoshop or any other image editing program and you will add a watermark on each image you have. It is time consuming but it will work.
But what if you have a blog for a really long time and you just decided you want to watermark all your images. You would have to copy every image on your blog to your computer, open all of them up and add a watermark.
But what about the original images? What if you ever need them without the watermark?
Here is my solution. I have been using this on a number of blogs and it work for me.
I must worn you do that it is for some what advanced users.
What we are going to do is fake the display of an image. What we are actually going to display instead of the image is a php file that processes the image and ads a watermark to it. The url of the image stays the same, the watermark get put on and best of all the image on the server doesn’t get modified at all.
All you need is a bit of code in the .htaccess file, a php watermark processing file and a watermark image (png preferably).
The first: Add this code in your .htaccess file:
RewriteRule ^(.*)wp-content/uploads/(.*) $1watermark.php?src=wp-content/uploads/$2
The second: Create a new file in the root of your directory(where wp-admin, wp-content and wp-include is) and name that file watermark.php. After that copy and paste the code below in that file and save it.
$src = $_GET['src'];
header(’Content-type: image/jpeg’);
//this will prevent the watermark from showing up in the thumbnail images
if (eregi(”150×150″, $src)) {
$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.”);
}
$size = getimagesize($src);
$dest_x = $size[0] - $watermark_width - 0;
$dest_y = $size[1] - $watermark_height - 0;
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);
Use any image editor you like and create a png file that will be your watermark image. Save that image with the name “watermark.png” and put it in your root directory.
Also create a png image which is 1×1pixels and it must be transparent. Name that file “empty.png” and save it also in your root directory.
That 1×1px image is meant to be displayed on the thumbnail images. The thumbnails are to small to but a watermark on them, it wouldn’t look too good.