note 48355 added to function.imagecopy
| From: | cod at crescentofdarkness dot cjb dot net | Date: | Mon, 20 Dec 2004 17:45:42 +0000 |
| Subject: | note 48355 added to function.imagecopy | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-82092@lists.php.net to get a copy of this message | ||
This function will put a truecolor png with transparency over a custom color backgorund.
The image will be gracefully blended with the background color using the alpha channel for each
color.
In real world we'd just mix foreground and backgorund colors looking at their percentages (i.e.
20% of background + 80% of foreground)
Here we have to calculate this for each r, g and b value of each color, and we have to use 127
instead of 100, because alpha channel goes from 0 to 127.
Try it on a color-to-transparent gradient!
<?php
function pngcolorizealpha($file, $color) {
/*
Function: pngcolorizealpha
Author: CoD (cod at crescentofdarkness dot cjb dot net)
Summary: Blends a truecolor png image with a coloured background using alpha channel
Input:
--------------------------
$file - string - path to the png image
$color- string - color in hex notation, without the #
Output:
--------------------------
a png image
*/
// first of all let's convert the background color
$background = array(
'red' => hexdec(substr($color,0,2)),
'green' => hexdec(substr($color,2,2)),
'blue' => hexdec(substr($color,4,2))
);
$im1 = imagecreatefrompng($file) or die('Cannot Initialize new GD image stream');
$im2 = imagecreatetruecolor(imagesx($im1), imagesy($im1));
$col1 = imagecolorallocate($im2, $background['red'], $background['green'],
$background['blue']);
imagefill($im2,0,0,$col1);
// for each color in the original png
for ($i=0; $i< imagecolorstotal($im1); $i++) {
// find r,g,b and alpha value
$foreground = imagecolorsforindex($im1, $i);
// blend fore and back colors using alpha value
$r = (($foreground['red'] / 127) * (127 - $foreground['alpha'])) +
(($background['red'] / 127)* $foreground['alpha']);
$g = (($foreground['green'] / 127) * (127 - $foreground['alpha'])) +
(($background['green'] / 127)* $foreground['alpha']);
$b = (($foreground['blue'] / 127) * (127 - $foreground['alpha'])) +
(($background['blue'] / 127)* $foreground['alpha']);
// allocate this new color in the destination image
imagecolorallocate($im2, $r,$g,$b);
}
imagecopy($im2, $im1, 0, 0, 0, 0, imagesx($im1), imagesy($im1));
header ("Content-type: image/png");
imagepng($im2);
imagedestroy($im1);
imagedestroy($im2);
}
?>
----
Manual Page -- http://www.php.net/manual/en/function.imagecopy.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+48355
Delete -- http://master.php.net/manage/user-notes.php?action=delete+48355&report=yes
Reason: bad code -- http://master.php.net/manage/user-notes.php?action=delete+48355&report=yes&reason=bad+code
Reason: spam -- http://master.php.net/manage/user-notes.php?action=delete+48355&report=yes&reason=spam
Reason: useless example -- http://master.php.net/manage/user-notes.php?action=delete+48355&report=yes&reason=useless+example
Reason: contains commercial links -- http://master.php.net/manage/user-notes.php?action=delete+48355&report=yes&reason=contains+commercial+links
Reason: useless note -- http://master.php.net/manage/user-notes.php?action=delete+48355&report=yes&reason=useless+note
Reject -- http://master.php.net/manage/user-notes.php?action=reject+48355&report=yes
Search -- http://master.php.net/manage/user-notes.php