In order to add text over an image, following PHP code can be used. Read the comments to understand purpose of code statements:
<?php
header ("Content-type: image/jpeg"); //setting the content type
//variable containing the string to be written on the image
$txt = "Here goes the text to be placed on the image";
$fontSize = 6; // sets size of the text as per need
/*below line sets pixel width of the section where the string would be placed. Imagefontwidth() returns the pixel width of a character by using the font size manually set by us in previous line. We would multiply it by total number of characters in the $txt variable to get the complete width of the string to be printed on image.*/
$widthX = imagefontwidth($fontSize) * strlen($txt) ;
//Similarly, this line sets pixel height of the section where the string would be placed.
$heightY = imagefontheight($fontSize) ;
// converts the argument file into a jpg image
$img = imagecreatefromjpeg("Image.jpg");
//setting X coordinates by subtracting the pixel width of the string from width of the image calculated using imagesx().
$xCoords = imagesx($img) - $widthX ;
//setting Y coordinates by subtracting the pixel height of the string from height of the image calculated using imagesy().
$yCoords = imagesy($img) - $heightY;
//setting the background-color of the string on image
$backgroundColor = imagecolorallocate ($img, 255, 255, 255);
$txtColor = imagecolorallocate ($img, 0, 0,0); //setting the text-color of the string on image
// rendering the string on the image using X and Y coordinate values and other specific details
imagestring ($img, $fontSize, $xCoords, $yCoords, $txt, $txtColor);
//creating the final jpg image along with text and sending to the browser
imagejpeg($img);
?>
This code works for all JPEG image files.
Webner Solutions is a Software Development company focused on developing CRM apps (Salesforce, Zoho), LMS Apps (Moodle/Totara), Websites and Mobile apps. If you need Web development or any other software development assistance please contact us at webdevelopment@webners.com
