php - Converting an image to pre-defined 16 colors -


given image file, what's best way convert old school 16 colors? i.e. white, orange, magenta, light blue, yellow, lime, pink, gray, light gray, cyan, purple, blue, brown, green, red, , black.

i've made little 1x16 pixel image containing 16 of colors can use source palette (right?) i'm having trouble making use of it. seems imagepalettecopy() want (take 16 pixel data image's color palette , copy onto fresh image) code came isn't working:

<?php  $palette = imagecreatefrompng( __dir__ . '/palette.png' );  $source = imagecreatefromjpeg( __dir__ . '/testimage.jpg' );  $source_w = imagesx( $source ); $source_h = imagesy( $source );  $image = imagecreate( $source_w, $source_h );  imagepalettecopy( $palette, $image );  imagecopy( $image, $source, 0, 0, 0, 0, $source_w, $source_h );  header('content-type: image/png'); imagepng( $image ); 

it seems convert 16 colors of it's choosing or (i'm not quite sure).

what missing or doing wrong?

edit: imagepalettecopy() call backwards fixing doesn't either. see below comments.

does changing
imagepalettecopy($palette, $image);
into
imagepalettecopy($image, $palette);
work?

edit:

i tried following palette gif:

enter image description here

i think these colors different yours. (i hand picked greens picture...)

this code tried (not difference here):

<?php  $palette = imagecreatefromgif('palette-gif-03.gif');  $source = imagecreatefromjpeg('test-image-01.jpg');  $source_w = imagesx($source); $source_h = imagesy($source);  $image = imagecreate($source_w, $source_h);  imagepalettecopy($image, $palette);  imagecopy($image, $source, 0, 0, 0, 0, $source_w, $source_h);  header('content-type: image/png'); imagepng($image);  imagedestroy($imgage); imagedestroy($palette); imagedestroy($source); ?> 

and result:

enter image description here

please let me know if should delete picture?!!


Comments

Popular posts from this blog

Change php variable from jquery value using ajax (same page) -

Pull out data related to my apps from Android Play Store and iOS App Store -

How can I fetch data from a web server in an android application? -