Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> PHP

PHP – Thiết lập hiệu ứng lớp với imagelayereffect()

imagelayereffect() là hàm nội bộ trong PHP dùng để thiết lập cờ alpha blending (trộn alpha) để sử dụng hiệu ứng lớp (layering effects). Trả về true khi thành công hoặc false khi thất bại.

Cú pháp

bool imagelayereffect(GdImage $image, int $effect)

Tham số

imagelayereffect() nhận hai tham số: $image và $effect.

  • $image – Trả về bởi hàm tạo ảnh imagecreatetruecolor(). Dùng để tạo kích thước ảnh.

  • $effect – Giá trị cờ trộn, dùng các hằng số hiệu ứng sau:

    • IMG_EFFECT_REPLACE – Thay thế pixel. Tương tự truyền true cho imagealphablending().

    • IMG_EFFECT_ALPHABLEND – Trộn pixel bình thường. Tương đương truyền false cho imagealphablending().

    • IMG_EFFECT_NORMAL – Giống IMG_EFFECT_ALPHABLEND.

    • IMG_EFFECT_OVERLAY – Pixel nền trắng giữ trắng, đen giữ đen, xám lấy màu pixel trước景.

    • IMG_EFFECT_MULTIPLY – Hiệu ứng nhân (multiply).

Giá trị trả về

imagelayereffect() trả về true khi thành công và false khi thất bại.

Ví dụ 1 – Hiệu ứng OVERLAY

<?php
$img = imagecreatetruecolor(700, 300);
imagefilledrectangle($img, 0, 0, 150, 150, imagecolorallocate($img, 122, 122, 122));
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);
?>

Kết quả

PHP – Thiết lập hiệu ứng lớp với imagelayereffect()

Ví dụ 2 – Hiệu ứng REPLACE

<?php
$img = imagecreatetruecolor(700, 200);
imagefilledrectangle($img, 0, 0, 200, 200, imagecolorallocate($img, 122, 122, 122));
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);
?>

Kết quả

PHP – Thiết lập hiệu ứng lớp với imagelayereffect()