Back to Top

How to write Multiline Long text on image in PHP

How to write Multiline Long text on image in PHP?

I recently had the case of a project where I was trying to display text on image and I have multiple long texts which I was fetching for third party API.You can not write text horizontally in one line especially on image as it is does not always fit into a given width of the Image so at that time you need to wrap the text to new line as per width of image.

I have googled the solution for this but not found any solution. Then I have posted my query on StackOverflow account but no one replied on my question so I understand that this is a very rare case or no one faced such situation before.

Finally after lots of research i got some basic solution from PHP Manual – imagettfbbox site.but before that i will explain you the use of imagettftext and imagettfbbox function in PHP.

Function Basic Understanding:

The main function which is needed to align the text:

imagettfbbox

This function Calculate the area occupied by the string of TrueType.This function is not used to display an image but simply determines whether the size and position would have taken a line of text.This returns an array of the coordinates of the rectangle which have the information about the size of the string.

SYNTAX:

imagettftext

This function output text of TrueType to the image

SYNTAX

This function puts the string text in the image. As usual, Parameter color must be a valid color identifier. Angle Parameter defines the angle in degrees of the output string which measured from the horizontal counter-clockwise. Coordinates (x, y) indicate the position of the text. Parameter size determines the font size to be used for text. Font file must be the name or path of the TTF-file in which the font.

The function returns a list of 8 elements. The first pair contain sets of coordinates (x, y) of the upper left corner of the rectangle restricting the text lines in the image and the second pair contain sets of coordinates (x, y) of the upper right corner.and others are the lower(x, y) coordinates.

PHP comes with GD library and it provides function imagettftext which is used to display text on an image, but this function has one critical drawback, You can not set the coordinates or width for multiline text.Therefore, You need to expand the function by own self to get desired output otherwise your long text overwrite and looks messy.

Once you understand the basic about imagettftext function in PHP, nothing prevents you from going further explore the possibilities of this function and you can use as you want to.

Let the code

Here is the working example to wrap the text with a given width on image:

In this function, First we explode the string by whitespace and then loop the exploded text and in loop,used condition which will prepare string for single line till the max width of image.and if coordinates are greater than width it will display the text and go for the next line.Here, we are using the imagettfbbox () function to calculate the width of the written text and draw a line below the text using the imageline ().

The result of the function is very basic for making the multi-line using imagettftext function will be up to trick otherwise the text will overwrite each other.The code is relatively simple here, I hope you will do the comments 🙂

Share:
Share on Facebook1

Share on LinkedIn1Tweet about this on TwitterShare on Google+0Buffer this pageDigg thisShare on Reddit0

Comments (1)

  1. Very information and relevant. Thanks for explaining.

Leave a Reply

Your email address will not be published. Required fields are marked *

Most Popular Posts

New Features in WordPress 3.5

Posted on 4 years ago

Bhumi

How to set DirectoryIndex using .htaccess?

Posted on 2 years ago

Bhumi

How To hide WordPress Toolbar

Posted on 4 months ago

Bhumi

How to convert RGB to Hex color PHP?

Posted on 3 years ago

Bhumi

How To view module Position in Joomla

Posted on 4 years ago

Bhumi