Adding Watermark On Image In PHP Example

  • 08-10-2022
  • 1526
  • PHP
  • Haresh Chauhan

Watch Youtube Video (HINDI) :

In his post, we will do how to add a watermark on the image using PHP. When you are working with the image application and want to add a watermark on the image then this post will help you to add it. it's simple to add a watermark logo on the image.

We will add the logo on the png image to take an example. Therefore we need two images, the first image is what you want to add as a watermark and the second thing is the main image on this image you want to add.

to get done watermark add to the image, we need to set the header content type for the image generated. then after we will give the width and height where you want to add the logo watermark.

Logo:

This is our simple logo.

image

Image:

This is our png image. On this image's bottom left I want to add above mentioned logo.

image

Code Example

You just create a simple PHP file. in this file paste all the given codes. You just need to give your image path and watermark image path add.

<?php
    
header('content-type: image/png');

$sourceImage = 'image.png'; // ADD IMAGE PATH
$myWatermark = imagecreatefrompng('logo.png'); // WATER MARK LOGO PATH

$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);

imagepng($image, time().'.png'); // SAVE IMAGE

imagepng($image); // SHOW IMAGE

imagedestroy($image);
imagedestroy($myWatermark);
Output With Watermark:

image


We always thanks to you for reading our blogs.


dharmesh-image

Dharmesh Chauhan

(Swapinfoway Founder)

Hello Sir, We are brothers origin from Gujarat India, Fullstack developers working together since 2016. We have lots of skills in web development in different technologies here I mention PHP, Laravel, Javascript, Vuejs, Ajax, API, Payment Gateway Integration, Database, HTML5, CSS3, and Server Administration. So you need our service Please Contact Us

haresh-image

Haresh Chauhan

(Co-Founder)


We Are Also Recommending You :