[英]Draw a line with thickness in a image?
I want to draw a line with thickness in a image with PHP using GD library. 我想使用GD库在PHP的图像中绘制一条粗细的线。 I have found some solutions in this page PHP: imageline - Manual but none of them seems to work correctly when the (x, y) position of the line changes. 我在此页面PHP中找到了一些解决方案:imageline-手动,但是当线的(x,y)位置更改时,它们似乎都无法正常工作。
There are 3 functions I found in the page 我在页面中找到了3个功能
function dickelinie($img, $start_x, $start_y, $end_x, $end_y, $color, $thickness)
{
$angle = (atan2(($start_y - $end_y), ($end_x - $start_x)));
$dist_x = $thickness * (sin($angle));
$dist_y = $thickness * (cos($angle));
$p1x = ceil(($start_x + $dist_x));
$p1y = ceil(($start_y + $dist_y));
$p2x = ceil(($end_x + $dist_x));
$p2y = ceil(($end_y + $dist_y));
$p3x = ceil(($end_x - $dist_x));
$p3y = ceil(($end_y - $dist_y));
$p4x = ceil(($start_x - $dist_x));
$p4y = ceil(($start_y - $dist_y));
$array = array(0 => $p1x, $p1y, $p2x, $p2y, $p3x, $p3y, $p4x, $p4y);
imagefilledpolygon ($img, $array, (count($array) / 2), $color);
}
function imagelinethick($image, $x1, $y1, $x2, $y2, $color, $thick = 1)
{
if ($thick == 1)
{
return imageline($image, $x1, $y1, $x2, $y2, $color);
}
$t = $thick / 2 - 0.5;
if ($x1 == $x2 || $y1 == $y2)
{
return imagefilledrectangle($image, round(min($x1, $x2) - $t), round(min($y1, $y2) - $t), round(max($x1, $x2) + $t), round(max($y1, $y2) + $t), $color);
}
$k = ($y2 - $y1) / ($x2 - $x1);
$a = $t / sqrt(1 + pow($k, 2));
$points = array(
round($x1 - (1+$k)*$a), round($y1 + (1-$k)*$a),
round($x1 - (1-$k)*$a), round($y1 - (1+$k)*$a),
round($x2 + (1+$k)*$a), round($y2 - (1-$k)*$a),
round($x2 + (1-$k)*$a), round($y2 + (1+$k)*$a),
);
imagefilledpolygon($image, $points, 4, $color);
return imagepolygon($image, $points, 4, $color);
}
function imagelinethick1($image, $x1, $y1, $x2, $y2, $color, $thick = 1)
{
imagesetthickness($image, $thick);
imageline($image, $x1, $y1, $x2, $y2, $color);
}
And my test case is 我的测试用例是
header("Content-Type: image/png");
$image = @imagecreatetruecolor(500, 500) or die("Cannot initialize new GD image stream");
$color = imagecolorallocate($image, 255, 255, 255);
# Line thickness equals to 18 pixels
$thickness = 18;
# OK
dickelinie($image, 0, 0, 0, 500, $color, $thickness);
# Wrong: The thickness of the line is doubled
dickelinie($image, 200, 0, 200, 500, $color, $thickness);
# Wrong: The thickness of the line is halved
imagelinethick($image, 0, 0, 0, 500, $color, $thickness);
# OK
imagelinethick($image, 200, 0, 200, 500, $color, $thickness);
# Wrong: The thickness of the line is halved
imagelinethick1($image, 0, 0, 0, 500, $color, $thickness);
# OK
imagelinethick1($image, 200, 0, 200, 500, $color, $thickness);
imagepng($image);
imagedestroy($image);
Can anybody tell me what is wrong please? 有人可以告诉我哪里出了问题吗?
php.net's example #1 for imageline
is "Drawing a thick line". php.net针对imageline
的示例#1是“绘制粗线”。 It suggests using either imagesetthickness
or the following code: 建议使用imagesetthickness
或以下代码:
<?php
function imagelinethick($image, $x1, $y1, $x2, $y2, $color, $thick = 1)
{
/* this way it works well only for orthogonal lines
imagesetthickness($image, $thick);
return imageline($image, $x1, $y1, $x2, $y2, $color);
*/
if ($thick == 1) {
return imageline($image, $x1, $y1, $x2, $y2, $color);
}
$t = $thick / 2 - 0.5;
if ($x1 == $x2 || $y1 == $y2) {
return imagefilledrectangle($image, round(min($x1, $x2) - $t), round(min($y1, $y2) - $t), round(max($x1, $x2) + $t), round(max($y1, $y2) + $t), $color);
}
$k = ($y2 - $y1) / ($x2 - $x1); //y = kx + q
$a = $t / sqrt(1 + pow($k, 2));
$points = array(
round($x1 - (1+$k)*$a), round($y1 + (1-$k)*$a),
round($x1 - (1-$k)*$a), round($y1 - (1+$k)*$a),
round($x2 + (1+$k)*$a), round($y2 - (1-$k)*$a),
round($x2 + (1-$k)*$a), round($y2 + (1+$k)*$a),
);
imagefilledpolygon($image, $points, 4, $color);
return imagepolygon($image, $points, 4, $color);
}
?>
Some of your test cases are wrong. 您的某些测试用例是错误的。 Every test case that renders with x = 0 will be cut in half as the routines will centre the new thicker line onto the given xy coordinates. x = 0渲染的每个测试用例都将切成两半,因为例程会将新的粗线居中到给定的xy坐标上。 Thus half of the rendered line is rendered outside of the bitmap and is lost. 因此,渲染线的一半在位图之外渲染并丢失。
Given that your test cases are a little flawed we can see that the "dickelinie" method always doubles the expected thickness. 鉴于您的测试用例有少许缺陷,我们可以看到“ dickelinie”方法总是使预期厚度加倍。
I still used the "dickelinie" method for rendering a simple analog clock as it produced the best results for my needs. 我仍然使用“ dickelinie”方法来渲染一个简单的模拟时钟,因为它产生了我需要的最佳结果。 I just passed it half the thickness to ensure it worked. 我只是通过了一半的厚度,以确保它能正常工作。 I also added the following code onto the end of the "dickelinie" method to ensure that the resulting line was rendered anti-aliased when told to be: 我还在“ dickelinie”方法的末尾添加了以下代码,以确保在被告知时,结果行被抗锯齿处理:
return imagepolygon($img, $array, (count($array) / 2), $color);
You will want this if you need to render the lines at different angles. 如果需要以不同角度渲染线,则将需要此。
This is not the fastest way to solve your problem, but I use this function in a project of mine: 这不是解决问题的最快方法,但是我在我的项目中使用了此功能:
function thickline( $img, $x1, $y1, $x2, $y2, $color, $thickness ) {
$radius = $thickness * .5;
$vx = $x2 - $x1;
$vy = $y2 - $y1;
$steps = ceil( .5 + max( abs($vx), abs($vy) ) );
$vx /= $steps;
$vy /= $steps;
$x = $x1;
$y = $y1;
while( $steps --> 0 ) {
imagefilledellipse( $img, $x, $y, $radius, $radius, $color );
$x += $vx;
$y += $vy;
}
}
The idea is to plot a circle on each point of a line. 想法是在直线的每个点上绘制一个圆。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.