0

Well I'm trying to write a text in a image and I have this:

header("Content-type: image/png");
$font = 4;
$width = ImageFontWidth($font) * strlen($string);
$height = ImageFontHeight($font);

$im = @imagecreate($width,$height);
$background_color = imagecolorallocatealpha($im, 255, 255, 255, 127);
$text_color = imagecolorallocate($im, 0, 0,0); 
imagestring($im, $font, 0, 0, utf8_decode($string), $text_color);
imagepng($im);
imagedestroy($im);

And the string:

$string = "Hello, My name is Ikillnukes and I'm trying to do a newline with PHP.";

Well I want to do this with the output:

Normal output: Hello, My name is Ikillnukes and I'm trying to do a newline with PHP.

That I want:

Hello, My name is Ikillnukes and

I'm trying to do a newline with PHP.

How can I do this?

And one more thing... I want to add some bold text to the string how can I do this?

Seazoux
  • 621
  • 1
  • 5
  • 28

1 Answers1

1

First at all, imagestring does not do automatic word wrapping. You have to do it on your own.

That means that you need to know how long in pixels a specific string is. Here is imagettfbox your friend.

In the documentation for imagettfbox is a good and comprehensive example how to auto-wrap strings to match the image width: Example by a2hansolo in the PHP documentation.

For your problem with the bold writing, take a look at the documentation about imagettftext, especially the example by mirza_aqueel_qau.

zero0
  • 847
  • 11
  • 26