How to set the alpha blending flag to use layering effects using imaglayereffect() function in PHP?


imagelayereffect() is an inbuilt function in PHP that is used to set the alpha blending flag to use layering effects. It returns True on success or False on failure.

Syntax

bool imagelayereffect($image, $effect)

Parameters

imagelayereffect() takes two different parameters: $image and $effect.

  • $image − This parameter is returned by the image creation function imagecreatetruecolor(). It is used to create the size of an image.

  • $effect − This parameter is used to set the value of blending flag, using different effect constants, which are given below −

    • IMG_EFFECT_REPLACE − It is used to set the pixel replacement. It is more similar to passing true to imagealphablending() function.

    • IMG_EFFETC_ALPHABLEND − It is used to set the normal pixel blending. This is equivalent to passing false to the imagealphablending() function.

    • IMG_EFFECT_NORMAL − It is the same as IMG_EFFETC_ALPHABLEND.

    • IMG_EFFETC_OVERLAY − By using IMG_EFFECT_OVERLAY the white color background pixels will remain white and the black color background pixels will remain black but the grey background pixels will take the color of the foreground pixel.

    • IMG_EFFETC_MULTIPLY − This will set the multiply effect.

Return Values

imagelayereffect() returns True on success and False on failure.

Example 1

<?php
   // Setup an image using imagecreatetruecolor() function
   $img = imagecreatetruecolor(700, 300);
   
   // Set a background color
   imagefilledrectangle($img, 0, 0, 150, 150, imagecolorallocate($img, 122, 122, 122));

   // Apply the overlay alpha blending flag
   imagelayereffect($img, IMG_EFFECT_OVERLAY);

   // Draw two grey 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);
?>

Output

Example 2

<?php
   // Setup an image using imagecreatetruecolor() function.
   $img = imagecreatetruecolor(700, 200);

   // Set a background color
   imagefilledrectangle($img, 0, 0, 200, 200, imagecolorallocate($img, 122, 122, 122));

   // Apply the overlay alpha blending flag
   imagelayereffect($img, IMG_EFFECT_REPLACE);

   // Draw two grey ellipses
   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);
?>

Output

Updated on: 09-Aug-2021

81 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements