首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PHP gd库,如何在现有的PNG图像上绘制线条

PHP gd库,如何在现有的PNG图像上绘制线条
EN

Stack Overflow用户
提问于 2012-05-02 19:33:29
回答 1查看 1.8K关注 0票数 0

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

下面是我的透明图像是如何创建的:

代码语言:javascript
复制
// 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个块来放置其他图像,下面是我如何生成这些随机块的方法:

代码语言:javascript
复制
$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,以精确地知道直线起点和终点的正确坐标。所以我这样做:

代码语言:javascript
复制
// Breziame kelius
$kordinate = $x * 16;
$kordinate2 = $y * 9;
$kordinate3 = $x2 * 16;
$kordinate4 = $y2 * 9;

好的,我现在有一条线的坐标了。这就是我被卡住的地方。我尝试了很多例子,比如人们预先创造的funkcions等等。但是我仍然不能使用php gd库来划清界限。那么也许有人可以提点建议呢?添加一些东西到图像创建代码中,或者简单地删除它,只留下空白的透明图像,打开它,然后画一条线……

EN

回答 1

Stack Overflow用户

发布于 2014-05-02 10:11:31

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

代码语言:javascript
复制
 <?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">
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10412895

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档