Skip to main content

PHP function to add watermark on an image


How to apply watermark on a image?

Watermarking means applying an image with a transparent/non transparent background on another image to protect the image from unauthorised use of it by other parties which doesn;t own the image.

The following function can be used to add watermark on images.





The function accepts 2 parameters

1. the source image - the image on which watermark has to be applied
2. the position of the watermark image on the source image.





*****************CODE START***************

<?php

function apply_watermark($imagesource, $position = 'bottomright'){
$info = getimagesize($imagesource);
if($info){
switch($info[2]){
case 1 : { $image = imagecreatefromgif($imagesource); break;}//gif
case 2 : { $image = imagecreatefromjpeg($imagesource); break; }//jpg
case 3 : { $image = imagecreatefrompng($imagesource); break; }//png
default : { exit("Unsupporter file type"); }
}
}
else{
print "Can't read required image data";
}

//watermark image
$watermark = @imagecreatefrompng(getenv('DOCUMENT_ROOT').'/images/logo.png');
$imagewidth = imagesx($image);
$imageheight = imagesy($image);
$watermarkwidth = imagesx($watermark);
$watermarkheight = imagesy($watermark);
switch($position){
case 'topleft':
$startwidth = 0;
$startheight = 0;
break;
case 'topright':
$startwidth = ($imagewidth - $watermarkwidth);
$startheight = 0;
break;
case 'middle':
$startwidth = (($imagewidth - $watermarkwidth)/2);
$startheight = (($imageheight - $watermarkheight)/2);
break;
case 'bottomleft':
$startwidth = 0;
$startheight = ($imageheight - $watermarkheight);
break;
case 'bottomright':
default:
$startwidth = ($imagewidth - $watermarkwidth);
$startheight = ($imageheight - $watermarkheight);
break;
}
imagecopy($image, $watermark, $startwidth, $startheight, 0, 0, $watermarkwidth, $watermarkheight);

// Output final image
switch($info[2]){
case 1 : { imagegif($image, $imagesource, 100); break;}//gif
case 2 : { imagejpeg($image, $imagesource, 100); break; }//jpg
case 3 : { imagepng($image, $imagesource, 100); break; }//png
default : { exit("Unsupporter file type"); break;}
}
imagedestroy($watermark);
}


?>

*****************CODE END***************



Comments

Popular posts from this blog

Intex Aqua 5.5 VR Plus genuine Review - Dont Buy Intex Aqua 5.5 VR Plus - Board complaint and low battery backup issues

Intex Aqua 5.5 VR Plus  Review - Dont Buy Intex Aqua 5.5 VR + - Board complaint and low battery backup issues I bought an Intex Aqua 5.5 VR Plus on April 23, 2018, With in a week it started to show Battery backup issues. Even if it is charged full, it will completely drain out with in 12 or 13 hours. During this time No internet was used, only 2 or 3 calls were done. Some times there was issues with net connection also.

Deep-sea Anglerfish Black Seadevil Scary looking creature Video

Deep-sea Anglerfish are the strange and elusive creature that are very rarely observed in their natural habitat. Fewer than half a dozen have ever been captured on film or video by deep-diving research vehicles.They are mostly  found in tropical to temperate waters of the Indian,Pacific and Atlantic Oceans.

How to use WiFi adapter on Ubuntu 16.04 desktop PC - Realtek RTL8188EUS 802.11n Wireless USB Network Adapter Driver installation

Installation of Realtek RTL8188EUS 802.11n Wireless USB Network Adapter on Desktop PC having Ubuntu 16.04 OS My PC is running in Ubuntu 16.04 OS, recently I thought of using a dongle wife adapter to access our home's Wifi network. For this I used Realtek RTL8188EUS 802.11n Wireless USB Network Adapter ( Model No: OT-WUA950NM ) This small device cost around Rupees 250/- to Rs 300/- in India. I did the following steps for installation of this Realtek Nano Wifi Adapter: Plug Realtek RTL8188EUS 802.11n Wireless USB Network Adapter to your PC's USB port, Take the terminal application and run the command "lsusb" to list the plugged in usb devices: Myhome:~$ lsusb Bus 007 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub Bus 002 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub Bus 006 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub Bus 005 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub Bus 001 Device 003: ID 0bda:8179 Realtek Semiconductor Corp. R...


Urgent Openings for PHP trainees, Andriod / IOS developers and PHP developers in Kochi Trivandrum Calicut and Bangalore. Please Send Your updated resumes to recruit.vo@gmail.com   Read more »
Member
Search This Blog