Link to home
Start Free TrialLog in
Avatar of umaxim
umaximFlag for United States of America

asked on

php imagettftext new line

H I have a question how i can break text in few line with imagettftext.
My project is i have image with blank space. I need to put there some text llike paragraph. I need some how break it in few line and put it there so it look fine and it not come out the limit of border.
Avatar of EMB01
EMB01
Flag of United States of America image

I think you can only position it by XYZ coordinates.  So, I would just explode my input by newline and foreach newline put a new imagettftext with the proper coordinates.  I've never used it, so there may be an easier way, but here's the idea:

<?php
// define var
$var = <<<INPUT
this is the first line
this is the second line
INPUT;

// explode by newline
$lines = explode("\n", $var);

// define y coord
$y = 20;

// foreach newline, create text with new y coords
foreach ($lines as $line) {
    imagettftext($im, 20, 0, 10, $y, $black, $font, $text);

}
...  i accidentally submitted the page, i'm working on the complete code...
This should point you in the right direction.
// define var
$var = <<<INPUT
this is the first line
this is the second line
INPUT;

// explode by newline
$lines = explode("\n", $var);

// define y coord
$y = 20;

// foreach newline, create text with new y coords
foreach ($lines as $line) {
    imagettftext($im, 20, 0, 10, $y, $black, $font, $text);
    $y += 10;
}

Open in new window

Sorry for multiple posts, i forgot to add one thing:
// define var
$var = <<<INPUT
this is the first line
this is the second line
INPUT;

// explode by newline
$lines = explode("\n", $var);

// define y coord
$y = 20;

// foreach newline, create text with new y coords
foreach ($lines as $line) {
    imagettftext($im, 20, 0, 10, $y, $black, $font, $line);
    $y += 10;
}

Open in new window

Avatar of umaxim

ASKER

how i can limit number of letter for one line
SOLUTION
Avatar of EMB01
EMB01
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of umaxim

ASKER

thank you