为GIF图像创建多边形轮廓

时间:2012-02-13 20:16:29

标签: php image-processing gif php-gd

我正在使用imagefilledpolygon在GIF图像(PHP)上绘制箭头,我想将箭头标记为黑色。我首先为箭头创建一个三角形,然后在三角形的底部画一条粗线以获得箭头效果。

我认为一个解决方案是创建两个三角形箭头,第一个是黑色,略大于第一个(创建轮廓效果),但我不确定如何进行数学计算。

任何帮助都会很棒!

创建箭头(头部和尾部)的代码:

function arrow($img, $x1, $y1, $x2, $y2, $alength, $awidth, $color) {
    $distance = sqrt(pow($x1 - $x2, 2) + pow($y1 - $y2, 2));

    $dx = $x2 + ($x1 - $x2) * $alength / $distance;
    $dy = $y2 + ($y1 - $y2) * $alength / $distance;

    $k = $awidth / $alength;

    $x2o = $x2 - $dx;
    $y2o = $dy - $y2;

    $x3 = $y2o * $k + $dx;
    $y3 = $x2o * $k + $dy;

    $x4 = $dx - $y2o * $k;
    $y4 = $dy - $x2o * $k;

    imagelinethick($img, $x1, $y1, $dx, $dy, $color,3);
    return imagefilledpolygon($img, array($x2, $y2, $x3, $y3, $x4, $y4), 3, $color);
}

为箭头线调用此函数(仅供参考):

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

1 个答案:

答案 0 :(得分:0)

绘制稍大的箭头会有问题,因为它不会在每个点都变大。您可以在轮廓颜色中绘制箭头8次,对于8个方向中的每个方向偏移1个像素,然后在其上绘制最终形状。

相关问题