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
Solved

php imagettftext new line

Posted on 2011-09-16
8
1,230 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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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 109

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

840 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