// When loading frames:
for( $i=0; $i<$this->sHeader['num_pal']; $i++ )
{
$this->frames[$i] = unpack('Swidth/Sheight/', fread($this->spr, 0x04));
extract( unpack('Ssize', fread($this->spr,0x02) ) );
$this->frames[$i]['data'] = fread( $this->spr, $size );
}
// When you want to draw a frame
$frame = $this->frames[$frame]; // $frame is the frame you want to render
$width = $frame['width'];
$height = $frame['height'];
$data = $frame['data'];
$img = imagecreatetruecolor( $width, $height );
// Allocate palette
$pal = array();
for ( $i=0, $j=4; $i<256; ++$i, ++$j )
$pal[$i] = imagecolorallocate(
$img,
ord($this->palette[$j++]),
ord($this->palette[$j++]),
ord($this->palette[$j++])
);
// Render image
for ( $i=0; $i < $width * $height; ++$i )
{
imagesetpixel( $img, $i % $width, $i/$width | 0, $pal[ ord($data[$i]) ] );
}
imagepng( $img );