[56K WARN] Help speed up my imagecreatefromgif()
Posted: Tue Oct 25, 2005 3:32 pm
Greetz, PHPDN, and thanks for reading this post. I have a working script that could use a little cleaning up to boost performance. Long story short: I'm writing an online RPG in PHP/MySQL and the world map is layed out in tiles 100 X 108.
To travel the map, I use a line of sight idea. Your character can see 6 tiles in each direction while standing in the middle of the image. When you take a step, your character stays in that location while the contents of the surrounding tiles relocate to reflect the movement.
The final output is sent to the browser as a newly generated image.
Here are some pieces:
The base tile. Anything that is not grass is added on, like
forest trees,
water,
and mountains.[/img]
----------------------------------------------------
Queries are made to locate your position and surrounding landscape and the final output is a single image file like this one.

----------------------------------------------------
----------------------------------------------------
As you can see, the script works. But I don't think my methods are as fast as they could be.
Here's that part of my script:
Creating this image is currently demanding the creation of 14: the basemap, and 13 updates!
Trying to tighten it up is going to be on my mind all day, so even if no replies, at least I shared some code.
Is it worth it to store my east/west variables to compound a single GIANT query and let ifs work it out?
Or does the creation of 14 images really take that long as compared to one?
Thanks for reading! I look forward to replies.
To play the current build , follow the image urls..
To travel the map, I use a line of sight idea. Your character can see 6 tiles in each direction while standing in the middle of the image. When you take a step, your character stays in that location while the contents of the surrounding tiles relocate to reflect the movement.
The final output is sent to the browser as a newly generated image.
Here are some pieces:
The base tile. Anything that is not grass is added on, like
forest trees,
water,
and mountains.[/img]----------------------------------------------------
Queries are made to locate your position and surrounding landscape and the final output is a single image file like this one.

----------------------------------------------------
----------------------------------------------------
As you can see, the script works. But I don't think my methods are as fast as they could be.
Here's that part of my script:
Code: Select all
// The base image of grassland
$background = imagecreatefromgif("los7.gif");
// Each player generates his/her own image to view. This overwrites any previous with a blank glassland.
$zpic= imagegif($background,"los/$_COOKIE[username].gif",100);
// $player is known at this point. $tarcol (Target Column), and $tarrow used in the final function calls
$tarcol=$player[col];
$tarrow=$player[row];
// map is in hexagons, so odd columns return different data from even. Rows are zig-zagged.
// this limit the queries to what is applicable. These turn into function argument ($wide).
if ($tarcol % 2 == 0){
$cm6=0;$cm5=2;$cm4=4;$cm3=6;$cm2=6;$cm1=6;$cr=6;$cp1=6;$cp2=6;$cp3=6;$cp4=5;$cp5=3;$cp6=1;
} else {
$cm6=1;$cm5=3;$cm4=5;$cm3=6;$cm2=6;$cm1=6;$cr=6;$cp1=6;$cp2=6;$cp3=6;$cp4=4;$cp5=2;$cp6=0;}
/////////////////////////////
//Finally, the function.
// It runs 13 times! Once per visible map row.
// a sample call from the bottom: los("-6","$cm6",$tarcol,$tarrow);
// arguments: ('my row minus 6','determined width','my col','my row')
function los($rownum, $wide,$tc,$tr){
$background = imagecreatefromgif("los/$_COOKIE[username].gif");
// scopes query
$row=($tr + $rownum);
$west=($tc-$wide);
$east=($tc+$wide);
// some pixel placement
$centeroffsetcol=($tc-7);
$centeroffsetrow=($tr-6);
//Query
$getthere=mysql_query("select * from map where (row='$row') and (col >= $west) and (col <= $east) order by id");
//more pixel placement
$tx=0;$lx=0;$tinc=27;$linc=30;$coloff=14;
while ($item=mysql_fetch_assoc($getthere)){
//////////////////////////////////////////////////////////terrain
if ($item[terrain] != 'land'){
//final pixel placement
if ($item[col] % 2 !=0){$offset=0;}else{$offset=$coloff;}
if ($tc % 2 == '0'){$offset=$offset+14;}else{$offset=$offset+27;}
$top= ($tx + (($item[row]-$centeroffsetrow) * $tinc) - $tinc + $offset -1);
$left=($lx + (($item[col]-$centeroffsetcol) * $linc) - $linc );
$insert = imagecreatefromgif("$item[terrain].gif");
imagecopymerge($background,$insert,$left,$top,0,0,40,29,100);
} // if !land
} // while contents of row
//Save the updated image
$zpic= imagegif($background,"los/$_COOKIE[username].gif",100);
} // func
los("-6","$cm6",$tarcol,$tarrow);
los("-5","$cm5",$tarcol,$tarrow);
los("-4","$cm4",$tarcol,$tarrow);
los("-3","$cm3",$tarcol,$tarrow);
los("-2","$cm2",$tarcol,$tarrow);
los("-1","$cm1",$tarcol,$tarrow);
los("0","$cr",$tarcol,$tarrow);
los("1","$cp1",$tarcol,$tarrow);
los("2","$cp2",$tarcol,$tarrow);
los("3","$cp3",$tarcol,$tarrow);
los("4","$cp4",$tarcol,$tarrow);
los("5","$cp5",$tarcol,$tarrow);
los("6","$cp6",$tarcol,$tarrow);
echo "<img src=los/$_COOKIE[username].gif id=map>";Trying to tighten it up is going to be on my mind all day, so even if no replies, at least I shared some code.
Is it worth it to store my east/west variables to compound a single GIANT query and let ifs work it out?
Or does the creation of 14 images really take that long as compared to one?
Thanks for reading! I look forward to replies.
To play the current build , follow the image urls..