PHP: Hypertext Preprocessor (original) (raw)

imagepalettecopy

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

imagepalettecopy — Copy the palette from one image to another

Description

Parameters

dst

The destination image object.

src

The source image object.

Return Values

No value is returned.

Changelog

Version Description
8.0.0 dst and src expectGdImage instances now; previously, resources were expected.

Examples

Example #1 imagepalettecopy() example

<?php // Create two palette images $palette1 = imagecreate(100, 100); $palette2 = imagecreate(100, 100);// Allocate the background to be // green in the first palette image <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>g</mi><mi>r</mi><mi>e</mi><mi>e</mi><mi>n</mi><mo>=</mo><mi>i</mi><mi>m</mi><mi>a</mi><mi>g</mi><mi>e</mi><mi>c</mi><mi>o</mi><mi>l</mi><mi>o</mi><mi>r</mi><mi>a</mi><mi>l</mi><mi>l</mi><mi>o</mi><mi>c</mi><mi>a</mi><mi>t</mi><mi>e</mi><mo stretchy="false">(</mo></mrow><annotation encoding="application/x-tex">green = imagecolorallocate(</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.625em;vertical-align:-0.1944em;"></span><span class="mord mathnormal" style="margin-right:0.03588em;">g</span><span class="mord mathnormal">ree</span><span class="mord mathnormal">n</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord mathnormal">ima</span><span class="mord mathnormal" style="margin-right:0.03588em;">g</span><span class="mord mathnormal">eco</span><span class="mord mathnormal" style="margin-right:0.01968em;">l</span><span class="mord mathnormal" style="margin-right:0.02778em;">or</span><span class="mord mathnormal">a</span><span class="mord mathnormal" style="margin-right:0.01968em;">ll</span><span class="mord mathnormal">oc</span><span class="mord mathnormal">a</span><span class="mord mathnormal">t</span><span class="mord mathnormal">e</span><span class="mopen">(</span></span></span></span>palette1, 0, 255, 0);// Copy the palette from image 1 to image 2 imagepalettecopy($palette2, $palette1);// Since the palette is now copied we can use the // green color allocated to image 1 without using // imagecolorallocate() twice imagefilledrectangle($palette2, 0, 0, 99, 99, $green);// Output image to the browser header('Content-type: image/png');imagepng($palette2); ?>

Found A Problem?

buzz at nospam dot oska dot com

19 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 ( dstimg,dst_img, dstimg,src_img) {
for( c=0;c = 0 ; c=0;c < imagecolorstotal($src_img); $c++) {
col=imagecolorsforindex(col = imagecolorsforindex(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 colinthecol in the colinthedst_image
}
}

?>

`

Los Olvidados

22 years ago

To be precise, this function replaces the palette in the destination.