Add Watermark Image to Image Using PHP

In this tutorial, we will introduce the way to add a watermark image to another image using php.

1. Read watermark image and get width, height

<?php

header('Content-type: image/jpeg');

$watermark=imagecreatefrompng('watermark.png');
$watermark_height= imagesy($watermark);
$watermark_width=imagesx($watermark);
?>

2. Read source image

<?php
$source='image.jpg';
$image=imagecreatetruecolor($watermark_width, $watermark_height);
$image=imagecreatefromjpeg($source);
$image_size=getimagesize($source);
?>

3. Add watermark image to source image

<?php
$x=$image_size[0]-$watermark_width-10;
$y=$image_size[1]-$watermark_width-10;
imagecopymerge($image, $watermark, $x, $y, 0, 0, $watermark_width, $watermark_width, 100);
imagejpeg($image);
?>

where $x and $y will determine the position of watermark image in source image.