imagealphablending() - gd函数(图像处理)
imagealphablending()
(PHP 4 >= 4.0.6, PHP 5, PHP 7)
设定图像的混色模式
说明
imagealphablending (resource $image,bool $blendmode) : boolimagealphablending() 允许在真彩色图像上使用两种不同的绘画模式。在混色(blending)模式下,alpha 通道色彩成分提供给所有的绘画函数,例如 imagesetpixel() 决定底层的颜色应在何种程度上被允许照射透过。作为结果,GD 自动将该点现有的颜色和画笔颜色混合,并将结果储存在图像中。结果的像素是不透明的。在非混色模式下,画笔颜色连同其 alpha 通道信息一起被拷贝,替换掉目标像素。混色模式在画调色板图像时不可用。如果$blendmode为 TRUE
,则启用混色模式,否则关闭。成功时返回 TRUE
, 或者在失败时返回 FALSE
。
Note: 此函数需要 GD 2.0.1 或更高版本(推荐 2.0.28 及更高版本)。
参数
$image由图象创建函数(例如imagecreatetruecolor())返回的图象资源。
$blendmode Whether to enable the blending mode or not. On true color images the default value is TRUE
otherwise the default value is FALSE
返回值
成功时返回 TRUE
, 或者在失败时返回 FALSE
。
范例
imagealphablending() usage example
注释
Note: 此函数需要 GD 2.0.1 或更高版本(推荐 2.0.28 及更高版本)。
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.)
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:
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.
I have create a little function for putting a watermark on any picture. Watermark can be png, with transparency, and the watermark can be placed anywhere on the image, using simple strings such as 'bottom-left', or 'center'.
"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:
I rewrote the code given below to skip calculations and pixel setting when not needed (full opaque or full transparent pixels), as the content of my overlays is generally mostly transparent. Reduced processing time from ~0.17s to ~0.06s on 216x145px images. function alphaOverlay($destImg, $overlayImg, $imgW, $imgH) { for($y=0;$y 24) > 16 & 0xFF; $ovrG = $ovrARGB >> 8 & 0xFF; $ovrB = $ovrARGB & 0xFF; $change = false; if($ovrA == 0) { $dstR = $ovrR; $dstG = $ovrG; $dstB = $ovrB; $change = true; } elseif($ovrA > 16 & 0xFF; $dstG = $dstARGB >> 8 & 0xFF; $dstB = $dstARGB & 0xFF; $dstR = (($ovrR * (0xFF-$ovrA)) >> 8) + (($dstR * $ovrA) >> 8); $dstG = (($ovrG * (0xFF-$ovrA)) >> 8) + (($dstG * $ovrA) >> 8); $dstB = (($ovrB * (0xFF-$ovrA)) >> 8) + (($dstB * $ovrA) >> 8); $change = true; } if($change) { $dstRGB = imagecolorallocatealpha($destImg, $dstR, $dstG, $dstB, 0); imagesetpixel($destImg, $x, $y, $dstRGB); } } } return $destImg; }
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.
Notice that AlphaBlending is ON by default. So, only use this function if you don't want to use AlphaBlending.
I have written a function that takes an image as parameter and returns the same image with a reflection effect (often seen in WEB 2.0 sites). I have not performance-tested this with large image files, for thumbnails it works fine (requires PHP 4.3.2 or above, or PHP5). This is rather hot-coded. You could go on and extract some of the values as parameters (80 is the transparency start value, $height / 2 is the reflection area...). Cheers, Christian
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.
Roy Conejo says: I' had to "per-pixel alpha blend" an image into a solid background, as seen on the very concise example from "barnabas at kendall dot NOSPAM dot net": an alpha blended .png logo on a .jpg photograph. The problem was... it doesn't worked out at all here (why? T_T). Note that I'm using just the source alpha to determine how much colour from source and destination will be present on the final pixel... and that I've to multiply the alpha value (0 - 127) by 2 because I need it to be 8 bits for the calculations. I think the code is pretty fast, no decimals, no rounding, no unnecesary coding. Bound checking or clipping could be implemented if you really need to. I hope it helps someone on my same situation ^.^