0

也许有人可以帮助我,因为我被困在这里。所以我有 zem.png 大小为 560x225 的图像。它具有透明背景。我需要用 gd 库从 x,y 到 x1,x2 画线(我需要画 5-7 条线,但如果能得到一个如何画一条线的简单例子就完美了)。

这是我的透明图像的创建方式:

// Kuriame paveiksleli pasinaudodami GD library keliams tarp miestu pavaizduoti
$im = imagecreatetruecolor(560, 225);
$red = imagecolorallocate($im, 255, 0, 0);
$black = imagecolorallocate($im, 0, 0, 0);

// Make the background transparent
imagecolortransparent($im, $black);

// Issaugome paveiksleli
imagepng($im, './zem.png');
imagedestroy($im);

创建此图像后,我将其用作表格或 div 的背景图像。我的桌子 ir div 被分成 x - 35 个块,y 25 个块。

我选择了 4 个点或 4 个块来放置其他图像,这就是我生成这些 rondom 块的方式:

$x = rand(1, 35);
$y = rand(1, 25);
$x2 = rand(1, 35);
$y2 = rand(1, 25);
$x3 = rand(1, 35);
$y3 = rand(1, 25);
$x4 = rand(1, 35);
$y4 = rand(1, 25);

单个块大小为 16x9。而且我需要从每个生成的块到其他块绘制一条线(作为代表城市的道路),所以我必须将我的 x (x2,x3..) 乘以 16 和我的 y 乘以 9 才能准确地知道线的正确坐标起点和终点。所以我这样做:

// Breziame kelius
$kordinate = $x * 16;
$kordinate2 = $y * 9;
$kordinate3 = $x2 * 16;
$kordinate4 = $y2 * 9;

好的,我现在有一条线的坐标。这就是我被困住的地方。我尝试了很多例子,人们预先创建了 funkcions 等。但我仍然无法使用 php gd 库画一条线。所以也许有人可以提出一些建议?要在图像创建代码中添加一些东西,或者只是将其删除并留下空白透明图像并打开它然后画一条线......

4

1 回答 1

1

当我制作表格或地图图像时,我总是准备一个类和函数。我不确定它是否有帮助,但这是我的示例代码。您可以将此文件命名为 GridTb.php 并运行它。

 <?php
header('Content-type: image/png');
$GridTb = new GridTb();
$GridTb->pngfile(330, 700);

class GridTb {

    function pngfile($width, $height) {

        define("WIDTH", $width);
        define("HEIGHT",$height);


        $png_image = imagecreate(WIDTH, HEIGHT);

        imagecolorallocate($png_image, 255, 255, 255);
        imagesetthickness($png_image, 1);
        $black = imagecolorallocate($png_image, 0, 0, 0);

        $x = 0;
        $y = 0;
        $w = imagesx($png_image) - 1;
        $z = imagesy($png_image) - 1;
        //basic square frame
        imageline($png_image, $x, $y, $x, $y + $z, $black);
        imageline($png_image, $x, $y, $x + $w, $y, $black);
        imageline($png_image, $x + $w, $y, $x + $w, $y + $z, $black);
        imagerectangle($png_image, $x, $y + $z, $x + $w, $y + $z, $black);

        $wid = 30;
        // $h=40;
        for ($row = 0; $row < 10; $row++) {

            imageline($png_image, $wid, HEIGHT, $wid, 0, $black);
            $wid+=30;
            imageline($png_image, $wid, HEIGHT, $wid, 0, $black);

            for ($h = 40; $h < 701; $h++) {

                $h2 = array(60,200,150,150,100);
                imageline($png_image, WIDTH, $h, 0, $h, $black);
                $h+=60;
                imageline($png_image, WIDTH, $h, 0, $h, $black);
                $h+=200;
                imageline($png_image, WIDTH, $h, 0, $h, $black);
                $h+=150;
                imageline($png_image, WIDTH, $h, 0, $h, $black);
                $h+=150;
                imageline($png_image, WIDTH, $h, 0, $h, $black);
                $h+=100;
                imageline($png_image, WIDTH, $h, 0, $h, $black);
                //sum of $h = 700
            }
        }
        imagepng($png_image);
        imagedestroy($png_image);
    }

}

?>
<IMG src="GridTb.php">
于 2014-05-02T02:11:30.880 回答