- I am merging text in an image using
GDinphp. - I am taking the text from the user input so my problem is that when users input long text it overflows and is not merged in the image.
- you can check the below image i tried to merge
26 alphabetsbut had problem so i want the text to split in several lines remaining within the image.
1 Answer
One way would be to work out how many characters can fit per line. (19 characters may work for your example). Then add new line characters to your user supplied text ("\n") every 19 characters.
This isn't a perfect solution because some characters have different widths (except in the case of monotype fonts). You may need to experiment in order to find the best character limit per line.
Also you probably only have room for three lines (based on your example).
UPDATE:
This solution could be improved by calculating the bounding box of the inserted text using the gd function 'imagettfbbox'. This function returns the x/y coordinates for the bounding box of inserted text. This information could tell you how wide and how high the inserted text will be. Allowing you to adjust where you insert new line characters or even the font size.