PHP 8.3.4 Released!

imagepalettecopy

(PHP 4 >= 4.0.1, PHP 5, PHP 7, PHP 8)

imagepalettecopyCopiar la paleta de una imagen a otra

Descripción

imagepalettecopy(resource $destination, resource $source): void

imagepalettecopy() copia la paleta de la imagen source a la imagen destination.

Parámetros

destination

El recurso de imagen de destino.

source

El recurso de imagen de origen.

Valores devueltos

No devuelve ningún valor.

Ejemplos

Ejemplo #1 Ejemplo de imagepalettecopy()

<?php
// Crear dos imágenes de paleta
$paleta1 = imagecreate(100, 100);
$paleta2 = imagecreate(100, 100);

// Asignar verde al fondo
// en la primera imagen de paleta
$verde = imagecolorallocate($paleta1, 0, 255, 0);

// Copiar la paleta de la imagen 1 a la imagen 2
imagepalettecopy($paleta2, $paleta1);

// Ya que la paleta ha sido copiada podemos usar el
// color verde asignado a la imagen 1 sin usar
// imagecolorallocate() dos veces
imagefilledrectangle($paleta2, 0, 0, 99, 99, $verde);

// Imprimir la imagen en el navegdor
header('Content-type: image/png');

imagepng($paleta2);
imagedestroy($paleta1);
imagedestroy($paleta2);
?>

add a note

User Contributed Notes 2 notes

up
0
buzz at nospam dot oska dot com
18 years ago
actually it doesn't "copy" the palette exactly. It copys the colors from the source palette to the destination image. the palette you end up with in the destination image will be "same colors different order". If you want an EXACT palette copy (at the expense of messing up your image if you aren't careful), then use this code:
<?
// this is a drop-in replacement for imagepalettecopy, except that it make NO attempt to modifiy any of the
// colors in the dest image, just the palette. The result? if you're palette's aren't very similar, the image will look completely different, and likely terrible!
function imagepalettecopy_exact ( $dst_img, $src_img) {
for( $c = 0 ; $c < imagecolorstotal($src_img); $c++) {
$col = imagecolorsforindex($src_img,$c); //get color at index 'c' in the color table
imagecolorset($dst_img,$c,$col[red],$col[green],$col[blue]); //set color at index 'c' to $col in the $dst_image
}
}

?>
up
0
Los Olvidados
21 years ago
To be precise, this function replaces the palette in the destination.
To Top