DEV Community

Cover image for How to add watermark to image using php gd
Onelinerhub
Onelinerhub

Posted on

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.

Top comments (0)