Navigation:    Home arrow Watermark on Uploaded Image
Image Watermark PDF  
Digg Reddit Ma.gnolia Stumble Upon Facebook Twitter Google Yahoo! MyWeb Furl" BlinkList Technorati Mixx Bookmark

This tutorial shows you how you can add a watermark image over images that are uploaded though a form. 

The following is a html code as simple as it can be with an image type input and a Submit button:

<form name="submit_image" method="post" enctype="multipart/form-data"  action="">

<input type="file" name="image">
<input name="Submit" type="submit" value="Upload Picture">


This is the function that applies the watermark over the uploaded picture and uploads the picture on the server. It considers that the location where the images are to be uploaded is a folder named "images":

function copy_marked($img_name,$newname,$type)
// check image type. The image type is sent as parameter 3 ($type). Note that for every image type a different function is used to create an image in memory from the uploaded file.

   if(!strcmp("image/jpg",$type) || !strcmp("image/jpeg",$type) || !strcmp("image/pjpeg",$type))



// get image size, we'll need it later

// create destination image


    // on this demo, the watermark image will be named watermark.gif and will be located in images folder. If you intend to use other file, you will have to change this

// you can setup the transparency used for watermark image.

// just in case you don't know what extension the watermark file has. This function is listed lower

// create an image from watermark file
    if(!strcmp("jpg",$wext) || !strcmp("jpeg",$wext)) $watermark=imagecreatefromjpeg($watermark_file);

    if(!strcmp("png",$wext)) $watermark=imagecreatefrompng($watermark_file);

    if(!strcmp("gif",$wext)) $watermark=imagecreatefromgif($watermark_file);

// get watermark width
    $watermark_width = imagesx($watermark);
    $watermark_height = imagesy($watermark);

// place watermark image on the right left of the main image
    $dest_x = $old_x - $watermark_width - 5;
    $dest_y = $old_y - $watermark_height - 5;

 // uncomment these lines and comment the ones above if you want to place the watermark in the very center of the image
//    $dest_x = (($thumb_w - $watermark_width)/2);
//    $dest_y = (($thumb_h - $watermark_height)/2);
// merge the two images
  imagecopymerge($dst_img, $watermark, $dest_x, $dest_y, 0, 0, $watermark_width, $watermark_height, $transparency);

 // copy the new created image to the destination file, in the images folder
   if(!strcmp("image/png",$type))  imagepng($dst_img,$newname);

    else if(!strcmp("image/gif",$type))  imagegif($dst_img,$newname);

    else imagejpeg($dst_img,$newname);
// delete the images from memory



 Function getFileExtension(), needed to get the file extension:

function getFileExtension($str) {
        $i = strrpos($str,".");
        if (!$i) { return ""; }
        $l = strlen($str) - $i;
        $ext = substr($str,$i+1,$l);
        return $ext;

 Having all these functions, here is how you verify if the form was submitted and call the copy_marked() function to upload the file and place the watermark:

    if ($_FILES['image']['name'])