note 21248 added to function.imagecolortransparent
| From: | marshall.AT.byterage.net@php.net | Date: | Sat, 04 May 2002 23:50:16 +0000 |
| Subject: | note 21248 added to function.imagecolortransparent | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-30199@lists.php.net to get a copy of this message | ||
In response to the note above...
ImageColorTransparent() does work on TrueColor images. I have it working myself but I do remember
having to add an extra step somewhere. Anyways, here is how I overlay text on a jpeg. Don't
forget, you need GD 2.x for true color to work. This example requires TTF support also.
//
// get src image and dimensions
$src_img = ImageCreateFromJPEG("foo.jpg");
$src_w = ImageSX($src_img);
$src_h = ImageSY($src_img);
//
// create empty true color canvas, def bg is black
$txt_img = ImageCreateTrueColor($src_w,$src_h);
//
// define colors
$white = ImageColorAllocate ($txt_img, 255, 255, 255);
$black = ImageColorAllocate ($txt_img, 0, 0, 0);
//
// add your white text
ImageTTFText ($txt_img, 80, 25, 65, 350, $white, "impact.ttf", "Kung Fu");
//
// make black bg transparent
ImageColorTransparent($txt_img, $black);
//
// merge text image onto src image
ImageCopyMerge($src_img,$txt_img,0,0,0,0,$src_w,$src_h,30); // 30% opacity
'Now' you can do resizing or whatever and output it. You need to create yet another blank
image to resample. (Actually you can resize the image before the text overlay step with some small
mods, but its REALLY messy if you want the output size to be dynamic)
//
// create empty true color canvas for resizing.
$dst_img = ImageCreateTrueColor(100,100);
//
// resize
ImageCopyResampled($dst_img,$src_img,0,0,0,0,100,100,$src_w,$src_h);
//
// output to browser.
ImageJPEG($dst_img,'',70); // quality 70
If someone could tell me a shorter way to do this, that would be sweet. The way it is now, it just
feels like there is one too many steps to get this done. But I am glad it works at all!
--
http://www.php.net/manual/en/function.imagecolortransparent.php
http://master.php.net/manage/user-notes.php?action=edit+21248
http://master.php.net/manage/user-notes.php?action=delete+21248
http://master.php.net/manage/user-notes.php?action=reject+21248