Jump to content

[SOLVED] word wrapping an image string (imagettftext) bug


Recommended Posts

i am displaying an string on top an image...this is my original code ..the original code works...however,

 

  when i want to word wrap the string....the string still displays only one line of text...it doesn't go/break to the next line....

 

  ORIGINAL CODE:

 

          //displays string

 

          imagettftext($im, 18, 0, 10, 90, $white, $font, $description);

 

 

 

  REVISED CODE with WORDWRAPPING (can't find the bug)..tnx

     

      //displays string and defines its position

 

      imagettftext($im, 18, 0, 10, 90, $white, $font, $description);

      $text = $imagettftext;

 

      //wraps string???

 

        $newtext = $newtext = wordwrap($text, 20, "<br />\n");

 

      echo $newtext;  ## is this needed??

 

------------------------------

 

tnx!!! ..hoping for your immediate reply

You want to use

wordwrap($text, 20, "<br />\n");

 

what you are currently doing is adding a new line (\n) in the source but this won't

affect the browser display.

 

EDIT:

 

Code will look something like this

//$text = the text you want outputted by imagettftext()
$new_text = wordwrap($text, 20, "<br />\n");
imagettftext($im, 18, 0, 10, 90, $white, $font, $new_text);

 

 

 

 

this is now my revised code...but what happens no string is displayed once i type some text on the description field when i press the submit button.....what are the errors on my code? tnx a lot! i'm a newbie to php and i'm beginnign to love this site..

 

REVISED CODE

--------------------

$text = $_GET['description']; 
  $new_text = wordwrap($text, 20, "<br />\n");
  imagettftext($im, 18, 0, 10, 90, $white, $font, $new_text);

---------------------------

 

**should i still use the echo $new_text syntax?...coz i removed it from the code...tnx

Looking at the comments on this function reference at php.net it is pretty involved to get multi-line text working with imagettftext().

 

This is the easiest user posted solution

function imagettfmultilinetext($image, $size, $angle, $x, $y, $color, $fontfile,  $text, $spacing=1)
{
$lines=explode("\n",$text);
for($i=0; $i< count($lines); $i++)
    {
    $newY=$y+($i * $size * $spacing);
    imagettftext($image, $size, $angle, $x, $newY, $color, $fontfile,  $lines[$i], $spacing);
    }
return null;
}

if(isset($_GET['description'])){
  $text = $_GET['description'];
  $new_text = wordwrap($text, 20, '\n');
  imagettfmultilinetext($im, 18, 0, 10, 90, $white, $font,  $new_text);
}

 

I was wrong to suggest

wordwrap($text, 20, "<br />\n")

, I forgot this was overlaying a string on an image so HTML formatting doesn't matter.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.