GD库弧光滑度

时间:2018-03-28 09:29:08

标签: php antialiasing smoothing

我已尝试使用上面的代码创建具有透明白色背景的圆弧,但它总是以扭曲的方式显示。请帮我如何使它们光滑弯曲?

$im = imagecreatetruecolor(300, 300);
$backgroundColor = imagecolorallocatealpha($im, 255, 255, 255, 127);
imagefill($im, 0, 0, $backgroundColor);
$white = imagecolorallocate($im, 255,255,255);
imagecolortransparent($im, $white);
    // Allocate A Color For The Text
    $dark_blue = imagecolorallocate($im, 30, 65, 71);
    imagesetthickness($im, 2);
    //creation of thick arc
    $thickness = 40;
    for ($i = $thickness; $i > 0; $i--) {
        imagearc($im, 150, 220, 180 - $i, 180 - $i,  180, 0, $dark_blue);
    }

1 个答案:

答案 0 :(得分:0)

增加图像尺寸和弧形厚度,然后逐渐使弧光滑度保持抗锯齿。我在下面添加我的代码,用于半圈/半甜甜圈图像/半圈: enter image description here

function scoreHalfDoughnutImage()
{   
    $score = 70;
    $maxScore = 100;
    $color = '#FC950C';
    $imagePath = "doughnutContent2Image_".$score.".jpg";

    // Create Image From Existing File
    $im = imagecreatetruecolor(550, 340);// Create a 550x340 image
    //imagealphablending($img,true);

    $backgroundColor = imagecolorallocatealpha($im, 255, 255, 255, 127);//blending colors
    imagefill($im, 0, 0, $backgroundColor);//creating white background
    // Switch antialiasing on for one image
    $white = imagecolorallocate($im, 255,255,255);
    $black = imagecolorallocate($im, 0, 0, 0);
    // for making transparent bg of white bg
    imagecolortransparent($im, $white);

    // Allocate A Color For The Text
    $orange = imagecolorallocate($im, 252, 149, 12);
    $dark_blue = imagecolorallocate($im, 30, 65, 71);
    imagesetthickness($im, 4);

    //creation of thick arc
    $thickness = 40;
    for ($i = $thickness; $i > 0; $i--) {
        imagearc($im, 275, 273, (180 - $i)*3, (180 - $i)*3,  180, 0, $dark_blue);
        imagearc($im, 275, 273, (180 - $i)*3, (180 - $i)*3, 180, 180+($score*1.8), $orange);
    }
//imageantialias($im, true);
    imagesetthickness($im, 1);
    $text='NA';
    // Setup the title on the center circle
    if($score>=0)
        $text=$score;

    // Set Path to Font File
    $font_path = 'lib/reports/MotivationEvaluation/font/arial.ttf';
    // Print Text On Image
    $textWidth = imagettfbbox(40, 0, $font_path, $text);
    $x = 235;
    if($textWidth[2] < 84)
    {
        $x = 250;
    }
    if($text!=='NA')
    {
        imagettftext($im, 45, 0, $x, 240, $dark_blue, $font_path, $text);
    }
    else
    {
        imagettftext($im, 35, 0, $x, 240, $dark_blue, $font_path, $text);
    }
    // Print Text On x-axis n y-axis
    imagettftext($im, 32, 0, 25, 315, $dark_blue, $font_path, 0);
    imagettftext($im, 32, 0, 25+450, 315, $dark_blue, $font_path, 100);
    //imagealphablending($im, true); //not needed as we created the image 
with alpha
    imagesavealpha($im, true);
    // Send Image to Browser
    imagejpeg($im,$imagePath);
    imagedestroy($im);
    return $imagePath;
}