GD绘制具有半径的矩形

时间:2015-10-05 14:44:39

标签: php gd rounding

我正在尝试绘制此图片:

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

这是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它应该绘制完美的圆形图像..

1 个答案:

答案 0 :(得分:1)

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

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

因此,您希望为给定图像添加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();