Add Watermark on Image Using PHP
This post is all about add watermark on the image using PHP. In general image recreation in PHP by adding some text or some color or resize it height and width is done by PHP GD library. So in this scenario, PHP provides us some in-built function to do such kind of put text mark or watermark on images. So Its very simple to do. I have provides some below function list with description, which can easily understand. If you want to download the source code, click the below link to download.
First, we have to enable php_gd2 PHP extension.
Go to php.ini
;extension=php_gd2.dll
to
extension=php_gd2.dll
Download the different font from google.
Here I download from below links
https://www.wfonts.com/
Table of Contents
index.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
<?php function add_watermark() { //imagecreatefrompng(image) — Create a new image from given image. // This is for png images, if you want .jpg image then the function witll change to imagecreatefromjpeg(); $image = imagecreatefrompng("image.png"); // This function will provide text color on the image. $textcolor = imagecolorallocate($image, 255, 255, 255); //imagesx(image) - Return the width of Given Image. //imagesy(image) - Return the height of given image. //imagettftext(image source, font size, angle, x axis, y axis, text color, font path, watermar text) - Add text on to image. imagettftext($image, 14, 0, imagesx($image)-125, imagesy($image)-20, $textcolor, 'HP-Arial.ttf', "SarojText"); header("Content-type: image/png"); //imagepng(image) - create png image //This function for png, if you want make jpg image then the function will be imagejpeg(); imagepng($image); //imagedestroy(image) - destroy image imagedestroy($image); } echo '<img src="'.add_watermark().'" />'; ?> |
Download Source code
Please leave a comment on this post, add watermark on the image using PHP.