Table of Contents
Adding watermark text or brand logo on images is a very interesting topic. Inside this article we will see the concept i.e How to add logo watermark to image in php. This concept you can use when you want to add your brand value to your copyright content. For example – Adding company name to any of your images, Adding company logo, etc.
This article will be step by step. Very super easy to learn and implement it in your project. PHP provides several functions by which we can work with image manipulation task. Functions are imagecreatefrompng(), imagesx(), imagesy(), imagecreatefromjpeg(), getimagesize(), imagecopy(), imagepng(), imagedestroy().
Before working with images related tasks, you should have these extensions enabled.
- GD Extension
- Imagick Extension
Learn More –
- How To Add Watermark Text To Image in PHP Tutorial
- Integration of Google reCaptcha v2 in PHP Tutorial
- jQuery Ajax File Upload with Form Data using PHP
- PHP Delete Files & Folders Using FTP From Remote Server
Let’s get started.
Create PHP Project
Go to your localhost directory.
Create a folder named as watermark-img. Create files inside it. Files are –
- index.php -> Create this file. This will generate the image with watermark text on it.
- company-logo.png -> Logo file which will be used as a brand as a watermark into other images.
- watermark-php-text.png -> Put any image of your own, an Image file where we add watermark text.
Required PHP Extensions
While working with image related stuff in PHP, make sure your PHP version should have these two most common image processing libraries GD Library and Imagick extensions enabled.
Whether your system already contains these extensions or not you can verify like this –
- Create info.php file at your localhost directory
- <?php phpinfo(); ?> Add this code into info.php file.
- Run this file into browser
You should see these information into php information page.
GD Extension Enabled
Imagick Extension Enabled
Open index.php file and write this code into it.
Use “.png” Image
<?php header('content-type: image/png'); $sourceImage = 'watermark-php.png'; // Add watermark into this $myWatermark = imagecreatefrompng('company-logo.png'); // brand logo $watermarkWidth = imagesx($myWatermark); $watermarkHeight = imagesy($myWatermark); $image = imagecreatefrompng($sourceImage); $imageSize = getimagesize($sourceImage); $wmX = $imageSize - $watermarkWidth - 20; $wmY = $imageSize - $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);
Use “.jpg” or “.jpeg” Image
In case if you have other image extensions like of .jpeg or .jpg then use this code for it.
<?php header('content-type: image/jpeg'); $sourceImage = 'watermark-php.png'; // brand logo $myWatermark = imagecreatefromjpeg('company-logo.png'); // Add watermark into this $watermarkWidth = imagesx($myWatermark); $watermarkHeight = imagesy($myWatermark); $image = imagecreatefromjpeg($sourceImage); $imageSize = getimagesize($sourceImage); $wmX = $imageSize - $watermarkWidth - 20; $wmY = $imageSize - $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);
Open application into browser.
It will execute index.php and opens application as
We hope this article helped you to learn How To Add Logo Watermark To Image in PHP Tutorial in a very detailed way.
Online Web Tutor invites you to try Skillshare free for 1 month! Learn CakePHP 4, Laravel APIs Development, CodeIgniter 4, Node Js, etc into a depth level. Master the Coding Skills to Become an Expert in Web Development. So, Search your favourite course and enroll now. Click here to join.