繁体   English   中英

PHP gd库,如何在现有的PNG图像上画线

[英]PHP gd library, how to draw line on existing PNG image

也许有人可以帮助我,因为我被困在这里。 所以我有560x225大小的zem.png图片。 它具有透明的背景。 我需要使用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个块来放置其他图像,这就是我生成这些无用块的方式:

$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;

Okey我现在有一行坐标。 还有我被卡住的地方。 我尝试了很多例子,例如人们预先创建的funkcion和ect。 但是我仍然无法使用php gd库画一条线。 所以也许有人可以提出一些建议? 要在创建图像的代码中添加一些内容,或简单地将其删除并保留空白的透明图像并打开它,然后画一条线...

制作表格或地图图像时,我总是准备一个类和函数。 我不确定是否有帮助,但这是我的示例代码。您可以将其命名为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">

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM