Add Text on Image in PHP when Upload - Watermarking

Sovary May 27, 2022 697
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

As the founder and passionate educator behind this platform, I’m dedicated to sharing practical knowledge in programming to help you grow. Whether you’re a beginner exploring Machine Learning, PHP, Laravel, Python, Java, or Android Development, you’ll find tutorials here that are simple, accessible, and easy to understand. My mission is to make learning enjoyable and effective for everyone. Dive in, start learning, and don’t forget to follow along for more tips and insights!. Follow him