How to add text to an image with PHP


In this example I am going to explain to you how to add text to a certificate image with GD with GD library.

<?php
/* FETCH IMAGE & WRITE TEXT */
$img = imagecreatefromjpeg('Certificate of Participation1_.jpg');
$color = imagecolorallocate($img, 255,0,0);
$name = "Sushanta Patra";
$course = " MCA";
$slno='OKCL-'.rand(100,99999);
$issue_date=date('d-m-Y');
$font = "arial.ttf";

$new_name="A.jpeg";

/* CENTER THE TEXT BLOCK - name */
imagettftext($img, 24, 0, 1080, 650, $color, $font, $name);
/* CENTER THE TEXT BLOCK - course */
imagettftext($img, 24, 0, 1080, 880, $color, $font, $course);
/* bottom Sl No THE TEXT BLOCK - course */
imagettftext($img, 24, 0, 780, 1380, $color, $font, $slno);
/* bottom Sl No THE TEXT BLOCK - course */
imagettftext($img, 24, 0, 1580, 1380, $color, $font, $issue_date);

/* OUTPUT IMAGE */
header('Content-type: image/jpeg');

/* create directory */
if (!file_exists('uploads/certificates/user')) {
mkdir('uploads/certificates/user', 0777, true);
$directory="uploads/certificates/user/";
}
/* image save */
imagejpeg($img, $directory.$new_name);

/* output image */
imagejpeg($img);

imagedestroy($jpg_image);

?>