php - GD 绘制带半径的矩形

标签 php gd rounding

我正在尝试绘制此图像:

enter image description here (底部没有那两个黑色三角形)

这是 12x16 图像。我的代码:

private function __draw_percent_bar()
{
    if ( $this->program_details['roi'] > 100 ) {
        $this->program_details['roi'] = 100;
    }

    $this->__tmp_data['bar_image'] = imagecreatetruecolor(12, $this->program_details['roi']);
    $this->__tmp_data['bar_bg'] = imagecolorallocate($this->__tmp_data['bar_image'], 136, 183, 5);

    $this->__rounded_borders(12, $this->program_details['roi'], 12, $this->program_details['roi'], 3, $this->__tmp_data['bar_bg']);
}

private function __rounded_borders($x, $y, $cx, $cy, $rad, $col)
{
    imagefilledrectangle($this->__tmp_data['bar_image'], $x, $y + $rad, $cx, $cy - $rad, $col);
    imagefilledrectangle($this->__tmp_data['bar_image'], $x + $rad, $y, $cx - $rad, $cy, $col);

    $dia = $rad * 2;

    imagefilledellipse($this->__tmp_data['bar_image'], $x + $rad, $y + $rad, $rad * 2, $dia, $col);
    imagefilledellipse($this->__tmp_data['bar_image'], $x + $rad, $cy - $rad, $rad * 2, $dia, $col);
    imagefilledellipse($this->__tmp_data['bar_image'], $cx - $rad, $cy - $rad, $rad * 2, $dia, $col);
    imagefilledellipse($this->__tmp_data['bar_image'], $cx - $rad, $y + $rad, $rad * 2, $dia, $col);
}

但它吸引我的不是那个图像,而是这个:

enter image description here (这是放大版)

问题是什么?使用本教程 http://www.web-max.ca/PHP/misc_10.php它应该画出完美的圆形图像..

最佳答案

因为我不知道你的要求,这里是我的两分钱:

您提出的解决方案绘制了完整的圆角矩形。但在大多数情况下,您希望在给定图像上具有圆边。 (假设您的输入图像目前只是一种颜色。但这可能会改变,它可能是一张照片)

因此,您想为给定图像添加 4 个圆角。首先创建一个包含完整圆圈的图像。圆圈本身应该是透明的,背景是您想要的背景颜色。

class YourFancyImage
{
    $img = null;

    public function __construct($width, $height)
    {
        $this->img = imagecreatetruecolor($width, $height);
    }

    //takes the radius and the background color as arguments
    //radius in pixel
    //background color as hex. i.e. "00FFAA"
    public function RoundedCorner($radius, $backgroundColor)
    {
        //create the temporary circle image
        $imgCircle = imagecreate($radius * 2 + 1, $radius * 2 + 1);

        //extract the colors
        sscanf($backgroundColor, "%2x%2x%2x", $red, $green, $blue);

        $redTrans = $red;

        //search a transparent color
        while($redTrans == $red)
        {
            $redTrans = rand(0, 255);
        }

        //set the background color
        imagecolorallocate($imgCircle, $red, $green, $blue);

        //draw the transparent circle
        $color = imagecolorallocate($imgCircle, $redTrans, $green, $blue);
        imagecolortransparent($imgCircle, $color);
        imagefilledellipse($imgCircle, $radius, $radius, $radius*2, $radius*2, $color);

        //copy every quarter to the right place
        imagecopyresampled($this->img, $imgCircle, 0, 0, 0, 0, $radius, $radius, $radius, $radius);

        imagecopyresampled($this->img, $imgCircle, imagesx($this->img)-$radius, 0, $radius+1, 0, $radius, $radius, $radius, $radius);

        imagecopyresampled($this->img, $imgCircle, 0, imagesy($this->img)-$radius, 0, $radius+1, $radius, $radius, $radius, $radius);

        imagecopyresampled($this->img, $imgCircle, imagesx($this->img)-$radius, imagesy($this->img)-$radius, $radius+1, $radius+1, $radius, $radius, $radius, $radius);
    }

    public function Display()
    {
        header('Content-Type: image/png');

        imagepng($this->img);

        imagedestroy($this->img);
        exit;
    }
}

$test = new YourFancyImage(200, 150);
$test->RoundedCorner(50, "FF00FF");
$test->Display();

关于php - GD 绘制带半径的矩形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32951414/

相关文章:

php - 使用 PHP curl 出现 411 错误

javascript - 如何在javascript函数中弹出php(与html标签混合)文件

javascript - jquery 向这个被点击的元素添加类

php - 如何在调整图像大小时填充白色背景

php - 可旋转图像或带有 javascript 的 div?

JavaScript 舍入在 77.475 和 67.475 上失败

c++ - boost::format 给出的结果与 round 不同

php - 从 array_rand 函数的结果创建简码

php - 在 php 中去隔行扫描 png

swift - 四舍五入到 0.5