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

Hàm imagecolorallocatealpha() trong PHP

imagecolorallocatealpha() cấp phát màu có độ trong suốt (alpha) cho ảnh GD.

Cú pháp

imagecolorallocatealpha(resource $image, int $red, int $green, int $blue, int $alpha): int|false

Tham số

  • alpha: 0 (hoàn toàn đặc) đến 127 (hoàn toàn trong suốt).

Ví dụ: Vẽ các elip chồng nhau với độ trong suốt khác nhau

<?php
$img = imagecreatetruecolor(520, 350);
$bg = imagecolorallocate($img, 50, 10, 255);
imagefill($img, 0, 0, $bg);
$c1 = imagecolorallocatealpha($img, 50, 255, 0, 70);
$c2 = imagecolorallocatealpha($img, 255, 0, 255, 50);
$c3 = imagecolorallocatealpha($img, 150, 255, 0, 60);
$c4 = imagecolorallocatealpha($img, 200, 0, 255, 90);
imagefilledellipse($img, 200, 150, 150, 150, $c1);
imagefilledellipse($img, 220, 150, 150, 150, $c2);
imagefilledellipse($img, 240, 150, 150, 150, $c3);
imagefilledellipse($img, 280, 150, 150, 150, $c4);
header('Content-Type: image/png');
imagepng($img);
imagedestroy($img);
?>