2013-04-08 3 views
0

GD 라이브러리를 사용하여 PHP로 이미지의 두께와 선을 그려야합니다. 나는이 페이지에서 몇 가지 해결책을 찾았으나 줄의 (x, y) 위치가 바뀌면 제대로 작동하지 않는 것으로 보인다.이미지에 두께가있는 선을 그립니다.

가 나는 페이지

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

에서 발견 된 3 개 기능은 다음과 같습니다 그리고 내 테스트 케이스는

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

imageline에 대한 php.net의 예제 # 1은 "두꺼운 선 그리기"입니다. 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

+0

@ user433531 글쎄, 당신의 코드가 같지 않아. – Pietu1998

+0

글쎄, 그들은 동일합니다. 내 코드에서 나는 단지 주석을 제거하고 코드를 다시 들여 썼다. – Teiv

0

일부 테스트 케이스가 잘못되었습니다. x = 0으로 렌더링하는 모든 테스트 케이스는 루틴이 새로운 두꺼운 선을 지정된 xy 좌표로 가운데 맞추기 때문에 반으로 절단됩니다. 따라서 렌더링 된 선의 절반이 비트 맵 외부로 렌더링되어 손실됩니다.

테스트 케이스에 약간의 결함이있는 경우 "dickelinie"메소드가 항상 예상 두께를 두 배로 늘리는 것을 볼 수 있습니다.

나는 여전히 내 요구에 가장 적합한 결과를 산출하는 간단한 아날로그 시계를 렌더링하는 데 "dickelinie"방법을 사용했습니다. 방금 작동하도록 두께의 절반을 통과 시켰습니다.

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

당신이 원하는 것 당신의 라인을 렌더링해야하는 경우 : 나는 또한으로 말했을 때 그 결과 선이 앤티 앨리어싱 렌더링되었는지 확인하기 위해 "dickelinie"방법의 끝 부분에 다음 코드를 추가 다른 각도.

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

아이디어는 라인의 각 지점에 원을 그릴 것입니다.

관련 문제