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


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

语法

void imagepalettecopy(resource $destination, resource $source)

参数

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

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

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

Explore our latest online courses and learn new skills at your own pace. Enroll and become a certified expert to boost your career.

返回值

imagepalettecopy() 不返回任何值。

示例 1

<?php
   // Create two palette images using imagecreate() function.
   $palette1 = imagecreate(700, 300);
   $palette2 = imagecreate(700, 300);
   
   // Allocate the background to be
   // gray in the first palette image
   $gray = imagecolorallocate($palette1, 122, 122, 122);

   // Copy the palette from image 1 to image 2
   imagepalettecopy($palette2, $palette1);

   // gray color allocated to image 1 without using
   // imagecolorallocate() twice
   imagefilledrectangle($palette2, 0, 0, 99, 99, $gray);

   // Output image to the browser
   header('Content-type: image/png');
   imagepng($palette2);
   imagedestroy($palette1);
   imagedestroy($palette2);
?>

输出

示例 2

 现场演示

<?php
   // Created two palette images using imagecreate() function.
   $palette1 = imagecreate(500, 200);
   $palette2 = imagecreate(500, 200);

   // Create a gray color
   $gray= imagecolorallocate($palette1, 0, 255, 0);

   // gray color as the background to palette 1
   imagefilledrectangle($palette1, 0, 0, 99, 99, $gray);

   // Copy the palette from image 1 to image 2
   imagepalettecopy($palette2, $palette1);

   // Get the number of colors in the image
   $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

更新于: 2021 年 8 月 9 日

94 次浏览

开启你的 职业生涯

通过完成课程获得认证

开始
广告