图像上的可点击网格

时间:2011-06-03 02:40:33

标签: php gd

我有一张我正在制作的RPG地图。地图上覆盖着一个单元网格,每个单元格为50px x 50px。每个单元格都应该是可点击的,但是在800px乘800px的地图上有这么多可点击的单元格,无论我设置多高,我的脚本都会耗尽分配的内存(最后一次尝试是250M)。正如您在下面的代码中看到的,我正在使用base64编码,然后将图像放入标记中。我相信这可能是问题的一部分,但我不希望用户能够在地图中看到这些信息。我怎样才能实现这一目标并保持在内存限制之下?

<?php

define("IN_GAME", true);
ini_set("memory_limit","500M");

function imagegrid($image, $w, $h, $s, $color)
{
    for($iw = 1; $iw < $w/$s; $iw++)
    {
        imageline($image, $iw*$s, 0, $iw*$s, $w, $color);
    }
    for($ih = 1; $ih < $h/$s; $ih++)
    {
        imageline($image, 0, $ih*$s, $w, $ih*$s, $color);
    }
}

$width = 800;
$height = 800;
$block_size = 50;

$image = "images/maps/world.png";

$img = imagecreatefrompng($image);
$grid = imagecolorallocate($img, 0, 0, 0);

imagesetstyle($img, $grid);
imagegrid($img, $width, $height, $block_size, IMG_COLOR_STYLED);

ob_start();
imagepng($img);
$contents =  ob_get_contents();
ob_end_clean();

$img_STR = "data:image/png;base64,".base64_encode($contents);

imagedestroy($img);

$split = $width/$block_size;
$c = 1;
while($c < $split)
{
    if($c = 1)
    {
        $x1 = $c;
    }
    else
    {
        $x1 = $c*$block_size;
    }

    if($c = 1)
    {
        $y1 = $c;
    }
    else
    {
        $y1 = $c*$block_size;
    }

    $x2 = $x1*$block_size;
    $y2 = $y1*$block_size;

    if(sizeof($coords))
    {
        array_push($coords, $x1);
        array_push($coords, $y1);
        array_push($coords, $x2);
        array_push($coords, $y2);
    }
    else
    {
        echo $x1.','.$x2.','.$y1.','.$y2;
        $coords = array($x1, $y1, $x2, $y2);
    }
}

    if (isset($ts_x) || ($_SERVER['QUERY_STRING'] != ""))
    {
        if (isset($ts_x))
        {
            $clickedX = $ts_x;
            $clickedY = $ts_y;
        }
        else
        {
            list($clickedX, $clickedY) = split(",", $_SERVER['QUERY_STRING'], 2);
        }

        require 'inc.php';
        $allowed = false;

        $allowed = inCoord('circ',$coords, $clickedX, $clickedY);
        if($allowed)
        { 
            ?>
            <html>
                <head>
                    <title>PHP Image Map</title>
                </head>
                <body bgcolor="#00FFC8">
                    <p align="center">
                        <b> Aye Eye you got in!</b>
                        <br>
                        <a href="index.php"> Click Here to Go to main</a>
                    </p>
                </body>
            </html>
            <?php
        }
        else
        {
            //include 'miss_it.inc';
            ?>
            <html>
                <head>
                    <title>PHP Image Map</title>
                </head>
                <body bgcolor="#8BCBF8">
                    <form METHOD="POST" action="">
                        <p align="center">
                            <span style="cursor:default;">
                                <input type="image" src="<?php echo $img_STR; ?>" name="ts">
                            </span>
                        </p>
                    </form>
                </body>
            </html>
            <?php
        }
   }
   else
   { 
        ?>
        <html>
                <head>
                    <title>PHP Image Map</title>
                </head>
                <body bgcolor="#8BCBF8">
                    <form METHOD="POST" action="">
                        <p align="center">
                            <span style="cursor:default;">
                                <input type="image" src="<?php echo $img_STR; ?>" name="ts">
                            </span>
                        </p>
                    </form>
                </body>
            </html>
        <?php
    } 


?>

0 个答案:

没有答案