Page 1 of 1

Image library - generating a map

Posted: Sun Jul 26, 2009 8:03 pm
by psychotomus
I have 100 tiles i need to place like

01,02,03,04,05,06,07,08,09,10
11,12,13,14,15,16,17,18,19,20
ect.

My code doesn't work to well. it creates a empty file. i get no errors so i must be heading in the right direction: heres what i got so far:

Code: Select all

function generate_map($gameid, $userid)
{
    //game info query.
    $result = mysql_query("SELECT map_id, playerturn FROM pvpgames WHERE id='$gameid' AND result='0' AND playerids LIKE '%:$userid:%'") or die(mysql_error());
 
    //fetch game info
    $game = mysql_fetch_object($result);
    
    //fetch map info
    $result = mysql_query("SELECT id, name, width, height FROM mapsmultiplayer WHERE id='$game->map_id'") or die(mysql_error());
    $map = mysql_fetch_object($result);
    
    $bg= imagecreate($map->width * 32, $map->height * 32);
    $counter = 0;
    //fetch map data
    $result = mysql_query("SELECT x,  y, charid, tile FROM pvpgamemapdata WHERE map_id='$map->id' AND game_id='$gameid' ORDER BY id ASC") or die(mysql_error());
    while($tile = mysql_fetch_object($result))
    {
        if($counter == $map->width)
        {
            $counter=0;
        }
        
        //if no chars.
        if($tile->charid == 0)
        {
            $over = imagecreatefrompng("data/tiles/$tile->tile");
            imagecopymerge($bg, $over, $tile->x*32, $tile->y*32, 0, 0, 32, 32, 100);
        }
        else
        {
            //fetch char information
            $result1 = mysql_query("SELECT * FROM pvpgamedatachars WHERE id='$tile->charid'") or die(mysql_error());
            $char = mysql_fetch_object($result1);
            
            //fetch rest of char information
            $result1 = mysql_query("SELECT name FROM charactersmultiplayer WHERE id='$char->charid'") or die(mysql_error());
            $chars = mysql_fetch_object($result1);
 
            $over1 = imagecreatefrompng("data/chars/$chars->name.png");
            $bg1 = imagecreatefrompng("data/tiles/$tile->tile");
            
            imagecopymerge($bg1, $over1, 0, 0, 0, 0, 32, 32, 50);
            imagecopymerge($bg, $bg1, $tile->x*32, $tile->y*32, 0, 0, 32, 32, 100);
        }
        
        
        $counter++;
    }
    
    $handle = fopen("data/games/$gameid.png", "w");
    fwrite($handle, $bg);
    fclose($handle);
    
    imagedestroy($bg);
    imagedestroy($bg1);
    imagedestroy($over);
    imagedestroy($over1);
}
?>

Re: Image library - generating a map

Posted: Mon Jul 27, 2009 12:23 am
by requinix

Code: Select all

$handle = fopen("data/games/$gameid.png", "w");
fwrite($handle, $bg);
fclose($handle);
That's not the way to save an image. Look up the imagepng function.