1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

[TUTORIAL] How to Watermark all Your Uploaded Images With a Simple Script

Discussion in 'PHP & Perl' started by jetiz, Apr 22, 2010.

  1. jetiz

    jetiz Junior Member

    Jan 16, 2010
    Likes Received:
    This post is a copy from some website. I felt like sharing this useful post with you guys.

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

    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 been using 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 ads 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).

    Step 1

    Add this code in your .htaccess file:

    RewriteRule ^(.*)wp-content/uploads/(.*) $1watermark.php?src=wp-content/uploads/$2  

    Step 2

    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(obviously).

    $src = $_GET['src'];
    header('Content-type: image/jpeg');
    //this will prevent the watermark from showing up in the thumbnail images
    if (eregi("150x150", $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;
    imagecopyresampled($image, $watermark, $dest_x, $dest_y, 0, 0, $watermark_width, $watermark_height, $watermark_width, $watermark_height);
    imagejpeg($image, "", 95);

    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.

    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 put a watermark on them, it wouldn't look too good.
    • Thanks Thanks x 2
  2. Killswitch

    Killswitch BANNED BANNED

    Aug 9, 2009
    Likes Received:
    Looks good, although one thing I'd suggest is not to search for thumbnail by filename, but to use the dimensions of the image itself to see if it's to small to watermark or not.
  3. tacopalypse

    tacopalypse Executive VIP Jr. VIP Premium Member

    Nov 30, 2009
    Likes Received:
    Home Page:
    that's fuckin awesome. i'm actually learning php right now. i had no idea functions already existed that could do stuff like that.
  4. ghprod

    ghprod Regular Member

    Mar 18, 2009
    Likes Received:
    Home Page:
    this is nice trick but if you get a lot of visitor then your hosting will screaming ... lol ..