Question

I am currently trying to generate a infographic with the PHP GD library, I can get the text to display in the correct place but when it gets larger it overlaps parts of the image that I don't wan't it to. What I want to do is have some form of html DIV equivalent that I can place over all my text areas and are a fixed size so the font will scale down to fit.

I have tried using imagettfbox() but it just wraps the text I need a way of setting a fixed size and location as a text area that I can then pass a string into.

Any Questions Just Ask.

Was it helpful?

Solution

Their is a simple way round this that I came up with. If I pass in the length of the section where the image is going to be then check the length of the text created by GD compare if the text's width is equal to the section width it does this and if it isn't the font size is reduced by one and the ypos is reduced by 0.5 to ensure it is still centred.

Here is the code

while ($width > $settings->sectionWidth)
{
    $settings->fontSize -= 1;
    $bbox = imagettfbbox($settings->fontSize, 0, $font_path, $settings->data);
    $width = abs($bbox[4] - $bbox[0]);
    $settings->yPos -= 0.5;

}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top