Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to set the alpha blending flag to use layering effects using imaglayereffect() function in PHP?
The imagelayereffect() function is an inbuilt PHP function used to set alpha blending flags for layering effects on images. It controls how pixels are blended when drawing on an image, returning true on success or false on failure.
Syntax
bool imagelayereffect($image, $effect)
Parameters
The function accepts two parameters ?
$image − An image resource created by image creation functions like
imagecreatetruecolor().-
$effect − The blending effect constant to apply. Available constants are ?
IMG_EFFECT_REPLACE − Sets pixel replacement mode (similar to
imagealphablending($image, true))IMG_EFFECT_ALPHABLEND − Sets normal pixel blending (equivalent to
imagealphablending($image, false))IMG_EFFECT_NORMAL − Same as
IMG_EFFECT_ALPHABLENDIMG_EFFECT_OVERLAY − White pixels remain white, black pixels remain black, grey pixels take foreground color
IMG_EFFECT_MULTIPLY − Applies multiply blending effect
Return Value
Returns true on success or false on failure.
Example − Using IMG_EFFECT_OVERLAY
This example demonstrates the overlay effect with colored ellipses ?
<?php
// Create an image
$img = imagecreatetruecolor(700, 300);
// Set grey background
imagefilledrectangle($img, 0, 0, 150, 150, imagecolorallocate($img, 122, 122, 122));
// Apply overlay blending effect
imagelayereffect($img, IMG_EFFECT_OVERLAY);
// Draw colored ellipses
imagefilledellipse($img, 50, 50, 40, 40, imagecolorallocate($img, 100, 255, 100));
imagefilledellipse($img, 50, 50, 50, 80, imagecolorallocate($img, 100, 100, 255));
imagefilledellipse($img, 50, 50, 80, 50, imagecolorallocate($img, 255, 0, 0));
// Output image
header('Content-type: image/png');
imagepng($img);
imagedestroy($img);
?>
Example − Using IMG_EFFECT_REPLACE
This example shows pixel replacement mode with concentric circles ?
<?php
// Create an image
$img = imagecreatetruecolor(700, 200);
// Set grey background
imagefilledrectangle($img, 0, 0, 200, 200, imagecolorallocate($img, 122, 122, 122));
// Apply replace blending effect
imagelayereffect($img, IMG_EFFECT_REPLACE);
// Draw concentric circles
imagefilledellipse($img, 100, 100, 160, 160, imagecolorallocate($img, 0, 0, 0));
imagefilledellipse($img, 100, 100, 140, 140, imagecolorallocate($img, 0, 0, 255));
imagefilledellipse($img, 100, 100, 100, 100, imagecolorallocate($img, 255, 0, 0));
// Output image
header('Content-type: image/png');
imagepng($img);
imagedestroy($img);
?>
Note: These examples require the GD extension and output images directly to the browser. To save images to files, use
imagepng($img, 'filename.png')instead.
Conclusion
The imagelayereffect() function provides powerful control over pixel blending in PHP image manipulation. Use IMG_EFFECT_OVERLAY for artistic effects and IMG_EFFECT_REPLACE for standard layering without transparency blending.
