note 59578 deleted from function.imagecolorallocatealpha by pajoye

From: Date: Tue, 17 Jan 2006 15:09:25 +0000
Subject: note 59578 deleted from function.imagecolorallocatealpha by pajoye
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-101973@lists.php.net to get a copy of this message
Note Submitter: paamayim_nekudotayim at hotmail dot com ---- If you want to use alpha channels for ImageFill or ImageColorSet, you're going to be disappointed. It doesn't work. I've written a function to calculate what an alpha color would look like on another color, and then return that, so that you can then use this color to fake an alpha channel. <?php /* * ImageColorAllocatePseudoAlpha(Image &$im, * int $red, * int $green, * int $blue, * int $alpha, * mixed $color, * enum $process) * Written by Paamayim Nekudotayim * * Fakes an alpha channel by mixing the argb with the color. Process determines * if the color should be allocated or returned as an array. */ if (!defined("COLOR_ALLOCATE")) define("COLOR_ALLOCATE", 0); if (!defined("COLOR_RGB")) define("COLOR_RGB", 1); function ImageColorAllocatePseudoAlpha(&$im, $nr, $ng, $nb, $a, $c, $p = 0) { if ($nr < 0 || $nr > 255) trigger_error("ImageColorAllocateFakeAlpha() requires argument 2 to be between 0 and 255", E_WARNING); if ($ng < 0 || $ng > 255) trigger_error("ImageColorAllocateFakeAlpha() requires argument 3 to be between 0 and 255", E_WARNING); if ($nb < 0 || $nb > 255) trigger_error("ImageColorAllocateFakeAlpha() requires argument 4 to be between 0 and 255", E_WARNING); if ($a < 0 || $a > 127) trigger_error("ImageColorAllocateFakeAlpha() requires argument 5 to be between 0 and 127", E_WARNING); if (count($c) == 1 || count($c) == 2) { $rgb = ((count($c) == 1) ? $c : ImageColorAt($im, $c[0], $c[1])); $r = ($rgb >> 16) & 0xFF; $g = ($rgb >> 8) & 0xFF; $b = $rgb & 0xFF; } elseif (count($c) == 3) list($r, $g, $b) = $c; $cr = min(max(($r + (($nr - $r) * (1 - ($a / 127)))), 0), 255); $cg = min(max(($g + (($ng - $g) * (1 - ($a / 127)))), 0), 255); $cb = min(max(($b + (($nb - $b) * (1 - ($a / 127)))), 0), 255); return (($p == 0) ? ImageColorAllocate($im, $cr, $cg, $cb) : array("r" => $cr, "g" => $cg, "b" => $cb)); } ?> The last two arguments are kind of funny, and here's what they do and what you can give them. mixed $color ($c): This is the color you want to use as a base to apply the argb color to. You may give it a truecolor index, an array of X and Y co-ordinates or an array of red, green and blue colors. <?php ImageColorAllocatePseudoAlpha(,,,,, $defined_color,); ImageColorAllocatePseudoAlpha(,,,,, array($x, $y),); ImageColorAllocatePseudoAlpha(,,,,, array($r, $g, $b),); ?> enum $process ($p): This variable indicates how the function should return. If passed COLOR_ALLOCATE (0), it will allocate the color to the image, and return a truecolor index. This is useful for ImageFill(). If given COLOR_RGB (1), it will return an associative array of 'r', 'g', and 'b' which all contain the values of those colors. This is useful for ImageColorSet(). <?php ImageColorAllocatePseudoAlpha(,,,,,, COLOR_ALLOCATE); ImageColorAllocatePseudoAlpha(,,,,,, 0); // if COLOR_ALLOCATE is already defined ImageColorAllocatePseudoAlpha(,,,,,, COLOR_RGB); ?>

« previous php.notes (#101973) next »