How to Add Watermark in Image using PHP?
Hi Dev,
In this tutorial, you will learn how to add watermark in image using php. I would like to show you php watermark image transparent png. if you want to see example of php add watermark to image and save then you are a right place. We will use how to add watermark image in php.
In this tutorial, i will give you two examples of how to add a watermark in images using php. The first example we will take png image and add as watermark. The second example we will use jpg image and add as a watermark. we will use imagecreatefrompng(), imagesx(), imagesy(), getimagesize(), imagecopy(), imagepng(), imagedestroy(), imagecreatefromjpeg() and imagejpeg() functions for adding image to watermark.
So, let's see both examples.
Example 1: Using PNG Image
In First example, we will use png image and add watermark on it. You can download dummy images that i used.
Let's copy below code and create index.php file.
index.php
<?php
header('content-type: image/png');
$sourceImage = 'demo.png';
$myWatermark = imagecreatefrompng('watermark_image.png');
$watermarkWidth = imagesx($myWatermark);
$watermarkHeight = imagesy($myWatermark);
$image = imagecreatefrompng($sourceImage);
$imageSize = getimagesize($sourceImage);
$wmX = $imageSize[0] - $watermarkWidth - 20;
$wmY = $imageSize[1] - $watermarkHeight - 20;
imagecopy($image, $myWatermark, $wmX, $wmY, 0, 0, $watermarkWidth, $watermarkHeight);
/*
-------- If you want to save image -------
imagepng($image, 'watermark_image_save.png');
*/
imagepng($image);
imagedestroy($image);
imagedestroy($myWatermark);
Output:
Example 2: Using JPG Image
In First example, we will use jpg image and add watermark on it. You can download dummy images that i used.
Let's copy below code and create index.php file.
index.php
<?php
header('content-type: image/jpeg');
$sourceImage = 'demo2.jpg';
$myWatermark = imagecreatefromjpeg('watermark_image2.jpg');
$watermarkWidth = imagesx($myWatermark);
$watermarkHeight = imagesy($myWatermark);
$image = imagecreatefromjpeg($sourceImage);
$imageSize = getimagesize($sourceImage);
$wmX = $imageSize[0] - $watermarkWidth - 20;
$wmY = $imageSize[1] - $watermarkHeight - 20;
imagecopy($image, $myWatermark, $wmX, $wmY, 0, 0, $watermarkWidth, $watermarkHeight);
/*
-------- If you want to save image -------
imagejpeg($image, 'watermark_image_save.jpg');
*/
imagejpeg($image);
imagedestroy($image);
imagedestroy($myWatermark);
Output:
I hope it can help you...
Hardik Savani
I'm a full-stack developer, entrepreneur and owner of ItSolutionstuff.com. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.
We are Recommending you
- PHP Curl Request with P12 Certificate Example
- How to Add Days to Date in PHP?
- How to Extract All Email Addresses from String in PHP?
- PHP Curl Delete Request Example Code
- PHP Curl Request with Certificate (cert pem file option) Example
- How to Upgrade PHP Version from 7.2 to 7.3 in Ubuntu?
- PHP - How to replace image src in a dynamic HTML string
- PHP Copy File from One Folder to Another Example
- PHP Find URL in Text (String) and Make Link Example
- PHP - Dropzone Add Download Button Example
- How to Partially Hide Email Address in PHP?
- How to Disable Submit Button After Form Submission in PHP?