intellix Posted November 25, 2010 Share Posted November 25, 2010 Hey guys, This is my first post... hopefully I'll remember my password with the restrictions applied to my usual password 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 Quote Link to comment https://forums.phpfreaks.com/topic/219803-gd-image-masking/ Share on other sites More sharing options...
intellix Posted November 25, 2010 Author Share Posted November 25, 2010 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(); Quote Link to comment https://forums.phpfreaks.com/topic/219803-gd-image-masking/#findComment-1139580 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.