Jump to content
  • 0

Applying .pal to created image file using PHP


Jupeto

Question


  • Group:  Members
  • Topic Count:  34
  • Topics Per Day:  0.01
  • Content Count:  225
  • Reputation:   39
  • Joined:  01/20/12
  • Last Seen:  

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
Link to comment
Share on other sites

5 answers to this question

Recommended Posts


  • Group:  Members
  • Topic Count:  9
  • Topics Per Day:  0.00
  • Content Count:  303
  • Reputation:   101
  • Joined:  11/13/11
  • Last Seen:  

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]);
}
}

Link to comment
Share on other sites


  • Group:  Members
  • Topic Count:  34
  • Topics Per Day:  0.01
  • Content Count:  225
  • Reputation:   39
  • Joined:  01/20/12
  • Last Seen:  

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
Link to comment
Share on other sites


  • Group:  Members
  • Topic Count:  21
  • Topics Per Day:  0.00
  • Content Count:  345
  • Reputation:   230
  • Joined:  03/21/12
  • Last Seen:  

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.

Link to comment
Share on other sites


  • Group:  Members
  • Topic Count:  34
  • Topics Per Day:  0.01
  • Content Count:  225
  • Reputation:   39
  • Joined:  01/20/12
  • Last Seen:  

Well I'm now able to extract the frame from each .spr file.

I was reading this thread but I'm still not getting the right syntax

http://www.google.co...8kW0gN7uoPoff6w

Edited by Jupeto
Link to comment
Share on other sites


  • Group:  Members
  • Topic Count:  9
  • Topics Per Day:  0.00
  • Content Count:  379
  • Reputation:   304
  • Joined:  11/10/11
  • Last Seen:  

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

Link to comment
Share on other sites

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.

×
×
  • Create New...