An exemple of a GD2 script:
<?
header("Content-type: image/png");
include('../config.php');
new connect;
$width = 550;
$height = 350;
$result = mysql_query("SELECT * FROM rpg_players WHERE id = '".$_GET['id']."'");
$player = mysql_fetch_array($result);
$pid = $player['id'];
$posx = $player['posx'];
$posy = $player['posy'];
$raceid = $player['race_id'];
$city_id = $player['city_id'];
$screen = $player['city_screen'];
$map_id = $player['map_id'];
$chests = $player['chests'];
$pm = $player['partymembers'];
if($pm != '')
$partymembers = explode('-', $pm);
$num = $_GET['count'];
for($i=0;$i<$num;$i++)
{
$var = "string$i";
$msgs[$i] =$_GET[$var];
}
//create main image
$temp = imagecreatetruecolor($width, $height);
//get city name
$city_name = mysql_result(mysql_query("SELECT name FROM rpg_cities WHERE id=$city_id"),0);
//get
$map_name = $city_name.".PNG";
//get path
$map_path = mysql_result(mysql_query("SELECT path from rpg_maps WHERE id=$map_id"),0);
//create image
$map = imagecreatefrompng($map_path);
//create player
$ava = imagecreatefrompng("/home/laene/public_html/Marc/worms/images/Player/Map".$raceid.".PNG");
$avaw = (int)(imagesx($ava) / 2);
$avah = (int)(imagesy($ava) / 2);
$white = imagecolorallocate($ava, 255, 255, 255);
imagecolortransparent($ava, $white);
// top left x and y of the viewport
$tlx = $posx - ($width/2);
$tly = $posy - ($height/2);
//Get players around you
$result2 = mysql_query("SELECT * FROM rpg_players WHERE map_id=$map_id AND posx > $tlx AND posx < $tlx + $width AND posy > $tly AND posy < $tly + $height AND id!=$pid");
$num2 = mysql_num_rows($result2);
//render players around
for($i=1; $i<=$num2; $i++)
{
$p = mysql_fetch_array($result2);
$p_race = $p["race_id"];
$px = $p["posx"];
$py = $p["posy"];
$ava2 = imagecreatefrompng("/home/laene/public_html/Marc/worms/images/Player/Map".$p_race.".PNG");
$ava2w = (int)(imagesx($ava2) / 2);
$ava2h = (int)(imagesy($ava2) / 2);
$white2 = imagecolorallocate($ava2, 255, 255, 255);
imagecolortransparent($ava2, $white);
imagecopymerge($map, $ava2, $px-$ava2w, $py-$ava2h, 0, 0, imagesx($ava2), imagesy($ava2), 100);
if($p['id'] == $partymembers[0] || $p['id'] == $partymembers[1])
{
//draw green elipse around him
$green = imagecolorallocate($map, 255, 255, 255);
imageellipse($map, $px, $py, $ava2w*2+5, $ava2h*2+5, $green);
}
}
//Get NPC's around
$result3 = mysql_query("SELECT * FROM rpg_npc WHERE map_id=$map_id AND posx > $tlx AND posx < $tlx + $width AND posy > $tly AND posy < $tly + $height");
$num3 = mysql_num_rows($result3);
//render NPC's around
for($i=1; $i<=$num3; $i++)
{
$npc = mysql_fetch_array($result3);
$px = $npc["posx"];
$py = $npc["posy"];
$sprite_id = $npc["sprite_id"];
$image = mysql_result(mysql_query("SELECT image FROM rpg_npc_sprites WHERE id=$sprite_id"),0);
$path = "/home/laene/public_html/Marc/worms/images/NPC/".$image;
$ava3 = imagecreatefrompng($path);
$ava3w = (int)(imagesx($ava3) / 2);
$ava3h = (int)(imagesy($ava3) / 2);
$white3 = imagecolorallocate($ava3, 255, 255, 255);
imagecolortransparent($ava3, $white3);
imagecopymerge($map, $ava3, $px-$ava3w, $py-$ava3h, 0, 0, imagesx($ava3), imagesy($ava3), 100);
}
//get chests around
$result4 = mysql_query("SELECT * FROM rpg_chests WHERE map_id=$map_id AND x > $tlx AND x < $tlx + $width AND y > $tly AND y < $tly + $height");
$num4 = mysql_num_rows($result4);
//render chests
for($i=1; $i<=$num4; $i++)
{
$chest = mysql_fetch_array($result4);
$cx = $chest['x'];
$cy = $chest['y'];
$cid = $chest['id'];
if(strstr($chests, $cid) == FALSE)
$cpath = "/home/laene/public_html/Marc/worms/images/Items/".$city_name."/".$city_name."ChestClosed.png";
else
$cpath = "/home/laene/public_html/Marc/worms/images/Items/".$city_name."/".$city_name."ChestOpen.png";
$chestim = imagecreatefrompng($cpath);
$white4 = imagecolorallocate($chestim, 255,255, 255);
imagecolortransparent($chestim, $white4);
imagecopymerge($map, $chestim, $cx, $cy, 0,0,15,15,100);
}
//render main character
imagecopymerge($map, $ava, $posx - $avaw, $posy - $avah, 0, 0, imagesx($ava), imagesy($ava), 100);
//Get Ovl path
$mapovlpath = str_replace(".PNG", "Ovl.PNG", $map_path);
//Check if Overlayer exists
if(file_exists($mapovlpath))
{
//if so, load the image and render it
$mapovl = imagecreatefrompng($mapovlpath);
$grey = imagecolorallocate($mapovl, 138, 179, 77);
imagecolortransparent($mapovl, $grey);
$ovlwidth = imagesx($mapovl);
$ovlheight = imagesy($mapovl);
imagecopymerge($map, $mapovl, 0, 0, 0, 0, $ovlwidth, $ovlheight, 100);
}
$mapbrightpath = str_replace(".PNG", "Bright.PNG", $map_path);
//Render 'time effect'
$bright = GetMapBrightness();
imagefilter($map, IMG_FILTER_BRIGHTNESS, $bright);
//check if 'bright' layer exists
if(file_exists($mapbrightpath))
{
//if so, load the image and render it
$mapbright = imagecreatefrompng($mapbrightpath);
$greybright = imagecolorallocate($mapbright, 138, 179, 77);
imagecolortransparent($mapbright, $greybright);
$brightwidth = imagesx($mapbright);
$brightheight = imagesy($mapbright);
imagecopymerge($map, $mapbright, 0, 0, 0, 0, $brightwidth, $brightheight, 100);
}
//if a TV screen
if($map_id == 16)
{
$numofscreen = 0;
$end = true;
while($end==true)
{
$imagename = $numofscreen + 1;
$filename = "/home/laene/public_html/Marc/worms/images/screens/".$imagename.".PNG";
if(file_exists($filename))
$numofscreen++;
else
$end=false;
}
$rand = rand(1,$numofscreen);
$pic = imagecreatefrompng("/home/laene/public_html/Marc/worms/images/screens/".$rand.".PNG");
$screen = imagecreatetruecolor(118, 51);
$pich = imagesx($pic);
$picw = imagesy($pic);
$startx = (int)(59-($pich/2));
imagecopymerge($screen, $pic, $startx, 0, 0, 0, $pich, $picw, 100);
imagecopymerge($screen, $pic, $startx, 0, 0, 0, $pich, $picw, 100);
imagecopymerge($map, $screen, 93, 9, 0, 0, 118, 51, 100);
}
//Render Weather effect
$weather = new Weather();
$type = $weather->GetWeather();
//fog is before cutting
if($type == 'fog')
{
if(strpos($map_path,$map_name) !== FALSE)
{
$fog = imagecreatefrompng("/home/laene/public_html/Marc/worms/images/Weather/Fog.PNG");
$mapw = imagesx($map);
$maph = imagesy($map);
$currx = 0;
$curry = 0;
while($currx <= $mapw)
{
while($curry <= $maph)
{
imagecopymerge($map, $fog, $currx, $curry, 0, 0, 550, 350, 65);
$curry += 350;
}
$currx += 550;
$curry = 0;
}
imagedestroy($fog);
}
}
//put the image into our frame
imagecopymerge($temp, $map, 0, 0, $tlx, $tly, $width, $height, 100);
//Rain effect
if(strpos($type, "rain") !== FALSE)
{
if(strpos($map_path,$map_name) !== FALSE)
{
$rainlayer = imagecreatefrompng("/home/laene/public_html/Marc/worms/images/Weather/RainLayer.PNG");
imagecopymerge($temp, $rainlayer, 0, 0, 0, 0, 550, 350, 50);
imagedestroy($rainlayer);
//1st quadrant
for($i=0;$i<30;$i++)
{
$rand = rand(1,4);
$randx = rand(0, 137);
$randy = rand(0, 174);
$drop = imagecreatefrompng("/home/laene/public_html/Marc/worms/images/Weather/".$type."2.PNG");
$white = imagecolorallocate($drop, 255, 255, 255);
imagecolortransparent($drop, $white);
imagecopymerge($temp, $drop, $randx, $randy, 0, 0, imagesx($drop), imagesy($drop), 63);
imagedestroy($drop);
}
//2nd quadrant
for($i=0;$i<30;$i++)
{
$rand = rand(1,4);
$randx = rand(138,274);
$randy = rand(0, 174);
$drop = imagecreatefrompng("/home/laene/public_html/Marc/worms/images/Weather/".$type."2.PNG");
$white = imagecolorallocate($drop, 255, 255, 255);
imagecolortransparent($drop, $white);
imagecopymerge($temp, $drop, $randx, $randy, 0, 0, imagesx($drop), imagesy($drop), 63);
imagedestroy($drop);
}
//3rd quadrant
for($i=0;$i<30;$i++)
{
$rand = rand(1,4);
$randx = rand(275,412);
$randy = rand(0, 174);
$drop = imagecreatefrompng("/home/laene/public_html/Marc/worms/images/Weather/".$type."2.PNG");
$white = imagecolorallocate($drop, 255, 255, 255);
imagecolortransparent($drop, $white);
imagecopymerge($temp, $drop, $randx, $randy, 0, 0, imagesx($drop), imagesy($drop), 63);
imagedestroy($drop);
}
//4th quadrant
for($i=0;$i<30;$i++)
{
$rand = rand(1,4);
$randx = rand(413,550);
$randy = rand(0, 174);
$drop = imagecreatefrompng("/home/laene/public_html/Marc/worms/images/Weather/".$type."2.PNG");
$white = imagecolorallocate($drop, 255, 255, 255);
imagecolortransparent($drop, $white);
imagecopymerge($temp, $drop, $randx, $randy, 0, 0, imagesx($drop), imagesy($drop), 63);
imagedestroy($drop);
}
//5th quadrant
for($i=0;$i<30;$i++)
{
$rand = rand(1,4);
$randx = rand(0,137);
$randy = rand(174, 350);
$drop = imagecreatefrompng("/home/laene/public_html/Marc/worms/images/Weather/".$type."2.PNG");
$white = imagecolorallocate($drop, 255, 255, 255);
imagecolortransparent($drop, $white);
imagecopymerge($temp, $drop, $randx, $randy, 0, 0, imagesx($drop), imagesy($drop), 63);
imagedestroy($drop);
}
//6th quadrant
for($i=0;$i<30;$i++)
{
$rand = rand(1,4);
$randx = rand(138,274);
$randy = rand(174, 350);
$drop = imagecreatefrompng("/home/laene/public_html/Marc/worms/images/Weather/".$type."2.PNG");
$white = imagecolorallocate($drop, 255, 255, 255);
imagecolortransparent($drop, $white);
imagecopymerge($temp, $drop, $randx, $randy, 0, 0, imagesx($drop), imagesy($drop), 63);
imagedestroy($drop);
}
//9th quadrant
for($i=0;$i<30;$i++)
{
$rand = rand(1,4);
$randx = $randx = rand(275,412);
$randy = rand(174, 350);
$drop = imagecreatefrompng("/home/laene/public_html/Marc/worms/images/Weather/".$type."2.PNG");
$white = imagecolorallocate($drop, 255, 255, 255);
imagecolortransparent($drop, $white);
imagecopymerge($temp, $drop, $randx, $randy, 0, 0, imagesx($drop), imagesy($drop), 63);
imagedestroy($drop);
}
//8th quadrant
for($i=0;$i<30;$i++)
{
$rand = rand(1,4);
$randx = rand(413,550);
$randy = rand(174, 350);
$drop = imagecreatefrompng("/home/laene/public_html/Marc/worms/images/Weather/".$type."2.PNG");
$white = imagecolorallocate($drop, 255, 255, 255);
imagecolortransparent($drop, $white);
imagecopymerge($temp, $drop, $randx, $randy, 0, 0, imagesx($drop), imagesy($drop), 63);
imagedestroy($drop);
}
}
}
//Output the Messages the Messages
$num = count($msgs);
for($i=1;$i<=$num;$i++)
{
$msgbox = imagecreatefrompng("/home/laene/public_html/Marc/worms/images/Hud/MessageBox.PNG");
$black = imagecolorallocate($msgbox, 0, 0, 0);
imagestring($msgbox, 3, 10, 15, $msgs[$i-1], $black);
imagecopymerge($temp, $msgbox, 25, 5 + ($i-1)*49, 0, 0, 501, 44, 70);
imagedestroy($msgbox);
}
//Check for drunkness
$drunkres = mysql_query("SELECT * FROM rpg_drunk WHERE player_id=$pid");
$drunknum = mysql_num_rows($drunkres);
if($drunknum == 1)
{
$drunk = mysql_fetch_object($drunkres);
$amplitude = 2*$drunk->level;
$frequency = 2*$drunk->level;
$wrap = 0;
$blurr = 10*$drunk->level;
//imagefilter($temp, IMG_FILTER_COLORIZE);
imageripple($temp,$amplitude,$frequency,$wrap);
}
//apply goggles
//imagefilter($temp, IMG_FILTER_COLORIZE,0,0,255);
//output the image
imagepng($temp);
function GetMapBrightness()
{
$hours = $_GET['hours'];
if($hours == 0)
return -50;
elseif($hours == 1)
return -42;
elseif($hours == 2)
return -33;
elseif($hours == 3)
return -25;
elseif($hours == 4)
return -17;
elseif($hours == 5)
return -8;
elseif($hours == 6)
return 0;
elseif($hours == 7)
return 8;
elseif($hours == 8)
return 17;
elseif($hours == 9)
return 25;
elseif($hours == 10)
return 33;
elseif($hours == 11)
return 42;
elseif($hours == 12)
return 50;
elseif($hours == 13)
return 42;
elseif($hours == 14)
return 33;
elseif($hours == 15)
return 25;
elseif($hours == 16)
return 17;
elseif($hours == 17)
return 8;
elseif($hours == 18)
return 0;
elseif($hours == 19)
return -8;
elseif($hours == 20)
return -17;
elseif($hours == 21)
return -25;
elseif($hours == 22)
return -33;
elseif($hours == 23)
return -42;
}
function imageripple($i,$amplitude,$frequency,$wrap) {
$width = imagesx($i);
$height = imagesy($i);
for ($y=0;$y<$height;$y++) {
for ($x=0;$x<$width;$x++) {
$dis_x = $x+(sin(deg2rad(($y/$height)*360)*$frequency)*$amplitude);
$dis_y = $y+(sin(deg2rad(($x/$width)*360)*$frequency)*$amplitude);
if ($wrap == 1) {
$dis_x = ($dis_x < 0) ? $dis_x + $width : $dis_x;
$dis_x = ($dis_x > $width) ? $dis_x - $width : $dis_x;
$dis_y = ($dis_y < 0) ? $dis_y + $height : $dis_y;
$dis_y = ($dis_y > $height) ? $dis_y - $height : $dis_y;
}
$displacement['x'][$x][$y] = $dis_x;
$displacement['y'][$x][$y] = $dis_y;
}
}
$i = imagedisplace($i,$displacement);
return $i;
}
function imagedisplace($i,$displacement) {
$width = imagesx($i);
$height = imagesy($i);
$temp = imagecreatetruecolor($width,$height);
imagecopy($temp,$i,0,0,0,0,$width,$height);
for ($y=0;$y<$height;$y++) {
for ($x=0;$x<$width;$x++) {
$rgb = imagecolorat($temp,$displacement['x'][$x][$y],$displacement['y'][$x][$y]);
$a = ($rgb >> 24) & 0xFF;
$r = ($rgb >> 16) & 0xFF;
$g = ($rgb >> 8) & 0xFF;
$b = $rgb & 0xFF;
$col = imagecolorallocatealpha($i,$r,$g,$b,$a);
imagesetpixel($i,$x,$y,$col);
}
}
imagedestroy($temp);
return $i;
}
?>
admin edit: added code tags.