note 48442 added to function.imagealphablending
| From: | dscharrer at gmail dot com | Date: | Thu, 23 Dec 2004 22:32:33 +0000 |
| Subject: | note 48442 added to function.imagealphablending | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-82217@lists.php.net to get a copy of this message | ||
If imagealphablending os set to true and you want to merge two images, you are left with no
transparency. If it is set to false, only the transparency of the second image is respected, causing
no parts of the first image to be shown. To solve this use the following function:
<?
//Merge multiple images and keep transparency
//$i is and array of the images to be merged:
// $i[1] will be overlayed over $i[0]
// $i[2] will be overlayed over that
// ...
//the function returns the resulting image ready for saving
function imagemergealpha($i) {
//create a new image
$s = imagecreatetruecolor(imagesx($i[0]),imagesy($i[1]));
//merge all images
imagealphablending($s,true);
$z = $i;
while($d = each($z)) {
imagecopy($s,$d[1],0,0,0,0,imagesx($d[1]),imagesy($d[1]));
}
//restore the transparency
imagealphablending($s,false);
$w = imagesx($s);
$h = imagesy($s);
for($x=0;$x<$w;$x++) {
for($y=0;$y<$h;$y++) {
$c = imagecolorat($s,$x,$y);
$c = imagecolorsforindex($s,$c);
$z = $i;
$t = 0;
while($d = each($z)) {
$ta = imagecolorat($d[1],$x,$y);
$ta = imagecolorsforindex($d[1],$ta);
$t += 127-$ta['alpha'];
}
$t = ($t > 127) ? 127 : $t;
$t = 127-$t;
$c =
imagecolorallocatealpha($s,$c['red'],$c['green'],$c['blue'],$t);
imagesetpixel($s,$x,$y,$c);
}
}
imagesavealpha($s,true);
return $s;
}
?>
----
Manual Page -- http://www.php.net/manual/en/function.imagealphablending.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+48442
Delete -- http://master.php.net/manage/user-notes.php?action=delete+48442&report=yes
Reason: bad code -- http://master.php.net/manage/user-notes.php?action=delete+48442&report=yes&reason=bad+code
Reason: spam -- http://master.php.net/manage/user-notes.php?action=delete+48442&report=yes&reason=spam
Reason: useless example -- http://master.php.net/manage/user-notes.php?action=delete+48442&report=yes&reason=useless+example
Reason: contains commercial links -- http://master.php.net/manage/user-notes.php?action=delete+48442&report=yes&reason=contains+commercial+links
Reason: useless note -- http://master.php.net/manage/user-notes.php?action=delete+48442&report=yes&reason=useless+note
Reject -- http://master.php.net/manage/user-notes.php?action=reject+48442&report=yes
Search -- http://master.php.net/manage/user-notes.php