Jump to content

creating an image with multiple text lines / wordwrap using imagettftext


Recommended Posts

Hi there,

 

I am in the process of creaing my first image using the GD library and imagettftext.  I have got so far, but now hit a stumbling block.

 

The process is as follows...

 

User answers choices to create the text of a letter.

Letter variables added to image?get

Image called from page.

 

I have got it all working fine apart from getting text to start on newlines when it reaches the end of a section.

 

I have 2 issues -

 

1.  I don't get how to set a boundary for text that tells it to word wrap when it reaches a point on the page.

2.  I can't get the text to start a newline with either \n or <br> in the image - however, I can get it to start newlines if I look at the get text in the url. 

 

I have scaled down a copy of the image here so you can see what i am getting so far - http:www.webserverworld.co.uk/letter.jpg

 

I have looked around on the web and have seen some references to wordwrapping and tried to implement them but so far have had no luck.  If anyone can point me in the right direction on this, I would very much appreciate it.

 

The code I am using thus far is below.

<? 
// Set the content-type
header("Content-type: image/jpeg");

// Create the image
$im = imagecreatefromjpeg("santa_letter.jpg");

// Create some colors
$black = imagecolorallocate($im, 0, 0, 0);

// The text to draw
$greeting = $_GET['greeting'];
$p1 = $_GET['p1'];
$p2 = $_GET['p2'];
$p3 = $_GET['p3'];
$user = $_GET['user'];
$pass = $_GET['pass'];

// Replace path by your own font path
$font = dirname(__FILE__).'/FGWurth.ttf';

// Add the text
imagettftext($im, 40, 0, 200, 900, $black, $font, $greeting);
imagettftext($im, 40, 0, 200, 1100, $black, $font, $p1);
imagettftext($im, 40, 0, 200, 1300, $black, $font, $p2);
imagettftext($im, 40, 0, 200, 1500, $black, $font, $p3);
imagettftext($im, 40, 0, 200, 1800, $black, $font, $user);
imagettftext($im, 40, 0, 200, 1880, $black, $font, $pass);

// array imagettftext ( resource $image, float $size, float $angle, int $x, int $y, int $color, string $fontfile, string $text )

imagejpeg($im);
imagedestroy($im);
?> 

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.