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

Hàm imagecolortransparent() trong PHP

imagecolortransparent() đặt màu trong suốt (transparent) cho hình ảnh.

Cú pháp

imagecolortransparent(resource $image, int $color): int

Tham số

  • image: Resource GD (từ imagecreatetruecolor()).
  • color: Color identifier từ imagecolorallocate().

Trả về

Identifier của màu trong suốt mới. Trả về -1 nếu không chỉ định màu hoặc ảnh không có màu trong suốt.

Ví dụ: Tạo hình chữ nhật xanh trên nền trong suốt

<?php
$img = imagecreatetruecolor(500, 400);
$blue = imagecolorallocate($img, 0, 0, 255);
$transparent = imagecolorallocate($img, 0, 0, 0); // đen
imagecolortransparent($img, $transparent); // đặt đen là trong suốt
imagefilledrectangle($img, 80, 90, 400, 220, $blue);
header('Content-Type: image/png');
imagepng($img);
imagedestroy($img);
?>