All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

Add Text To Image Using PHP

Last Updated : Jul 1, 2023

IN - PHP PHP GD | Written & Updated By - Pragati

In this tutorial we will show you how to add text to images using PHP GD library.The text can be any text like your name, web address, email or your number.You may also like add watermark to an image using PHP.

Add Text To Image Using PHP

To Add Text To Image It Takes Only One Step:-

  1. Make a PHP file and define scripting

Step 1. Make a PHP file and define scripting

We make a PHP file and save it with a name text_to_image.php

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

// Load And Create Image From Source
$our_image = imagecreatefromjpeg('images/image1.jpg');

// Allocate A Color For The Text Enter RGB Value
$white_color = imagecolorallocate($our_image, 255, 255, 255);

// Set Path to Font File
$font_path = 'font/larabiefont.TTF';

// Set Text to Be Printed On Image
$text ="Welcome To Talkerscode";

$size=20;
$angle=0;
$left=125;
$top=200;
	
// Print Text On Image
imagettftext($our_image, $size,$angle,$left,$top, $white_color, $font_path, $text);

// Send Image to Browser
imagejpeg($our_image);

// Clear Memory
imagedestroy($our_image);

?>

In this step we first set the image header to return image after processing.Then we load the image from directory and create that image using imagecreatefromjpeg() function then we allocate the color.

We use white color in text so we use RGB value of white you can use any color RGB value, then we specify font path it is very important to add font path because without using font your text will not be visible you can use any font for your text.

Then we set the text and use imagettftext() function to add text to images you can also customise the size, angle and position of the text as per your need.You may also like display text over image.

Thats all, this is how to add text on images using PHP and php gd library.You can customize this code further as per your requirement. And please feel free to give comments on this tutorial.

I hope this tutorial on text on image php helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Pragati

Experienced coding content writer who enjoys breaking down complex concepts in programming languages like Java, Python, C, and C++. Over three years of experience producing interesting and relevant content for a variety of entities. Committed to providing concise and easy-to-understand articles that assist readers in easily understanding technology and industry trends in the world of coding and software development.

Follow Pragati On Linkedin 🡪