Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

php imagettftext new line

Posted on 2011-09-16
8
Medium Priority
?
1,382 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 1000 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 111

Accepted Solution

by:
Ray Paseur earned 1000 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

610 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