Simple Watermarking with PHP

Here’s a pretty code snippet for watermarking images with php.

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

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

$image = imagecreatetruecolor($watermark_width, $watermark_height);
$image = imagecreatefromjpeg('photo.jpg');
$size = getimagesize('photo.jpg');

$dest_x = $size[0] - $watermark_width - 5;
$dest_y = $size[1] - $watermark_height - 5;

imagecopymerge($image, $watermark, $dest_x, $dest_y, 0, 0, $watermark_width, $watermark_height, 100);

imagejpeg($image);
imagedestroy($image);
imagedestroy($watermark);
?>

Where photo.jpg is the image that you want to watermark. It uses gd image library which is now a standard php webhosting feature, so you should have any problems. You could learn more about working with images in php at the PHP GD Manual.

Oh, and by the way, the code highlighting plugin for wordpress is called WP-Syntax and is available at the wordpress plugins directory for free. It’s the only one I liked, out of a bunch of others. It supports over a hundred different coding languages and looks neat.

About the author

Konstantin Kovshenin

WordPress Core Contributor, ex-Automattician, public speaker and consultant, enjoying life in Moscow. I blog about tech, WordPress and DevOps.