How to Create Watermark Effect in PHP

Watermarking helps in protecting images from being used by other people. As a blogger, you should use this option to protect your original images. You have some images on your website that needs to be watermarked, you should try watermarking tools. If you think your next web app needs thins kind of feature where users upload photos and add watermark on it, you can try coding it. If you are using PHP, I have a perfect solution.
Create Watermark Effect in PHP
The simple solution to this problem is to add watermark with PHP. In this tutorial, I will show you how to create a watermark effect using PHP.
In the given code, I will add logo.png as a copyright watermark to the other image MyLogo.gif
See the code below for image to image watermark
<?php $myImage = imagecreatefromgif('MyLogo.GIF'); $Copyright = imagecreatefrompng('logo.png'); $dWidth = imagesx($myImage); $dHeight = imagesy($myImage); $sWidth = imagesx($Copyright); $sHeight = imagesy($Copyright); $destX = ($dWidth - $sWidth) / 2; $destY = ($dHeight - $sHeight) / 2; $white = imagecolorexact($Copyright, 255, 255, 255); imagecolortransparent($Copyright, $white); imagecopymerge($myImage, $Copyright, $destX, $destY, 0, 0, $sWidth, $sHeight, 50); header("Content-type: image/jpeg"); imagejpeg($myImage); imagedestroy($myImage); imagedestroy($Copyright); ?>
If you want text to image watermark, this code can be useful
<?php $string = "Deepanker"; $im = ImageCreateFromJpeg("welcome.jpeg"); $color = ImageColorAllocate($im, 255, 0, 0); $px = (Imagesx($im) - 6.5 * strlen($string))/2; $px = Imagesy($im)- 20; ImageString($im, 200, $px, $px, $string, $color); imageJpeg($im,"welcome.jpeg"); ImageDestroy($im); ?>
Try the code and let me know whether it works or not.
In case of any problem, you can comment below.
Leave a comment
Comment policy: We love comments and appreciate the time that readers spend to share ideas and give feedback. However, all comments are manually moderated and those deemed to be spam or solely promotional will be deleted.