Solved

php imagettftext new line

Posted on 2011-09-16
8
1,247 Views
Last Modified: 2012-05-12
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.
0
Comment
Question by:umaxim
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 2
8 Comments
 
LVL 14

Expert Comment

by:EMB01
ID: 36550999
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);

}
0
 
LVL 14

Expert Comment

by:EMB01
ID: 36551006
...  i accidentally submitted the page, i'm working on the complete code...
0
 
LVL 14

Expert Comment

by:EMB01
ID: 36551022
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

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 14

Expert Comment

by:EMB01
ID: 36551031
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

0
 
LVL 1

Author Comment

by:umaxim
ID: 36551220
how i can limit number of letter for one line
0
 
LVL 14

Assisted Solution

by:EMB01
EMB01 earned 250 total points
ID: 36551368
The best way to do that would probably be to do something like this:

$string = "this is my very long string this is my very long string this is my very long string this is my very long string";

// ensure length is only 20 characters...
if (strlen($string) > 20) {
    $string = substr($string, 0, -(strlen($string) - 20));
}

echo $string;

Open in new window

0
 
LVL 110

Accepted Solution

by:
Ray Paseur earned 250 total points
ID: 36552018
how i can limit number of letter for one line

The same general technology that is used for a publisher's "teaser fragment" can be used here.  Note the function wordwrap().  It will not break the words.
<?php // RAY_teaser_fragment.php
error_reporting(E_ALL);


// CREATE A TEASER FRAGMENT HEADLINE
// RETURN FIRST FEW WHOLE WORDS FOLLOWED BY ELLIPSES
// WITH A LINK TO THE FULL ARTICLE
// $length IS MINIMUM TRUNCATION CHARACTER COUNT


function teaser_fragment($text, $length=32, $url='/', $delim='|||')
{
    // IF TRUNCATION IS NEEDED
    if (strlen($text) > $length)
    {
        // IF TRUNCATION IS NEEDED, BREAK STRING APART
        $t = wordwrap($text, $length, $delim);
        $a = explode($delim, $t);
        $z = '...';
    }
    // IF TRUNCATION IS NOT NEEDED
    else
    {
        $a[0] = $text;
        $z = NULL;
    }

    // CONSTRUCT THE FRAGMENT WITH THE LINK AND ADD ELLIPSIS (LINK) TO THE END
    $teaser
    = '<a target="_blank" href="'
    . $url
    . '">'
    . $a[0]
    . $z
    . '</a>'
    ;
    return $teaser;
}



// USE CASES
echo "<pre>";
echo PHP_EOL;
echo "1...5...10...15...20...25...30...35...40...45..." . PHP_EOL;
echo teaser_fragment('Now is the time for all good men to come to the aid of their party');

echo PHP_EOL;
echo teaser_fragment('Now is the time for all good men to come to the aid of their party', 300);

echo PHP_EOL;
echo teaser_fragment('Now is the time for all good men to come to the aid of their party', 15, 'http://en.wikipedia.org/wiki/Filler_text');

Open in new window

0
 
LVL 1

Author Closing Comment

by:umaxim
ID: 36926196
thank you
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question