How to add watermark to image using php gd

nonunicorn

Onelinerhub

Posted on October 6, 2022

How to add watermark to image using php gd

Using PHP GD library to adding watermark is as simple as merging 2 images.

First, we create gd image from photo to add watermark to:

$bg = imagecreatefromjpeg('photo.jpg');
Enter fullscreen mode Exit fullscreen mode

Next, we create gd image from watermark file (logo.png) and get it's size:

$wm = imagecreatefrompng('logo.png');
$wm_size = getimagesize('logo.png');
Enter fullscreen mode Exit fullscreen mode

Now place watermark at 25 pixels from top & left borders:

imagecopy($bg, $wm, 25, 25, 0, 0, $wm_size[0], $wm_size[1]);
Enter fullscreen mode Exit fullscreen mode

Finally, save resulting JPG image to result.jpg:

imagejpeg($bg, 'result.jpg');
Enter fullscreen mode Exit fullscreen mode

Get and contribute more PHP GD code solutions.

💖 💪 🙅 🚩
nonunicorn
Onelinerhub

Posted on October 6, 2022

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related