Closed Thread
Results 1 to 4 of 4
Like Tree2Likes
  • 1 Post By Zubair
  • 1 Post By diabolo

Thread: Watermark on Uploaded Image Using PHP

  1. #1
    vishal's Avatar
    vishal is offline -::-X10 Guru-::- vishal has a brilliant futurevishal has a brilliant future
    Join Date
    Nov 2009
    Location
    INDIA
    Posts
    5,250

    Smile Watermark on Uploaded Image Using PHP

    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:
    Code:
    <form name="image" method="post" enctype="multipart/form-data"  action="">
    
    <input type="file" name="image">
    <input name="Submit" type="submit" value="Upload ">
    
    </form>
    This is the function that applies the watermark over the uploaded picture and uploads the picture on the server.
    Code:
    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))
            $src_img=imagecreatefromjpeg($img_name);
    
        if(!strcmp("image/png",$type))
            $src_img=imagecreatefrompng($img_name);
    
        if(!strcmp("image/gif",$type))
            $src_img=imagecreatefromgif($img_name);
    
    // get image size, we'll need it later
        $old_x=imageSX($src_img);
        $old_y=imageSY($src_img);
    
    // create destination image
        $dst_img=ImageCreateTrueColor($old_x,$old_y);
    
        imagecopyresampled($dst_img,$src_img,0,0,0,0,$old_x,$old_y,$old_x,$old_y);
    
        // 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
        $watermark_file='images/watermark.gif';
    
    // you can setup the transparency used for watermark image.
        $transparency=50;
    
    // just in case you don't know what extension the watermark file has. This function is listed lower
       $wext=getFileExtension('images/watermark.gif');
    
    // 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
        imagedestroy($dst_img);
        imagedestroy($src_img);
    
    }
    Code:
    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:
    Code:
    if($_SERVER['REQUEST_METHOD']=='POST')
    {
        if ($_FILES['image']['name']) 
        {
            $image=$_FILES['image']['name'];
             $newname="http://forums.x10hosting.com/images/".$image;
            copy_marked($_FILES['image']['tmp_name'],$newname,$_FILES['image']['type']);
        }    
    }
    Last edited by vishal; 01-11-2010 at 08:02 PM.
    Regards ~ Vishal
    Giving Reputation (at bottom of my post ) is the best way to encourage the person who helped you on forums.

  2. #2
    Zubair's Avatar
    Zubair is offline x10 Super Spammer Zubair has a reputation beyond reputeZubair has a reputation beyond reputeZubair has a reputation beyond repute
    Join Date
    Jul 2009
    Location
    Pakistan
    Posts
    8,743

    Re: Watermark on Uploaded Image Using PHP

    Thanks for the great tutorial.
    dinomirt96 likes this.
    Zubair Barkat | Level 2 Tech
    █ 888-X10-9668 - zubair[@]x10hosting.com
    x10Hosting - Giving Away Hosting Since 2004

  3. #3
    diabolo's Avatar
    diabolo is offline Community Advocate diabolo is on a distinguished road
    Join Date
    Nov 2007
    Location
    Jersey Shore
    Posts
    1,683

    Re: Watermark on Uploaded Image Using PHP

    did you write this script yourself?
    did you have permission to copy it onto another forums?

    http://www.google.com/search?q=funct...hl=en&filter=0
    karimirt47 likes this.

  4. #4
    vishal's Avatar
    vishal is offline -::-X10 Guru-::- vishal has a brilliant futurevishal has a brilliant future
    Join Date
    Nov 2009
    Location
    INDIA
    Posts
    5,250

    Re: Watermark on Uploaded Image Using PHP

    Quote Originally Posted by diabolo View Post
    did you write this script yourself?
    did you have permission to copy it onto another forums?

    http://www.google.com/search?q=funct...hl=en&filter=0
    One of my friend downloded it long back ago,, well if it is not allowed in x10 i will remove it or add refrence link
    Regards ~ Vishal
    Giving Reputation (at bottom of my post ) is the best way to encourage the person who helped you on forums.

Closed Thread

Similar Threads

  1. Need image gallery php code
    By nimisha in forum The Marketplace
    Replies: 1
    Last Post: 01-10-2010, 12:28 PM
  2. How to get number of uploaded bytes in php
    By oracle in forum Programming Help
    Replies: 23
    Last Post: 12-17-2009, 06:41 PM
  3. Replies: 1
    Last Post: 12-20-2008, 06:44 PM
  4. Noticia De upgrade PHP espaņol
    By figu120 in forum General
    Replies: 2
    Last Post: 11-22-2007, 05:42 PM
  5. Create image in PHP
    By alfred in forum Tutorials
    Replies: 2
    Last Post: 10-21-2007, 11:06 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers