Jump to content
  • 0

Applying .pal to created image file using PHP


Question

Posted (edited)

I'm just wondering if how can I apply a palette on my created single head frame resource something like in the code below

$head = imagecreatefrompng('first.png');

I'm using PHP to apply .pal file. Also, please be noted that the image above doesn't come / parsed from .spr file

Edited by Jupeto

5 answers to this question

Recommended Posts

Posted

You can't do this on a PNG as the colors are stored as RGBA colors, but you can find the color index by comparing the pixel against the original palette, then swap the colors with the new palette.

Example:

$origpallete = array(...);
$newpallete = array(...);

for ($x = 0; $x < $width; $x++)
{
for ($x = 0; $x < $width; $x++)
{
	$color = getcolor($x, $y);

	for ($i = 0; $i < count($origpallete); $i++)
	{
		if ($origpallete[$i] == $color)
			break;
	}

	if ($i < count($newpallete))
		setpixel($x, $y, $newpallete[$i]);
}
}

Posted (edited)

I really don't know how to work with palettes

To any of you know how please do help, Thanks

here's my current code


$sprite = new sprite('../../data/sprite/head/' . $gender . '/' . $head . '_' . $gender . '.spr');
$sprite_resource = $sprite->displayFrame( $frame );

$palette_handle = fopen( '../../data/palette/head/9.pal', "r" );
$palette_data = fread( $palette_handle, 1024 );

/* Open the base job image */
$base_job = imagecreatefrompng('../../data/sprite/body/1.png');

$frame  = $sprite->frames[$frame];
$width  = $frame['width'];
$height = $frame['height'];
$data   = $frame['data'];

$pal = array();
for ( $i=0, $j=4; $i<256; ++$i, ++$j )
$pal[$i] = imagecolorallocate(
$sprite_resource,
ord($sprite->palette[$j++]),
ord($sprite->palette[$j++]),
ord($sprite->palette[$j++])
);

for ( $i=0; $i < $width * $height; ++$i )
{
imagesetpixel( $sprite_resource, $i % $width, $i/$width | 0, $pal[ ord($data[$i]) ] );
}

/* Create the base head image*/
imagegif( $sprite_resource, $temp_file );

This currently results to this image; I only want the head to be dyed

artworx_character.png

Edited by Jupeto
Posted

Not sure it will help but that's how I do it. I'll use pseudo-code, because I forgot about PHP.

Make sure your PNG Head is in 8 bits. Export the palette of your PNG in .pal (using photoshop), let's name it "Original_Head.pal". Remove the photoshop header in your .pal file.

Load "Original_Head.pal" in hex string in your php script. => $hex_head_pal

Load "New.pal" that you want to apply to your head in hex string => $hex_new_pal

Load your "Head.png" in hex string => $hex_head_image

Now do: $hex_head_image.replace($hex_head_pal, $hex_new_pal).

Then create your image using a png header and echo the binary content of the image. Load your image in a src.

Posted

Well, I will release my php charsim soon, but I can help for now.

There is a lot of codes that aren't used in your code so it's hard to know what you do exactly. But for sure, the $j in the for() should be init as 0 (not 4).

Well if you need help it will be better to post the whole script, here or in pm :P

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...