Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> PHP

PHP – คัดลอก Palette ระหว่างรูปภาพด้วย imagepalettecopy()

ฟังก์ชัน imagepalettecopy() ใช้คัดลอก color palette จากรูปภาพหนึ่งไปยังอีกรูปหนึ่ง คัดลอกจาก source ไป destination

รูปแบบการใช้งาน (Syntax)

void imagepalettecopy(GdImage $destination, GdImage $source)

พารามิเตอร์

  • $destination – รูปภาพปลายทาง
  • $source – รูปภาพต้นทาง

ค่าที่คืนกลับ

ไม่คืนค่า (void)

ตัวอย่าง 1: คัดลอก palette แล้วใช้สี

<?php
$palette1 = imagecreate(700, 300);
$palette2 = imagecreate(700, 300);
$gray = imagecolorallocate($palette1, 122, 122, 122);
imagepalettecopy($palette2, $palette1);
imagefilledrectangle($palette2, 0, 0, 99, 99, $gray);
header('Content-type: image/png');
imagepng($palette2);
imagedestroy($palette1);
imagedestroy($palette2);
?>

ผลลัพธ์

PHP – คัดลอก Palette ระหว่างรูปภาพด้วย imagepalettecopy()

ตัวอย่าง 2: นับจำนวนสีใน palette

<?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 . "
"; echo "Colors in image 2 is " . $color2; ?>

ผลลัพธ์

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