如何使用 PHP 中的 imagepalettecopy() 函数将调色板从一个图像复制到另一个图像?

phpserver side programmingprogramming

imagepalettecopy() 是一个内置的 PHP 函数,用于将调色板从一个图像复制到另一个图像。此函数将调色板从源图像复制到目标图像。

语法

void imagepalettecopy(resource $destination, resource $source)

参数

imagepalettecopy() 接受两个参数 − $source$destination

  • $destination −指定目标图像资源。

  • $source − 指定源图像资源。

返回值

imagepalettecopy() 不返回任何值。

示例 1

<?php
   // 使用 imagecreate() 函数创建两个调色板图像。
   $palette1 = imagecreate(700, 300);
   $palette2 = imagecreate(700, 300);
   
   // 将背景分配为
   // 在第一个调色板图像中为灰色
   $gray = imagecolorallocate($palette1, 122, 122, 122);

   // 将调色板从图像 1 复制到图像 2
   imagepalettecopy($palette2, $palette1);

   // 未使用灰色分配给图像 1
   // imagecolorallocate() 两次
   imagefilledrectangle($palette2, 0, 0, 99, 99, $gray);

   // 将图片输出到浏览器
   header('Content-type: image/png');
   imagepng($palette2);
   imagedestroy($palette1);
   imagedestroy($palette2);
?>

输出

示例 2

<?php
   // 使用 imagecreate() 函数创建两个调色板图像。
   $palette1 = imagecreate(500, 200);
   $palette2 = imagecreate(500, 200);

   // 创建灰色
   $gray= imagecolorallocate($palette1, 0, 255, 0);

   // 灰色作为调色板 1 的背景
   imagefilledrectangle($palette1, 0, 0, 99, 99, $gray);

   // 将调色板从图像 1 复制到图像 2
   imagepalettecopy($palette2, $palette1);

   // 获取图像中的颜色数量
   $color1 = imagecolorstotal($palette1);
   $color2 = imagecolorstotal($palette2);
   
   echo "Colors in image 1 are " . $color1 . "<br>";
   echo "Colors in image 2 is " . $color2;
?>

输出

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

相关文章