Add Text on Image in PHP when Upload - Watermarking

Sovary May 27, 2022 661
1 minute read

Today I will show you how to insert watermark on your images. Sometime you don't want spend time to edit picture in Photoshop by just only place text on your copyright image.

You can do this by upload your image and do everything in PHP. Following simple steps below you will learn how to add watermark text to image in php.

Also Read

Summary Functions

The below are functions that we will use in this article.

Functions Description
imagecreatefrompng() Create object original image png file
imagecolorallocate() Set text color
imagettftext() Add text to image
imagepng() Save new image as png
getimagesize() Get info about width and height image
imagedestroy() Free clear memory associated with image

Add Text on Image

Example #1 - Using png file.

<?php
    $sourceImage = "original_photo.png";
    // New image name to be watermarked
    $desImage = "original_photo_watermarked.png";
    
    // Create image object from PNG file.
    $imgToBeWatermark= imagecreatefrompng($sourceImage);
    // Your path may different
    $font = "C:\Windows\Fonts\arial.ttf"; 
    $watermarkText = "cambotutorial.com png";
    $fontSize = 24;
    // Image to be watermarked quality (0 to 9)
    $quality = 6; 
    //get size height and width
    $size = getimagesize($sourceImage);
    // RGB color white 255,255,255
    $fontcolor = imagecolorallocate($imgToBeWatermark, 255, 255, 255);
    $posX = $size[0] - 350; // Watermark move to left 350
    $posY = $size[1] - 30; // Watermark move to top 30

    imagettftext($imgToBeWatermark, $fontSize, 0, $posX, $posY, $fontcolor, $font, $watermarkText);
    // Save new png image
    imagepng($imgToBeWatermark, $desImage, $quality);
    // Clear memory
    imagedestroy($imgToBeWatermark);
?>

Output #1

Example #2 - Using jpeg file.

<?php
    $sourceImage = "original_photo.jpg";
    // New image name to be watermarked
    $desImage = "original_photo_watermarked.jpg";
    
    // Create image object from PNG file.
    $imgToBeWatermark= imagecreatefromjpeg($sourceImage);
    // Your path may different
    $font = "C:\Windows\Fonts\arial.ttf"; 
    $watermarkText = "cambotutorial.com jpeg";
    $fontSize = 24;
    // Image to be watermarked quality (0 to 100)
    $quality = 60; 
    //get size height and width
    $size = getimagesize($sourceImage);
    // RGB color white 255,255,255
    $fontcolor = imagecolorallocate($imgToBeWatermark, 255, 255, 255); 
    $posX = $size[0] - 350; // Watermark move to left 350
    $posY = $size[1] - 30; // Watermark move to top 30

    imagettftext($imgToBeWatermark, $fontSize, 0, $posX, $posY, $fontcolor, $font, $watermarkText);
    // Save new png image
    imagejpeg($imgToBeWatermark, $desImage, $quality);
    // Clear memory
    imagedestroy($imgToBeWatermark);
?>

 Output #2

PHP 
Author

Founder of CamboTutorial.com, I am happy to share my knowledge related to programming that can help other people. I love write tutorial related to PHP, Laravel, Python, Java, Android Developement, all published post are make simple and easy to understand for beginner. Follow him