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);
누구 잘못하시기 바랍니다 무엇인지 말해 줄 수 있습니까?
고마워요.하지만 위의 질문에 이미 게시했는데 설명대로 올바르게 작동하지 않습니다. – Teiv
@ user433531 글쎄, 당신의 코드가 같지 않아. – Pietu1998
글쎄, 그들은 동일합니다. 내 코드에서 나는 단지 주석을 제거하고 코드를 다시 들여 썼다. – Teiv