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

PHP – Sao chép bảng điều màu (palette) giữa ảnh với imagepalettecopy()

imagepalettecopy() là hàm PHP nội bộ dùng để sao chép bảng điều màu (palette) từ một ảnh sang ảnh khác. Hàm này sao chép palette từ ảnh nguồn (source) sang ảnh đích (destination).

Cú pháp

void imagepalettecopy(GdImage $destination, GdImage $source)

Tham số

imagepalettecopy() nhận hai tham số: $source và $destination.

  • $destination – Tài nguyên ảnh đích.

  • $source – Tài nguyên ảnh nguồn.

Giá trị trả về

imagepalettecopy() không trả về giá trị.

Ví dụ 1

<?php
// Tạo hai ảnh palette bằng imagecreate()
$palette1 = imagecreate(700, 300);
$palette2 = imagecreate(700, 300);
// Cấp phát màu xám cho ảnh palette 1
$gray = imagecolorallocate($palette1, 122, 122, 122);
// Sao chép palette từ ảnh 1 sang ảnh 2
imagepalettecopy($palette2, $palette1);
// Màu xám cấp phát cho ảnh 1 dùng cho ảnh 2 mà không cần imagecolorallocate() lần 2
imagefilledrectangle($palette2, 0, 0, 99, 99, $gray);
header('Content-type: image/png');
imagepng($palette2);
imagedestroy($palette1);
imagedestroy($palette2);
?>

Kết quả

PHP – Sao chép bảng điều màu (palette) giữa ảnh với imagepalettecopy()

Ví dụ 2

<?php
$palette1 = imagecreate(500, 200);
$palette2 = imagecreate(500, 200);
$gray = imagecolorallocate($palette1, 0, 255, 0);
imagefilledrectangle($palette1, 0, 0, 99, 99, $gray);
imagepalettecopy($palette2, $palette1);
$color1 = imagecolorstotal($palette1);
$color2 = imagecolorstotal($palette2);
echo "Colors in image 1 are " . $color1 . "<br>";
echo "Colors in image 2 is " . $color2;
?>

Kết quả

Colors in image 1 are 1
Colors in image 2 are 1