dongshuo2752 2018-10-26 19:01
浏览 153

PHP GD绘制线宽> 1的虚线或虚线

Trying to draw a dashed or dotted line of width 1 gives the result I want, no problem.

But when I try it with a width bigger than 1 it results in lines being drawn in an unexpected pattern (example 1).

Then, when I try to be clever and draw n lines of width 1 I still get an unexpected pattern (example 2).

So how do I create a dashed (- - - - -) or dotted ( . . . . ) line of width > 1?

Example 1:

Example 1

Example 2:

example 2


Finally, I tried something else and it seems that I can render horizontal dashed lines by subtracting line count from the horizontal coordinate, but even this is not ideal as it doesn't render the last line properly.

Even so, is there really no better way to render a thick dashed line without having to draw single lines of 1 up to the tickness?

<?php

$size=[500,500];

$width=5;

$left=10; $right=200;
$top=60; $bottom=60;

$resImage=imagecreatetruecolor($size[0],$size[1]);

$intColour=imagecolorallocate($resImage,0xff,0xff,0);

imagesetthickness($resImage,1);

imagesetstyle(

    $resImage,

    [
        $intColour,$intColour,$intColour,$intColour,

        IMG_COLOR_TRANSPARENT,IMG_COLOR_TRANSPARENT,

        IMG_COLOR_TRANSPARENT,IMG_COLOR_TRANSPARENT
    ]

);

for($intLineCount=0; $intLineCount<$width; $intLineCount++) {

    imageline(

        $resImage,

        $left-$intLineCount,

        $top+$intLineCount,

        $right-$intLineCount,

        $bottom+$intLineCount,

        IMG_COLOR_STYLED
    );
}

imagepng($resImage,'/tmp/myimage.png');

This renders an almost perfect horizontally dashed line, but note the missing pixels at the bottom right corner of the line:

example 3

But try drawing a slanted line and again things go horribly wrong...

I am aware of the fact that there is a function called imagedashedline. It is however marked as obsolete. In fact, I am following the example on that page on php.net of how to do it without this function.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥100 为什么这个恒流源电路不能恒流?
    • ¥15 有偿求跨组件数据流路径图
    • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
    • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
    • ¥15 CSAPPattacklab
    • ¥15 一直显示正在等待HID—ISP
    • ¥15 Python turtle 画图
    • ¥15 关于大棚监测的pcb板设计
    • ¥15 stm32开发clion时遇到的编译问题
    • ¥15 lna设计 源简并电感型共源放大器