Watermark an image in PHP

How to add a line of text as a watermark to any image (jpg/png/gif) using the built-in GD functions. The code given here should run on any version of PHP >= 4.0.

The code creates the watermarked image on the server, based on the path provided. Note that animated gifs do not remain animated once they have been modified in code. Be sure to give correct folder locations when calling the function and make sure the target folder has write permissions otherwise the images wont be written. You will also need to put the font file COURIER.TTF on the server where it is accessible to the code. You can use any TTF font file, just change the name of the font file in the code.

Example output of jpeg image

























Example output of png image




















Example output of gif image




















The code follows:

error_reporting(E_ALL ^ E_NOTICE ^ E_WARNING ^ E_DEPRECATION);

// Create a watermark on an image
// Amit Sengupta, Sep 2015

 * Add a line of text as watermark to an image file
 * @param string $sourceImage path to source image 
 * @param string $destImage path to desination image which will be created
 * @param string text which will be added to the image
 * @param string font location of font file
 * @return None
function watermark($sourceImage, $destImage, $text, $font) {
    //get image size
    $arrSize = getImageSize($sourceImage);

    // get type of image and then use the relevant function
    // only handling gif, jpg and png here.
    $imageType = exif_imagetype($sourceImage);
    $pSource = null;
    $pDest = null;
    if ($imageType == 1) { //GIF
	$pSource = imagecreatefromgif($sourceImage);
	$pDest = imagecreatefromgif($sourceImage);
    else if ($imageType == 2) { // JPEG
	$pSource = imagecreatefromjpeg($sourceImage);
	$pDest = imagecreatefromjpeg($sourceImage);
    else if ($imageType == 3) { // PNG
	$pSource = imagecreatefrompng($sourceImage);
	$pDest = imagecreatefrompng($sourceImage);
	exit("Unsupported image type");

    // copy source image to dest image object - not really reqd IMHO because the dest is same as source
    $retval = imagecopyresampled($pDest, $pSource, 0,0,0,0, $arrSize["width"], $arrSize["height"],
			$arrSize["width"], $arrSize["height"]);
    if (!$retval)
	exit("Imagecopyresampled failed");
    // superimpose text on image
    $blackcolor = imagecolorallocate($pDest, 0,0,0,0);
    $result = imagettftext($pDest, $fontSize, 0, 10, 20, $blackcolor, $font, $text);
    if (!$result)
	exit("imagettftext failed");

    // write destination file
    if ($imageType == 1) { //GIF
	imagegif($pDest, $destImage);
    else if ($imageType == 2) { // JPEG
	if (!imagejpeg($pDest, $destImage, 100))
	    exit("imagejpg failed for " . $destImage);
    else if ($imageType == 3) { // PNG
	if (!imagepng($pDest, $destImage))
	    exit("imagepng failed for " . $destImage);
    // destroy image resources

$source = $_SERVER['DOCUMENT_ROOT'] . "/dump/test.png";
$dest = $_SERVER["DOCUMENT_ROOT"] . "/dump/output.png";

if (!is_writable($_SERVER["DOCUMENT_ROOT"] . "/dump"))
    exit ("destination is not writable");

watermark($source, $dest, "Test Watermark", $font);

Be the first to comment

Leave a Reply

Your email address will not be published.