Friday, February 6, 2026
HomeLanguagesPHP | imagepalettecopy() function

PHP | imagepalettecopy() function

The imagepalettecopy() function is an inbuilt function in PHP which is used to copy the palette from one image to another.

Syntax:

void imagepalettecopy( resource $destination, resource $source )

Parameters:This function accepts two parameters as mentioned above and described below:

  • $destination: It specifies the destination image resource.
  • $source: It specifies the source image resource.

Return Value: This function returns nothing.

Below given programs illustrate the imagepalettecopy() function in PHP:

Program 1:




<?php
// Create two palette images
$palette1 = imagecreate(700, 200);
$palette2 = imagecreate(700, 200);
  
// Create a green color
$green = imagecolorallocate($palette1, 0, 255, 0);
  
// Add green color as background to pallete 1
imagefilledrectangle($palette1, 0, 0, 99, 99, $green);
  
// Copy the palette from image 1 to image 2
imagepalettecopy($palette2, $palette1);
  
// Output image to the browser and see the color is green
header('Content-type: image/png');
imagepng($palette2);
?>


Output:

Program 2:




<?php
// Create two palette images
$palette1 = imagecreate(700, 200);
$palette2 = imagecreate(700, 200);
  
// Create a green color
$green = imagecolorallocate($palette1, 0, 255, 0);
  
// Add green color as background to pallete 1
imagefilledrectangle($palette1, 0, 0, 99, 99, $green);
  
// Copy the palette from image 1 to image 2
imagepalettecopy($palette2, $palette1);
  
// Get the number of colors in image
$color1 = imagecolorstotal($palette1);
$color2 = imagecolorstotal($palette2);
  
echo "Number of colors at image 1 is " . $color1 . "<br>";
echo "Number of colors at image 2 is " . $color2;
?>


Output:

Number of colors at image 1 is 1
Number of colors at image 2 is 1

Reference: https://www.php.net/manual/en/function.imagepalettecopy.php

RELATED ARTICLES

Most Popular

Dominic
32491 POSTS0 COMMENTS
Milvus
126 POSTS0 COMMENTS
Nango Kala
6862 POSTS0 COMMENTS
Nicole Veronica
11986 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12075 POSTS0 COMMENTS
Shaida Kate Naidoo
6995 POSTS0 COMMENTS
Ted Musemwa
7236 POSTS0 COMMENTS
Thapelo Manthata
6946 POSTS0 COMMENTS
Umr Jansen
6931 POSTS0 COMMENTS