note 19222 deleted from function.imagettftext by aidan
| From: | aidan@php.net | Date: | Mon, 20 Sep 2004 10:11:09 +0000 |
| Subject: | note 19222 deleted from function.imagettftext by aidan | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-77037@lists.php.net to get a copy of this message | ||
Note Submitter: mshambaugh#nucleusinc.com
----
Here's a hack to overcome the poor quality of the text rendered by this function on truecolor
images:
1) Create a temporary palette image (with ImageCreate rather than ImageCreateTrueColor)
2) Render your text into that image
3) Use ImageJPEG to write it to disk
4) Use ImageCreateFromJPEG to load that file back - presto, your text is now truecolor and still
looks great
5) Set a transparency color if need be and composite your text image onto your original truecolor
image
Like so:
// Create paletted image...
$buff = imagecreate($imagewidth, $imageheight);
$background_color = ImageColorAllocate($buff, 255, 255, 255);
$color = ImageColorAllocate($buff, 0, 0, 0);
// Render clean text into it...
ImageTTFText($buff, $fontsize, 0, $pos_x, $pos_y, $color, $font, $string);
// Write it out as a JPEG...
$filename = "/tmp/test_".date("U").".jpg";
ImageJPEG($buff, $filename, 100);
ImageDestroy($buff);
// Pull it back in from the JPEG...
$buff = ImageCreateFromJPEG($filename);
// Composite it onto the original image...
$background_color = ImageColorAllocate($buff, 255, 255, 255);
imagecolortransparent($buff, $background_color);
ImageAlphaBlending($image, true);
ImageCopy($image, $buff, 0, 0, 0, 0, ImageSX($image), ImageSY($image));
ImageAlphaBlending($image, false);
ImageDestroy($buff);