Jump to content

GD image masking


intellix

Recommended Posts

Hey guys, This is my first post... hopefully I'll remember my password with the restrictions applied to my usual password :D but anyway...

 

I've just started using GD to mask two images together and present users the end result and am noticing that it's quite slow and nowhere near as quick as I first expected...

 

Basically I'm devving a game where players explore a map. I'm creating a world map where there are separate transparent images for each terrain type and players can turn these layers on and off on their world map view as they please. On top of this players shouldn't be able to view the raw images as they must first explore the squares to reveal them.

 

So what I have is per player/clan a Fog of war mask image is created and cached per player by querying the database. I then merge this Fog of War mask on top of the terrain pictures to return to the player a picture of each terrain on the map they have explored.

 

<img src="worldmap.php?terrain=1" />
<img src="worldmap.php?terrain=2" />
<img src="worldmap.php?terrain=3" />

 

Inside worldmap.php the player's session is grabbed so players only ever receive their own fog of war image

 

I originally thought this was going to be a simple put one image on top of the other with transparency to hide pixels they have not yet explored but instead the code snippet I grabbed is a loop that checks each pixel and writes it to a new image. Again I was expecting this to be almost instant but instead it takes about 2-3 seconds per picture and there are about 15 different images being grabbed per player. The images are 501x501 each.

At the current rate its looking like I'll also have to cache each of the fow and terrain merged images to deliver them as quick as I wanted.

 

Is this really a slow process or am I doing something wrong? I'll paste some of the PHP to give you an idea of how I'm doing it...

 

function imagealphamask( &$picture, $mask ) {
	// http://php.net/manual/en/function.imagesavealpha.php
	// Get sizes and set up new picture
	$xSize = imagesx( $picture );
	$ySize = imagesy( $picture );
	$newPicture = imagecreatetruecolor( $xSize, $ySize );
	imagesavealpha( $newPicture, true  );
	imagefill( $newPicture, 0, 0, imagecolorallocatealpha( $newPicture, 0, 0, 0, 127 ) );

	// Perform pixel-based alpha map application
	for( $y = 0; $y < $ySize; $y++ ) {
		for( $x = 0; $x < $xSize; $x++ ) {
			$maskColor = imagecolorat( $mask, $x, $y);
			$maskAlpha = imagecolorsforindex( $mask, $maskColor );
			$maskAlpha = floor( $maskAlpha[ 'red' ] / 2 );
			$pictureColor = imagecolorat( $picture, $x, $y);
			$pictureAlpha = imagecolorsforindex( $picture, $pictureColor );
			$pictureAlpha = floor( $pictureAlpha[ 'red' ] / 2 );

			// Todo: switch colour based on terrain type?

			// Also respect picture alpha
			if ($pictureAlpha == 127){
				$maskAlpha = 127;
			}

			$color = imagecolorsforindex( $picture, $pictureColor );
			imagesetpixel( $newPicture, $x, $y, imagecolorallocatealpha( $newPicture, $color[ 'red' ], $color[ 'green' ], $color[ 'blue' ], $maskAlpha ) );
		}
	}

	// Copy back to original picture
	imagedestroy( $picture );
	$picture = $newPicture;
}

 

 

Thanks alot guys :)

Link to comment
Share on other sites

I've had a little look through it... played with a few other functions but nothing else seems to do the same thing :S

 

I commented out these lines and it seems to be MUCH quicker without them... obviously they're needed but I can see where that the slowest function here is imagecolorsforindex();

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

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

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  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...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.