imagealphablending

(PHP 4 >= 4.0.6, PHP 5)

imagealphablending -- Set the blending mode for an image

Description

bool imagealphablending ( resource image, bool blendmode )

imagealphablending() allows for two different modes of drawing on truecolor images. In blending mode, the alpha channel component of the color supplied to all drawing function, such as imagesetpixel() determines how much of the underlying color should be allowed to shine through. As a result, gd automatically blends the existing color at that point with the drawing color, and stores the result in the image. The resulting pixel is opaque. In non-blending mode, the drawing color is copied literally with its alpha channel information, replacing the destination pixel. Blending mode is not available when drawing on palette images. If blendmode is TRUE, then blending mode is enabled, otherwise disabled. Возвращает TRUE в случае успешного завершения или FALSE в случае возникновения ошибки.

Замечание: Эта функция нуждается в GD версии 2.0.1 или выше.



imagealphablending
ashita at profund dot hu
05-Oct-2005 02:41
For "webmaster at nweurosport dot com" :

Currently IE does not support 24 (+8 alpha) bit PNG-s (nor any kind of transparent images). Mozilla and Opera handle them correctly.
Jakub Argasiski
05-Jun-2005 11:53
I have been looking around for a while to find a script which does the following: generates image with text using specified font with given color, but with totally transparent background (by alpha-channnel, not via color transparency). Finally, I have created the script by myself. It's just a rough idea how to do it.

<?php
$tekst
= "This is a test message\nza gl ja!\nZAӣ GʦL JA?";

$h = 9;

$size = imageTTFBBox($h, 0, "arial.ttf", $tekst);
$image = imageCreateTrueColor(abs($size[2]) + abs($size[0]), abs($size[7]) + abs($size[1]));
imageSaveAlpha($image, true);
ImageAlphaBlending($image, false);

$tlo = imagecolorallocatealpha($image, 220, 220, 220, 127);
imagefill($image, 0, 0, $tlo);

$napis = imagecolorallocate($image, 220, 220, 220);
imagettftext($image, $h, 0, 0, abs($size[5]), $napis, "arial.ttf", $tekst);
imagepng($image, "output.png");
imagedestroy($image);

?>
<html>
<head>
</head>
<body bgcolor="#808080">
<img src="output.png" alt="">
</body>
</html>
webmaster at nweurosport dot com
17-Feb-2005 01:00
When saving images for use in transparent overlays like the logo addition mentioned above I've found that it is not succesful with PNG-24, only GIF and PNG-8.  I've had great success with PNG-8's.
dscharrer at gmail dot com
23-Dec-2004 02:32
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;
}
?>
joe AT cerberon DOT net
01-Mar-2004 05:02
Note that alpha blending must be enabled to render antialiased text in true color mode.

For OLDER versions of PHP and/or GD (e.g. which comes with Debian woody) alpha blending is DISABLED by default and it is ENABLED for NEWER versions.
www.deebster.com
17-Nov-2003 03:12
Your target image resource not must be paletted if you want to use blending.

This means using ImageCreateTrueColor() rather than ImageCreate().

(If your source is e.g. a jpeg and you've used ImageCreateFromJPEG(), the above is irrelevant.)
boo at php dot net
30-Jun-2003 02:18
Notice that AlphaBlending is ON by default.
So, only use this function if you don't want to use AlphaBlending.
tcarter at roundcorners dot com
13-Feb-2002 07:55
If you are saving an image as PNG with transparency then saving it as a PNG-8 will give it transparency in the same way GIF has, but that won't work with this function.

For this function to work the image needs to have an alpha channel (obviously really when you think about it), so make sure you save as PNG-24
barnabas at kendall dot NOSPAM dot net
11-Oct-2001 05:56
If you are trying to copy a transparant image on to another image, you might assume that you should apply the ImageAlphaBlending function to the image that has the transparancy, the source image. In reality, you must apply the ImageAlphaBlending function to the destination image. Basically it's saying, "make the specified image respect transparancy".

Here's a real world example. Suppose you want to put your logo on the upper left corner of a photograph. Your logo is a PNG with transparancy, and the photo is a JPEG. Here's what you would do:

<?php
$photoImage
= ImageCreateFromJPEG('photo.jpg');
ImageAlphaBlending($photoImage, true);

$logoImage = ImageCreateFromPNG('logo.png');
$logoW = ImageSX($logoImage);
$logoH = ImageSY($logoImage);

ImageCopy($photoImage, $logoImage, 0, 0, 0, 0, $logoW, $logoH);

ImageJPEG($photoImage); // output to browser

ImageDestroy($photoImage);
ImageDestroy($logoImage);
?>

<image2wbmpimageantialias>
 Last updated: Mon, 14 Nov 2005