0

I got good result in PHP/GD to write text to image template, text wraps fine but is not "smooth", here is the code I am using:

<?php

header("Content-type: image/png");

$text = "go to school go to school go to school go to school go to school go to school go to school go to school go to school go to school go to school ";
$arrText=explode("\n",wordwrap($text,60,"\n"));

$im = imagecreatefrompng("template.png");
$y = 15; //vertical position of text
foreach($arrText as $arr)
{
    $white = imagecolorallocate($im,0,0,0); //sets text color
    imagestring($im,5,15,$y,trim($arr),$white); //create the text string for image,added     trim() to remove unwanted chars
    $y = $y+20;

}
imagepng($im);
imagedestroy($im);
?>
Stephen Kennedy
  • 20,585
  • 22
  • 95
  • 108
Basheer
  • 328
  • 1
  • 2
  • 10
  • 1
    Any chance you can add an image of the current result? – FThompson Aug 07 '12 at 17:26
  • Define smooth. When "wordwrap" is called it makes a hard break at 60, or `n`, characters. – Robert K Aug 07 '12 at 17:32
  • What is the question? Are you asking how to make the text "smooth"? – davidethell Aug 07 '12 at 17:32
  • @Vulcan, I am not able to post pictures coz I am new user. – Basheer Aug 07 '12 at 17:38
  • @davidethell yes I mean antialias. – Basheer Aug 07 '12 at 17:38
  • @BasheerHallak I believe you can post a link, but I think we all understand now that it's antialiasing that you want. Please add that to the question. – FThompson Aug 07 '12 at 17:58
  • Basheer, I got your message, but your email didn't come through. I would invite you to a private StackOverflow chat to discuss, but you don't have enough reputation to join chat yet. Try contacting me again through my site and include your email in the body of the message. – davidethell Aug 08 '12 at 10:22

2 Answers2

2

Please try this:-

Remember : set correct path for font $font = 'arial.ttf';

    header('Content-type: image/png');

    // Create the image
    $im = imagecreatetruecolor(400, 30);

    // Create some colors
    $white = imagecolorallocate($im, 255, 255, 255);
    $grey = imagecolorallocate($im, 128, 128, 128);
    $black = imagecolorallocate($im, 0, 0, 0);
    imagefilledrectangle($im, 0, 0, 399, 29, $white);

    // The text to draw
    $text = 'go to school go to school go to school go to school go to school go to school go to school go to school go to school go to school go to school';

   // Replace path by your own font path
    $font = 'arial.ttf';

    // Add some shadow to the text
    imagettftext($im, 20, 0, 11, 21, $grey, $font, $text);

    // Add the text
    imagettftext($im, 20, 0, 10, 20, $black, $font, $text);

    // Using imagepng() results in clearer text compared with imagejpeg()
    imagepng($im);
    imagedestroy($im);
Abid Hussain
  • 7,724
  • 3
  • 35
  • 53
0

Try turning on anti-aliasing:

imageantialias($im, true);

Does that make it look any better?

Or else try imagettftext with a truetype font:

imagettftext ( $im, 15, 0, 15, $y, $white, 'fontfile.ttf', trim($arr));
davidethell
  • 11,708
  • 6
  • 43
  • 63