Solved

php imagettftext new line

Posted on 2011-09-16
8
1,166 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
  • 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
 
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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 108

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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

758 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now