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.
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.
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
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.
Edit: I was attempting to describe this problem using the markup I'm currently using, but that seems to be causing confusion. Look at the edit history if you'd like to see that should the question be unclear
I would like to write a php script generating an image from user submitted content.
The content is a block of text of undetermined length on a background. The text could be anything from a couple words to a few sentences.
There could be anywhere from 1 to 6 of these blocks of text that will vary in size. So, since the text varies in length, the container/background will vary in height. And since I could have multiple blocks, the overall image will vary in height.
Given that I need to dymically size so much of this (the image itself, the text containers, possibly font size), and I really don't even know where to start with the GD library, my first questions are:
Is this possible?
If possible, is it feasible to implement without a ton of headache?
Is there a cross-platform alternative (preferably no flash, and likely no <canvas>)?
There is a PHP function that gets the image size and returns it as an array.
$image_data = getimagesize($image_path);
returns an index array with 4 elements:
0: width in pixels
1: height in pixels
2: type as integer (1-GIF, 2-JPG, 3-PNG, 4-SWF, 5-PSD, etc)
3: the string " height='(height)' width='(width)'
You can use the $image_data[0] and $image_data[1] to return the proper width/height, which you can use to dynamically size the containers and whatnot.
The other issues you have are somewhat beyond the amount of time I can put into a post at the moment. Perhaps later I can expound upon those. Good luck.
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!