Water marking images by PHP GD support

We can water mark pictures by adding our logo or any other picture to a main picture by using GD library and PHP. You must have seen this in many picture gallery sites where images are protected by adding a water mark logo to the main picture. Some time after uploading images the script adds water mark to the uploaded image on the fly. How this is done ? We will learn that here.

We will try to learn the basic script of how to add one water mark image over a main image but this can be a part of many scripts. Some of the common application can be automatic adding of water mark to uploaded images, reading all images of a directory and then creating another directory with water mark added images. We will only try to learn on adding the water mark image to a single file.

We need one water mark image created. This should be our logo or any other image which we want to be imposed or added to the main image. You can read the article on how to create water mark image using PhotoShop here. Once this image is ready with us we will develop the script to add this to our main image. Here we have kept our main image inside a directory known as source directory. After adding the water mark image we will store the water marked image inside another directory named as destination directory. We must have write permission to this destination directory to create the file inside it. Here are the settings we want for our script. You can change the settings as per your requirement.
$file_name="f1.jpg"; // File name on which water mark is to be added
$add="source/$file_name"; // Source directory location
$add2="destination/$file_name"; // Destination directory location
$water_img="wtt7.gif"; // Water Image file
You can see we have kept the water marked logo in root where the script is running. You can change all the above settings as per your requirement. Now we will read both the image sizes. Note that the array first element will store the width of the image and second element will store the height of the image. We will read dimensions for both our main image and water mark log image. Here it is
$img_ar=GetImageSize ($add); // reading source image size
$img_wt_ar=GetImageSize ($water_img); // reading water image size
Note that the above two variables are arrays. Now let us decide on the location on our main image where our water mark will be added. Note that left top corner is our 0 , 0 location. Let us fix this at 300 x 300 to keep the mark at center for a 600 x 600 image. You can change these values as per your requirement.
$location_height=300; // Location of water mark
$location_width=300;  // Location of water mark
Now here is our main script which creates the water marked image
$im=ImageCreateFromJpeg($add);  // for main image
$water_img1=ImageCreateFromgif($water_img); // for water image
$newimage=imagecreatetruecolor($img_ar[0],$img_ar[1]);//
imageCopyResized($newimage,$im,0,0,0,0,$img_ar[0],$img_ar[1],
$img_ar[0],$img_ar[1]); $t=ImageCopy($newimage,$water_img1,$location_width,$location_height,
0,0,$img_wt_ar[0],$img_wt_ar[1]);
Take care about the write permission at destination directory. This script is in use at India travel picture gallery.

Here is the total script.
$file_name="f1.jpg"; // File name on which water mark is to be added
$add="source/$file_name"; // Source directory location
$add2="destination/$file_name"; // Destination directory location 
$water_img="wtt7.gif"; // Water Image file

///////////////////////////Settings for the script over////////////

$img_ar=GetImageSize ($add); // reading source image size
$img_wt_ar=GetImageSize ($water_img); // reading water image size

$location_height=300; // Location of water mark
$location_width=300;  // Location of water mark

$im=ImageCreateFromJpeg($add);  // for main image
$water_img1=ImageCreateFromgif($water_img); // for water image
$newimage=imagecreatetruecolor($img_ar[0],$img_ar[1]);//
imageCopyResized($newimage,$im,0,0,0,0,$img_ar[0],$img_ar[1],
$img_ar[0],$img_ar[1]); $t=ImageCopy($newimage,$water_img1,$location_width,$location_height,
0,0,$img_wt_ar[0],$img_wt_ar[1]); ImageJpeg($newimage,$add2,100); chmod("$add2",0666);

GD functions
Subscribe to our YouTube Channel here


Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com







    Post your comments , suggestion , error , requirements etc here





    PHP video Tutorials
    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2024 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer