2013-04-08 137 views
0

我想在使用GD库的PHP中使用厚度绘制线条。我在这个页面找到了一些解决方案PHP: imageline - Manual,但是当线条的(x,y)位置改变时,它们都不能正常工作。在图像中画一条粗细的线条?

有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); 
} 

发现我的测试用例是

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

谁能告诉我什么是错的吗?

回答

0

php.net的例子#1为imageline是“绘制一条粗线”。它建议使用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); 
} 
?> 
+0

谢谢,但我已经在上面的问题中发布了这一点,它不能像描述的那样正常工作。 – Teiv 2013-04-08 06:04:34

+0

@ user433531那么,你的代码不一样。 – Pietu1998 2013-04-08 06:10:26

+0

嗯,它们是一样的。在我的代码中,我只是删除了注释并重新缩进了代码。 – Teiv 2013-04-08 06:43:16

0

某些测试用例是错误的。每个用x = 0渲染的测试用例将被削减一半,因为这些例程会将新的粗线置于给定的xy坐标上。因此渲染线的一半呈现在位图之外并丢失。

鉴于您的测试案例有点缺陷,我们可以看到“dickelinie”方法总是使预期厚度翻倍。

我仍然使用“dickelinie”方法渲染一个简单的模拟时钟,因为它产生了我所需要的最佳结果。我只是通过它一半的厚度,以确保它的工作。我还添加以下代码到“dickelinie”方法的末尾,以确保当被告知是导致线路被渲染抗锯齿:

return imagepolygon($img, $array, (count($array)/2), $color); 

你会想这个,如果你需要渲染线,在不同的角度。

0

这不是解决你的问题的最快方法,但我在我的项目中使用此功能:

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

的想法是绘制在一条线上的每一个点的圆。