Jump to content

Recommended Posts

Is it possible to take text from a POST that is being written to an image using the GD ImageTTFText function and breaking it apart so that it writes like a paragraph. For example if you have "Welcome to the happiest place on earth" typed into the input text box and subinted but the width of the image it is being written too only is wide enough to hold "Welcome to the happiest", is there a function that automatically breaks text in the image to a second line?? or do I have write my own function for that?

Link to comment
https://forums.phpfreaks.com/topic/66929-solved-imagettftext-paragraph-issue/
Share on other sites

like this

<?php
$text = "Welcome to the happiest place on earth";

$textArray = explode('|', wordwrap($text,25,'|'));

$y = $firstLinePos;
foreach ($textArray as $txt) 
{
    imagettftext(..... $txt);
    $y += $lineHeight;
}
?>

thank you, though, it seems that I have another problem now, but I"m not sure why.

 

I have some math in my code that aligns my text where I want it, centers it and then indents.

 

The more characters the text is, with the explode and wordwrap, the farther to the right the text shifts.

 

<?php
//$ln1[0] = text
//$ln1[2] = font size

$box = ImageTTFBBox($ln1[2],0,$font,$ln1[0]);

$d = $box[2] - $box[0];

$align = $width/2 + 30 - $d/2;

$textArray = explode('|', wordwrap($ln1[0],25,'|'));

$y = 40;
foreach ($textArray as $txt) 
{
    ImageTTFText($im,$ln1[2],0,$align,$y,$color,$font,$txt);
    $y += $ln1[2] + 3;
}

?>

k, I got it, heres the reslult...thank you all for your help I really appreciate it

 



<?php

$textArray = explode('|', wordwrap($ln1[0],25,'|'));

$y = 40;
foreach ($textArray as $txt) 
{
    
    $box = ImageTTFBBox($ln1[2],0,$font,$txt);

    $d = $box[2] - $box[0];

    $align = $width/2 + 30 - $d/2;

    ImageTTFText($im,$ln1[2],0,$align,$y,$color,$font,$txt);
    $y += $ln1[2] + 3;


}

?>

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.