Create Watermark of image using PHP

0

Creating a watermark for normal individuals is not an easy task. For a professional designer, it is an easier task to do. Let’s create a system that will help you create an image with a watermark in just a minute of time using PHP. We decide to use PHP as it is one of the most popular programming language in the context of Nepal.

Doing it on the local machine requires xampp or wamp or lamp or mamp. The purpose of using one of these is they provide PHP and Apache server to work locally.

Step 1: Loading the images

$logo = imagecreatefrompng('logo.png');
$image = imagecreatefrompng('image.png');
here we are using the PHP function imagecreatefrompng, which is used to create a new image from the given URL. In our context, the two images are in the same directory of the index.php file.

Step 2: Getting the height and width along with the margins

$margin_right = 500;
$margin_bottom = 350;
$img_width = imagesx($logo);
$img_height = imagesy($logo);
here we are using another PHP function called imagesx and imagesy.  imagesx has been used to get the image width and imagesy to get the image height.

Step 3: Positioning the image and aligning the logo as per the calculation

imagecopy($image, $logo, imagesx($image ) - $img_width - $margin_right, imagesy($image) - $img_height - 
$margin_bottom, 0, 0, imagesx($logo), imagesy($logo));

here we are positioning the image that we want to use as a watermark over another.

Step 4: Output and freeing memory

header('Content-type: image/png');
imagepng($image);
imagedestroy($image);

here we are displaying the output and freeing the memory that has been held during the operation.

Results

Images that have been used:

watermark imagewatermark logo

Output Generated:

watermark output

Source Code

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More