Add watermark text logo to images and save using PHP - Mostlikers

11 January, 2017

Add watermark text logo to images and save using PHP

Hi guys, After a long time I am posting a new topic. Today we are going see about How to Add watermark text logo to images and save using PHP script. Watermark is the best method to protect your website images. Specially E-commerce, Matrimony website users want to protect they are personal images and digital photo. For those kinds of websites, I would suggest to you add watermark text in your website's images.

Add watermark text logo to images and save using PHP

Live demo        Download

 For watermark, you can add text or your brand logo. My live demo has used my website logo. if you want to make logo watermark, The logo should be a png format.


<form method="post" enctype="multipart/form-data">
    <input type="file" name="image_file" />
    <input type="submit" value="submit_form" />

The above HTML form action work same. So add below PHP code on the same page.


The PHP I have used my site logo for a watermark. the code will be work based on PHP gd and image functions. 
  • imagecreatefrompng, imagecreatefromjpeg, imagecreatefromgif - it will create a new image from files.
  • $marge_right and bottom - it will fix the image position bottom and right side.
  •  imagecopy - It will copy the image file.

    $destination_folder = 'uploads/';    
    $image_name = $_FILES['image_file']['name']; //file name
    $image_temp = $_FILES['image_file']['tmp_name']; //file temp
    $image_type = $_FILES['image_file']['type']; //file type
    $extension = strtolower($image_type);
    if($extension == 'image/png') :
        $im =  imagecreatefrompng($image_temp);
    elseif($extension == 'image/jpeg' || $extension == 'image/pjpeg'):
        $im = imagecreatefromjpeg($image_temp);
    elseif($extension == 'image/gif'):
        $im =  imagecreatefromgif($image_temp);
        echo 'Unsupport file. try valid format'; exit;
    // your watermark logo image 
    $watermark_img = imagecreatefrompng('mostlikers.png');
    // right and left side padding
    $marge_right = 20; $marge_bottom = 20;
    $sx = imagesx($watermark_img);
    $sy = imagesy($watermark_img);
    imagecopy($im, $watermark_img, imagesx($im) - $sx - $marge_right, 
    imagesy($im) - $sy - $marge_bottom, 0, 0, imagesx($watermark_img), 
    //new file name 
    $file_name = time().$image_name;
    imagepng($im, $destination_folder.$file_name);
    // write here insert or update mysql code.
    header('Content-type: image/png');
    imagepng($im); // display saved image

Text watermark Script

The above code only work has an image watermark. For a text, watermark refers the below code.
$watermark_img = imagecreatetruecolor(200, 70);
imagefilledrectangle($watermark_img, 0, 0, 200, 69, 0x0000FF);
imagefilledrectangle($watermark_img, 9, 9, 200, 60, 0xFFFFFF);
imagestring($watermark_img, 20, 20, 20, 'mostlikers', 0x0000FF);