Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Text wrapping #137

Open
kristos80 opened this issue Apr 28, 2016 · 10 comments
Open

Text wrapping #137

kristos80 opened this issue Apr 28, 2016 · 10 comments

Comments

@kristos80
Copy link

It would be a nice feature to make the text break in two lines if the available width is not enough for the text given and the font size or decrease automatically the font size in order to fit, but not under a specific limit (ex 8px) where it will break it in two lines.

Nevertheless is a really great library.

@claviska
Copy link
Owner

claviska commented Jan 6, 2017

This is a rather complex request, and GD doesn't have anything built-in for it as far as I can tell. I would entertain a proper PR, but only if it doesn't bloat the library.

Furthermore, I don't think this should be a feature of the text method directly, but probably a separate method that uses text internally to achieve the desired effect.

@claviska claviska closed this as completed Jan 6, 2017
@claviska claviska reopened this Jan 6, 2017
@claviska claviska changed the title Request: Line break in long text Text wrapping Jan 10, 2017
@sahinyusuf
Copy link

Is there any update or progress about this request?

@claviska
Copy link
Owner

I've learned a bit more about how bounding boxes are calculated by GD thanks to #164. It's definitely possible to determine if the text will overlap a defined width, but I'm not sure how well it will perform.

The idea is:

  • Accept a string of text.
  • Determine the left/right position of the bounding box and calculate width.
  • Is the start position + width > the defined width?
    • If not, draw the text.
    • If so, pop a word off the end of the string and try again.
  • Start a new line and repeat the process with remaining words until the string is empty.

Things to consider:

That said, I'm in the middle of another project at the moment and won't be able to tackle this right away. If anyone is interested in taking a stab at it, let me know!

@m-vdv
Copy link

m-vdv commented Sep 12, 2017

This would be a great feature to have! +1

@claviska claviska mentioned this issue May 29, 2020
@maPer77
Copy link
Contributor

maPer77 commented Nov 10, 2020

This can be useful, it is not the best solution but it can work very well in many cases.

$text  = '  Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce ac scelerisque mi, eget facilisis eros.'.PHP_EOL;
$text .= '  Vestibulum vitae libero elementum, dictum nisi in, porttitor arcu. Nunc viverra eu tortor vitae rutrum.'.PHP_EOL;
$text .= '  Nullam in vulputate lorem. Nunc gravida tristique posuere. Maecenas vel bibendum augue.'.PHP_EOL;
$text .= '  Nunc ultricies velit ut justo ultrices, eu aliquet dolor luctus.';
$text  = wordwrap($text, 30, PHP_EOL, true);
$image->text($text, ....................

font

font

@maPer77
Copy link
Contributor

maPer77 commented Nov 12, 2020

I am working on a function to do this.
With right, left, center alignment ...

@maPer77
Copy link
Contributor

maPer77 commented Nov 15, 2020

I'm doing a test, in the final stage.
The results were very good.

Justify
teste14_before

Line spacing changed
teste14_before (5)

Left
teste14_before (2)

Right
teste14_before (3)

Center
teste14_before (4)

@maPer77
Copy link
Contributor

maPer77 commented Nov 15, 2020

Manual line break is also supported.
This is the text of my example, where I put manual line breaks "PHP_EOL"

$text  = "    Morbi eu iaculis  arcu. Cras sit amet elementum arcu, a consectetur erat. Pellentesque enec quam interdum, eleifend eros non, dapibus nisl.";
$text .= PHP_EOL;
$text .= "    Mauris mattis metus at lacus eleifend tempor in feugiat magna. Vivamus id condimentum justo, fermentum efficitur mi. ";
$text .= PHP_EOL;
$text .= "    Praesent volutpat libero at lacus aliquet, interdum lobortis ante lobortis.";

@maPer77
Copy link
Contributor

maPer77 commented Nov 15, 2020

Font Scriptina
teste14_before

Font Queenstown Signature
teste14_before

Font Kaushan Script
teste14_before

@claviska
Copy link
Owner

Fantastic work! This looks amazing. 😍

I'll review the source tomorrow or the next day. Apologies for the delay!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants