My website has a picture generator. It allows visitors to write a custom text on an image.
I handle the situation by displaying the picture and using Javascript, so they can write on the picture (to preview how the picture will look like). After they press submit I get x&y coordinates, text and font size.
(I also support multiply font sizes)
For writing the text on the image I use the ImageTTFText function.
Ok, so far everything was good. Now the problem I have is how to know when the sentence is too long to fit in one line. I came across the wordwrap function, but it's not reliable. It splits the sentence depending on the number of chars. But, for example, if you type 'I' ten times and 'D' ten times you will see there is a difference in width.
Ok, then I came across ImageTTFBBox which will calculate the size of the box so I will know when it's too long. Well this is fine but how could I split the sentence then? (by words).
I would be very grateful if anyone could give me an answer.
my logic would be to add as many words to each line as possible without having to cut a word. So, I would add a word to a line, check its rendered size with ImageTTFBBox(), and repeat until the line overflows(and then obvious remove the word that caused the overflow). If you come across the situation where the line overflows and there's only one word on the line, then you must cut the word. you will be calling ImageTTFBBox() many many times.
If you are using HTML5, you could ask Javascript to give you some help, before sending over. Assuming you have a canvas (even created in code) and a context ctxt you could do:
ctxt.font = "14pt Arial";
ctxt.textAlign = "left";
ctxt.fillStyle = "#000";
ctxt.fillText(text, x, y);
var metrics = ctxt.measureText(text);
var width = metrics.width;
Now you know how wide your text is, and can do some basic word wrapping. Say your effective width (image width minus inside margin) is 180 px, and your text width is 679 px. That's a +/- 25% ratio (26.05%). Use that ratio to extract the first 25% of the sentence, see if it fits in the width, draw, and move down a line.
Related
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.
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.
It sounds harder than it really is, and I can program this, though I'm searching for shortcuts.
I have a transparent PNG, with the dimensions of 250x525 pixels. Inside is a character that can be customized by my users, thus the dimensions of the real content inside that 250x525 image can vary. I want to reduce the 250x525 image down to just the avatar, by detecting where the first pixel begins vertically and horizontally, and where the last pixel ends.
Is there a shortcut for this? A function, or another way to do it perhaps? An example of an image I'd like to apply this to: http://i.troll.ws/92f2db20.png - imagine a box around the character. I want to calculate an invisible box around it, and pull the character out of it into its own image resource in GD.
The Reason: I have a function that can take an image and scale it to fit a larger image. I want to apply this to these characters, and first need to dumb it down to just the character.
Not GD, but Imagemagick/Imagick is able to do that vith the trimImage method.
$im = new Imagick("http://i.troll.ws/92f2db20.png");
$im->trimImage(0);
$im->writeImage('image.png');
I'm looking for a php class that can help me add text to an image.
I would like to define an x and y point for the upper left corner, and then set a width (maybe height) and then get the text in there so that it automatically continues on the next line. I've been searching but I can't figure out how to do this....
You can achieve this using the GD library really easily. A tutorial is available right here.
I've done something similar a few years back with using PHP and GD to create header graphics. It basically breaks the string of text into words, calculates the width of each word, and then adds each word one by one. If a word takes it over the width limit, it pushes that to the next line. With a bit of tweaking it should do the trick for you: http://www.ashleysheridan.co.uk/coding/php/PHP+Image+Header
I want to pull a dynamic content, which consists of a long text input with some images, into a div with a fixed width (300px) and height (1000px), the challenge is I cannot use overflow: auto in css when the content's length is exceeding the div's height (1000px), instead, I am asked to split the long content into pages with a pagination.
Is it possible to achieve with PHP or do I have to use javascript (jquery)?
I was thinking to count the number of characters and splitting them, but it doesn't seem correct when the content comes with different sizes of images...
Any ideas??
This might be very complicated(I'd like to say "impossible") to do it on the serverside, because there are too many clientside effects that can't be calculated(browser-default-settings for margins, paddings, line-height, font-size and user-setting for zooming), I would prefer to do this on clientside.
I made a little example using jQuery: http://jsfiddle.net/doktormolle/XwUuA/
It takes the childnodes of the target-element, and wraps them into new elements which have the same dimensions like the target-element(as long as the height of the wrapper does'nt exceed the height of the target-element).
Maybe it's useful to you(It's a draft, of course there still has to be worked on it to match your needs)
You could use PHP. Find out how many characters you can get per line, and how many lines of characters will fit in your div. Then, with PHP, count characters, divide by characters/line, then you'll have how many lines your text will take up. Then you can use getimagesize() to get an images dimensions, and go from there.
See the PHP function for more info.
I wanted to do something similar with HTML but in a C# Windows Forms application.
What I wanted to do was to generate some contents based on some database tables and send them out to the printer. The contents had to fit into A4 papers.
After lots of trial and error I measured the maximum size of the contents based on their size, place etc. and wrote the numbers in the CSS portion of my HTML.
With that I could get a nice result. Still some slight errors on some inputs, but that worked for me!
so I'm using FPDF in PHP to programmatically generate a PDF file from images and text (not HTML).
One of the biggest issues I've been having is being able to wordwrap text around an image. My current algorithm looks like:
Get text as an array of words
Add words one at a time to a 'current line' variable, and call GetStringWidth() on it to determine the width of the current line
Once I reach a pre-determined max width, I pass off the current line to an array of lines, and start on a new 'current line'.
Doing this allows me to get an array of lines that shouldn't be breaking improperly, however I've discovered that because my text is justified-aligned, GetStringWidth() can't accurately give me the width of the line when it has been justified.
I've dug into FPDF's MultiCell method to try and figure out how it breaks justified text properly but can't really make heads nor tails of it. It seems to boil down to a similar algorithim (and it writes each line using Cell ) but it never actually seems to calculate the width, it writes out PDF "code" such as 0.375 Tw.
Does anyone know how to calculate the width of justified text, given a string and a max width?
Answering a really old Question.
In the end, I took the function that does justification out of the library and rewrote it to allow for another parameter that makes it continue justifying text even on the last line.
This would result in text that was fully justified for a specific paragraph, then you write the final paragraph without the parameter and have the final line not justified (as per normal).
not sure if this helps but I have a related problem - needing to know how many lines a MultiCell will take up.
I did this by using GetStringWidth() / $maxWidth and getting the ceil() of that.
I can then work out the estimated height (as I know the line height I am using) and use that figure (in my case to switch columns or not).
Perhaps feeding the base text into GetStringWidth() and estimating height this way will allow you to determine an appropriate place to break the text into the multiple MultiCell()s.