Add Watermark To Photos On Site in SocialEngine

How many of you have wanted to place a watermark on all of your images?

This is a small trick I have found which was for WordPress but have made changes to a few things to allow it to work with SocialEngine

Let’s say you are just starting your own blog and the first solution that comes to mind is to fire up Adobe Photoshop or any other image editing program and place a watermark on all your images. 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 all your images from your blog to your computer, open all of them up and add a watermark. Not good.

But what about the original images? What if you ever need them without the watermark?
Here is my solution. I have used this on a number of blogs and it does the job really well.

What we are going to do is fake the display of an image. What we are displaying instead of the image is a php file that processes the image and adds a watermark to it. The url of the image stays the same, the watermark gets printed on the image and best of all the image on the server doesn’t stays intact.

All you need is a bit of code in your .htaccess file, a php watermark processing file and a watermark image (png preferably).

Add this code in your .htaccess file:

RewriteRule (.*)public/user/(.*\.(jpe?g|gif|png))$ $1watermark.php?p=br&q=90&src=public/user/$2
RewriteRule (.*)public/album_photo/(.*\.(jpe?g|gif|png))$ $1watermark.php?p=br&q=90&src=public/album_photo/$2

Change p=br and q=90 with your preferred watermark position and image quality.

Create a new file in the root of your directory(where social engine site is) and name that file watermark.php. After that copy and paste the code below in that file and save it(obviously).

//we tell the server to treat this file as if it wore an image
header('Content-type: image/jpeg');
//image file path
$img = $_GET['src'];
//watermark position
$p = $_GET['p']; if(!$p) $p = 'br';
/*
p can be anything from the following list:
tl = top left
tc = top center
tr = top right
cl = center left
c = center of the image
cr = center right
bl = bottom left
bc = bottom center
br = bottom right
*/
//watermarked image quality
$q = $_GET['q'];
//if the quality field is missing or is not on the 0 to 100 scale then we set the quality to 93
if(!$q || $q<0 || $q>100) $q = '93';
$filetype = substr($img,strlen($img)-4,4);
$filetype = strtolower($filetype);
if($filetype == ".gif") $image = @imagecreatefromgif($img);
if($filetype == ".jpg") $image = @imagecreatefromjpeg($img);
if($filetype == ".png") $image = @imagecreatefrompng($img);
if (!$image) die();
//getting the image size for the original image
$img_w = imagesx($image);
$img_h = imagesy($image);
//if the filename has 150x150 in it's name then we don't apply the watermark
if (eregi("150x150", $img)) {
    imagejpeg($image, null, $q); die();
} else {
    $watermark = @imagecreatefrompng('watermark.png');
}
/*
//if you want to use the watermark only on bigger images then use this instead of the condition above
if ($img_w < "150") {//if image width is less then 150 pixels
    imagejpeg($image, null, $q); die();
} else {
    $watermark = @imagecreatefrompng('watermark.png');
}
*/
//getting the image size for the watermark
$w_w = imagesx($watermark);
$w_h = imagesy($watermark);
if($p == "tl") {
    $dest_x = 0;
    $dest_y = 0;
} elseif ($p == "tc") {
    $dest_x = ($img_w - $w_w)/2;
    $dest_y = 0;
} elseif ($p == "tr") {
    $dest_x = $img_w - $w_w;
    $dest_y = 0;
} elseif ($p == "cl") {
    $dest_x = 0;
    $dest_y = ($img_h - $w_h)/2;
} elseif ($p == "c") {
    $dest_x = ($img_w - $w_w)/2;
    $dest_y = ($img_h - $w_h)/2;
} elseif ($p == "cr") {
    $dest_x = $img_w - $w_w;
    $dest_y = ($img_h - $w_h)/2;
} elseif ($p == "bl") {
    $dest_x = 0;
    $dest_y = $img_h - $w_h;
} elseif ($p == "bc") {
    $dest_x = ($img_w - $w_w)/2;
    $dest_y = $img_h - $w_h;
} elseif ($p == "br") {
    $dest_x = $img_w - $w_w;
    $dest_y = $img_h - $w_h;
}
imagecopy($image, $watermark, $dest_x, $dest_y, 0, 0, $w_w, $w_h);
imagejpeg($image, null, $q);
imagedestroy($image);
imagedestroy($watermark);

Use any image editor you like and create a png file that you will use as your watermark image. Save that image with the name “watermark.png” and put it in your root directory.

Looking for quality Social Engine Web Hosting? Look no further than Arvixe Web Hosting!

Tags: , , , , , , , , , , , , | Posted under SocialEngine | RSS 2.0

Author Spotlight

Andrew Cross

Andrew Cross

Hi, I'm Andrew. A am a 26, ICT Administrator which offers support to company's around the South Wales Area. I have created a few social sites for myself and other company's. So far 2 out of 3 have been successful using the powerful SocialEngine Script. I continue to try and help the community of SocialEngine by giving Support and Technical Help where possible.

4 Comments on Add Watermark To Photos On Site in SocialEngine

  1. David Meunier says:

    I try you script for watermark but not working I follow carefully your instructions.

Leave a Reply

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


× 1 = 3

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