How To Add Watermark To Image Using PHP
How To Add Watermark To Image Using PHP
HOME
PHP HOW TO ADD WATERMARK TO IMAGE USING PHP?
HOW TO
PHP
AMAN MEHRA
JULY 4, 2021
LEAVE A COMMENT
Tweet on Twitter
Share on Facebook
SUBSCRIBE
RECENT POSTS
CATEGORIES
Table of Contents
Git
1
What is a Watermark?
How To
2
Add Text Watermark to Image Using PHP
3
Add Image Watermark to Image Using PHP JavaScript
4
Some guidelines: Laravel
PHP
ReactJS
Suppose You are a web professional and working on an e-commerce project.
WooCommerce
you have some images of the different products. Now you need to write
‘featured’ or ‘new’? on some products. for doing so you need to edit your images WordPress
of the product.
So you can do it using a simple PHP GD script that’s called a watermark. MOST VIEWED POSTS
Below are some steps to create a watermark on an image you just need to
TIPS & TRICKS
follow these steps:
Create CSV from PHP Array
Step 1: First of all You need to download the ‘arial.ttf’ font file and place it in Get Current Page URL in PHP
your script folder. you can download the font file from here.
How to Take Chrome Full Page
Screenshot Without Extension?
Step 2: Create a new PHP file in your script folder and place the following code
Check IP Address with Python
in your file.
How to Detect IE Browser (Internet
1 <?php Explorer or Edge) in JavaScript?
2
3 function watermarkImage ($SourceFile, $WaterMarkText, $D
4 list($width, $height) = getimagesize($SourceFile);
5 $image_p = imagecreatetruecolor($width, $height);
6 $image = imagecreatefromjpeg($SourceFile);
7 imagecopyresampled($image_p, $image, 0, 0, 0, 0, $w
8 $red = imagecolorallocate($image_p, 255, 0, 0);
9 $font = 'D:\xamp\htdocs\phptest\watermark\arial.ttf
10 $font_size = 10;
11 imagettftext($image_p, $font_size, 40, 100, 100, $r
12 if ($DestinationFile) {
13 imagejpeg ($image_p, $DestinationFile, 100);
14 } else {
15 header('Content-Type: image/jpeg');
16 imagejpeg($image_p, null, 100);
17 };
18 imagedestroy($image);
19 imagedestroy($image_p);
20 }
21
22 $SourceFile = 'avatar.jpg';
23 $DestinationFile = 'avatar-watermarkk.jpg';
24 $WaterMarkText = 'Copyright phpJabbers.com';
25 watermarkImage ($SourceFile, $WaterMarkText, $Destinatio
26
27 ?>
The last 4 lines for holding the data to run the watermark function.
Step 3: If you want to change the value of the $red variable but make sure
your color will be in RGB color. And change the watermark text angle and
position where text will be shown, you can change in the imagettftext().
If you don’t want to download the image and just want to show it on the browser
then set the $DestinationFile variable blank in the if condition like if
($DestinationFile="") .
Step 4: After the change, all the required fields as per your requirements then
save the file and run it on the browser. You will see the image displaying with a
watermark text message.
As we said above, you can also add image watermark to an image. This means,
you can add an image onto another image as a watermark. To do this you need
to use imagecopy() function.
The imagecopy() function will copy the source image onto the destination
image.
Step 1: Create a folder and take two images inside it. One will be main image
and second will be watermal image in PNG format.
Step 2: Now create a new PHP file and paste the following code in this file.
1 <?php
2
3 function watermarkImage ($SourceFile, $WaterMarkImage, $
4 $watermark = imagecreatefrompng($WaterMarkImage);
5 $imageURL = imagecreatefromjpeg($SourceFile);
6 $watermarkX = imagesx($watermark);
7 $watermarkY = imagesy($watermark);
8 imagecopy($imageURL, $watermark, imagesx($imageURL)/
9 if ($DestinationFile) {
10 imagejpeg ($imageURL, $DestinationFile, 100);
11 } else {
12 header('Content-Type: image/jpeg');
13 imagejpeg($imageURL, null, 100);
14 }
15 imagedestroy($imageURL);
16 }
17
18 $SourceFile = 'avatar.jpg';
19 $DestinationFile = 'avatar-watermarkk.jpg';
20 $WaterMarkImage = 'watermark.png';
21 watermarkImage ($SourceFile, $WaterMarkImage, $Destinati
22
23 ?>
This script code will create a new image with watermark image. You have to
provide the images file and required parameter. The last 4 line same as I
explained in above.
Step 3: Save the file and run on the browser. You will see watermark image onto
the main image file.
That’s it!!!
Some guidelines:
$SourceFile – It is the full file path to the JPG image that you are going to
watermark.
$DestinationFile – It can either be blank or full file path to a new file which will
be the source file with watermark text on it.
What this function does is to read the source file, then create a new image object
(using the imagecopyresampled() function). Then using the Arial.ttf font file
and the imagettftext() function it writes the text onto the image and image onto
image. The last IF statement checks, if it should save a new watermarked file or
should just display it on the screen.
Hope you understand the code to add watermark to image using PHP GD script
library.
If you have any quetions please ask me in the comment section. I’ll respond to
you as soon as possible.
Tweet on Twitter
Share on Facebook
How to Test Internet Speed Using Twitter Search API Example Using
Python? PHP
LEAVE A REPLY
Your email address will not be published. Required fields are marked *
Comment
Save my name, email, and website in this browser for the next time I comment.
POST COMMENT
Your Blog Coach is the best site Blog How to Hide Admin Bar in
Name
for finding the solution to any WordPress?
issue related to coding and learn Categories How to Duplicate Pages OR Posts
Your email address
more cool stuff and tricks. in WordPress?
Contact
Laravel Eloquent Methods SUBSCRIBE
About
firstOrNew firstOrCreate
updateOrCreate