note 69058 added to function.imagealphablending
| From: | lukedotstanleyatgmaildotcom at osu1 dot php dot net | Date: | Mon, 21 Aug 2006 20:30:37 +0000 |
| Subject: | note 69058 added to function.imagealphablending | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-116134@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:"
dscharrer at gmail dot com offered this without a use example, so here is one:
<?
$flag = imagecreatefrompng('a.png');
$mask = imagecreatefrompng('b.png');
imagealphablending($flag, 1);
imagealphablending($mask, 1);
$i= array($flag, $mask); // here is the array of images, using the above specified $flag and $mask
images
$s = imagemergealpha($i);
header("Content-type: image/png");
imagepng($s);
//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;
}
?>
----
Server IP: 83.138.144.80
Probable Submitter: 80.45.66.80
----
X-Spam-Status: No, hits=3.1 required=5.0 tests=DATE_MISSING,FROM_NO_LOWER
autolearn=no version=2.64
----
Manual Page -- http://www.php.net/manual/en/function.imagealphablending.php
Edit -- https://master.php.net/note/edit/69058
Del: integrated -- https://master.php.net/note/delete/69058/integrated
Del: useless -- https://master.php.net/note/delete/69058/useless
Del: bad code -- https://master.php.net/note/delete/69058/bad+code
Del: spam -- https://master.php.net/note/delete/69058/spam
Del: non-english -- https://master.php.net/note/delete/69058/non-english
Del: in docs -- https://master.php.net/note/delete/69058/in+docs
Del: other reasons-- https://master.php.net/note/delete/69058
Reject -- https://master.php.net/note/reject/69058
Search -- https://master.php.net/manage/user-notes.php