0
  • I am merging text in an image using GD in php.
  • 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 alphabets but had problem so i want the text to split in several lines remaining within the image.enter image description here

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.