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 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:
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.