tags:

views:

50

answers:

5

Hello,

i create a transparent gif with text with the gd library but the output quality of the Text isn't good. Has anybody an idea how can i improve the quality?

Here is the code:

$req = explode('|', $_REQUEST['r']); $text = $req[0]; header ("Content-type: image/gif"); $font = getFont($req[2]); $font_size = $req[1]; $tmpcolor = getColor($req[3]);
$tmp_image=@imagecreatefromgif('gfx/transparent.gif'); $width = imagesx($tmp_image); $height = imagesy($tmp_image);

//calculate the new width / height
$tmp = imagettfbbox($font_size,0,$font,$text);
$new_width = $tmp[2]+10;
$new_height = $font_size+5;

$new_image = imagecreate($new_width,$new_height);
ImageCopyResized($new_image, $tmp_image,0,0,0,0, $new_width, $new_height, $width, $height);
$black = ImageColorAllocate($new_image,  0, 0,0);
$trans = ImageColortransparent($new_image,$black);
$color = ImageColorAllocate($new_image, trim($tmpcolor[0]), trim($tmpcolor[1]), trim($tmpcolor[2]));
imagettftext($new_image, $font_size, 0, 0, $font_size, $color, $font, $text);
//Grab new image
imagegif($new_image);
imagedestroy($new_image);
imagedestroy($tmp_image);

Here is the result: http://desmond.yfrog.com/Himg691/scaled.php?tn=0&server=691&filename=createphp.gif&xsize=640&ysize=640

Thank you

A: 

Try using imagecreatetruecolor instead of imagecreate.

Sjoerd
imagecreatetruecolor stops my output and it loose the textoutput. :(
apple
@apple it shouldn't do that. Turn error reporting on: `error_reporting(E_ALL);` to see what the problem is.
Pekka
+1  A: 

Other answerers point out that this could be a simple transparency issue rather than a TrueType rendering one. Try those suggestions first, as they may already remedy the problem at hand.

Sadly, GD's TrueType font rendering capabilities are not great.

  • Try the imageFTText() family of functions first. They rely on the external FreeType library which is better in quality, and also respects the kerning information in TrueType fonts (the individual distances between specific pairs of characters that make text look regular) better than the TTF functions.

  • If that doesn't work, use Imagemagick which in my experience is far superior to anything GD has to offer.

Pekka
i think imageFTText doesn change anything and i wouldn't like to use an external library like imagemagick.Thank you
apple
@apple yup, the problem at hand is a different one, see my comment in the other answer.
Pekka
+2  A: 

GIF format supports only 1-bit transparency (so pixel is either transpatent or opaque), so your text has jagged edges. To get smooth edges, use PNG format (which has 8-bit alpha channel, which means 256 levels of translucency), use GIF without transparency (i.e. on opaque background).

el.pescado
+1 I overread that he is creating a *transparent* GIF. That obviously can't work without jagged edges.
Pekka
i need a transparent background because i would set the Text over an other image.
apple
but how can i create an image with text and a transparent background in a good quality?
apple
@apple what you are trying to do is impossible using GIF. A transparent GIF can "fade" to one background colour value only. It will always look jagged on an image. You will need to use the PNG format, with the caveat that it'll need workarounds to work in IE6.
Pekka
@Pekkathanks, i changed into png but the result wasn't better as before.:(
apple
@apple have you switched to `imagecreatetruecolor` as suggested by Sjoerd?
Pekka
@Pekka, yes but the text was lost
apple
@apple it shouldn't be lost. It should work fine. Are you sure?
Pekka
@Pekka i think the text isn't lost but the transparency is lost and becomes a black background like the text. :(
apple
A: 

Pekka, now the background is black too but i want that the background is transparent. :(

$req = explode('|', $_REQUEST['r']);
$text = $req[0];
header ("Content-type: image/png");
$font = getFont($req[2]);
$font_size = $req[1];
$tmpcolor = getColor($req[3]);
$tmp_image=@imagecreatefrompng('gfx/transparent.png');
$width = imagesx($tmp_image);
$height = imagesy($tmp_image);

//calculate the new width / height
$tmp = imagettfbbox($font_size,0,$font,$text);
$new_width = $tmp[2]+10;
$new_height = $font_size+5;

$new_image = imagecreatetruecolor($new_width,$new_height);
ImageCopyResized($new_image, $tmp_image,0,0,0,0, $new_width, $new_height, $width, $height);
#$black = ImageColorAllocate($new_image,  0, 0,0);
#$trans = ImageColortransparent($new_image,$black);
$color = ImageColorAllocate($new_image, trim($tmpcolor[0]), trim($tmpcolor[1]), trim($tmpcolor[2]));
imagettftext($new_image, $font_size, 0, 0, $font_size, $color, $font, $text);
//Grab new image
imagepng($new_image);
imagedestroy($new_image);
imagedestroy($tmp_image);
apple
A: 

Has anybody an idea how these page generates the text with transparent gif background with a very good quality?

http://www.gutscheinvordrucke.de/kulinarisch/candlelight-dinner.html Thanks

apple