You are on page 1of 2

Blog Categories  Tips & Tricks Contact

HOME
PHP HOW TO ADD WATERMARK TO IMAGE USING PHP?

How to Add Watermark to Image Using FOLLOW US


PHP? Stay updated via social channels

HOW TO
PHP

AMAN MEHRA
JULY 4, 2021
LEAVE A COMMENT

Tweet on Twitter
Share on Facebook

Get New Post By Email


Name

Your email address

SUBSCRIBE

RECENT POSTS

How to Hide Admin Bar in


WordPress?
Do you know what is what is a watermark and how you can protect your How to Duplicate Pages OR Posts
images by adding watermark on them using PHP programmatically? in WordPress?

Laravel Eloquent Methods


NO? firstOrNew firstOrCreate
updateOrCreate
Don’t worry!
How to Test Internet Speed Using
Python?
In this tutorial, we will learn how to add a watermark on the image using PHP
Fix WordPress Fatal Error Allowed
and what it is actually.
Memory Size Exhausted

So let’s get started.

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

What is a Watermark? Python

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

WooCommerce Remove Update


You can create different types of watermarks on all of your images using PHP Cart Button and Make it
script. With this PHP GD script, you can add text as a watermark and also image Automatically Update.
as a watermark. Let’s start work with this library script.
How to Redirect to Checkout After
Add to Cart?
Add Text Watermark to Image Using PHP
How to Change Price of Specific
Product and Quantity in Cart?
Here I am going to show you a way how you can dynamically put a text
message on your images of the products. Upload Multiple Featured Images
in a Post OR Page
Only you need it? JPG product image file and a font file used to generate the
How can I Prevent SQL Injection in
watermark message. Here I am using Arial font which you can also download PHP?
from google.

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 ?>

In the above code, we created a function watermarkImage() and did the


functionality for adding watermark on image inside.

The last 4 lines for holding the data to run the watermark function.

$SourcceFile: The main image file path

$DestinationFile: Output image name

$WaterMarkText: Text you want to add on the image as watermark.

And last is hitting the watermarkImage() function to create a watermark image


with text.

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.

Add Image Watermark to Image Using PHP

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.

Syntax: imagecopy($destinationImage, $srcImage,


$destinatioX, $destinationY, $sourceX, $sourceY,
$sourceWidth, $sourceHeight);

Follow the below steps to add image watermark:

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:

As you can see watermarkImage() image function in the script above.

which takes 3 parameters ($SourceFile, $WaterMarkText, $DestinationFile) and


creates an watermarked image from the source image specified.

$SourceFile – It is the full file path to the JPG image that you are going to
watermark.

$WaterMarkText and $WaterMarkImage – It is the text message and image


that you want to use for the 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.

ADD WATERMARK TO IMAGE GETIMAGESIZE() HOW TO USE WATERMARK


IMAGECOLORALLOCATE() IMAGECOPY() IMAGECOPYRESAMPLED()
IMAGECREATEFROMJPEG() IMAGECREATEFROMPNG() IMAGECREATETRUECOLOR()
IMAGEDESTROY() IMAGEJPEG() IMAGESX() IMAGESY() LIST() PHP
PHP ADD WATERMARK TO IMAGE AND SAVE PHP SCRIPT FOR WATERMARK
PHP WATERMARK IMAGE WITH TEXT WATERMARK PATTERN WATERMARK PHP SCRIPT
WATERMARK STYLE

Tweet on Twitter
Share on Facebook

YOU MAY ALSO LIKE

How to Hide Admin Bar in How to Duplicate Pages OR Posts in


WordPress? WordPress?

How to Test Internet Speed Using Twitter Search API Example Using
Python? PHP

How to Change Proceed to Checkout Most Useful PHP Array Functions


Button Text in WooCommerce?

ABOUT THE AUTHOR: AMAN MEHRA


Hey! I'm Aman Mehra and I'm a full-stack developer and have 5+
years of experience. I love coding and help to people with this blog.

LEAVE A REPLY

Your email address will not be published. Required fields are marked *

Comment

Name * Email * Website

Save my name, email, and website in this browser for the next time I comment.

POST COMMENT

ABOUT QUICK LINKS RECENT POSTS GET IN TOUCH

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

© 2020 Your Blog Coach Privacy Policy


Terms and Conditions
Sitemap

You might also like