如何使用 PHP 中的 imaglayereffect() 函数设置 Alpha 混合标志以使用分层效果?

phpserver side programmingprogramming

imagelayereffect() 是 PHP 中的一个内置函数,用于设置 Alpha 混合标志以使用分层效果。成功时返回 True,失败时返回 False。

语法

bool imagelayereffect($image, $effect)

参数

imagelayereffect() 接受两个不同的参数:$image$effect

  • $image − 此参数由图像创建函数 imagecreatetruecolor() 返回。它用于创建图像的大小。

  • $effect − 此参数用于设置混合标志的值,使用不同的效果常量,如下所示 −

    • IMG_EFFECT_REPLACE − 它用于设置像素替换。它更类似于将 true 传递给 imagealphablending() 函数。

    • IMG_EFFETC_ALPHABLEND − 它用于设置正常的像素混合。这相当于将 false 传递给 imagealphablending() 函数。

    • IMG_EFFECT_NORMAL −它与 IMG_EFFETC_ALPHABLEND 相同。

    • IMG_EFFETC_OVERLAY − 使用 IMG_EFFECT_OVERLAY 时,白色背景像素将保持白色,黑色背景像素将保持黑色,但灰色背景像素将采用前景像素的颜色。

    • IMG_EFFETC_MULTIPLY − 这将设置乘法效果。

返回值

imagelayereffect() 成功时返回 True,失败时返回 False。

示例 1

<?php
   // 使用 imagecreatetruecolor() 函数设置图像
   $img = imagecreatetruecolor(700, 300);
   
   // 设置背景颜色
   imagefilledrectangle($img, 0, 0, 150, 150, imagecolorallocate($img, 122, 122, 122));

   // 应用叠加 Alpha 混合标志
   imagelayereffect($img, IMG_EFFECT_OVERLAY);

  // 绘制两个灰色椭圆
   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));

   // 输出图像
   header('Content-type: image/png');
   imagepng($img);
   imagedestroy($img);
?>

输出

示例 2

<?php
   // 使用 imagecreatetruecolor() 函数设置图像。
   $img = imagecreatetruecolor(700, 200);

   // 设置背景颜色
   imagefilledrectangle($img, 0, 0, 200, 200, imagecolorallocate($img, 122, 122, 122));

   // 应用叠加 Alpha 混合标志
   imagelayereffect($img, IMG_EFFECT_REPLACE);

   // 绘制两个灰色椭圆
   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));

   // 输出图像
   header('Content-type: image/png');
   imagepng($img);
   imagedestroy($img);
?>

输出


相关文章