2015-10-05 2 views
3

Я пытаюсь сделать это изображение:GD Жеребьевка прямоугольник с радиусом

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

Так как я не знаю ваших требований здесь мои два цента:

Ваше предлагаемое решение черпает полный прямоугольник с закругленными углами. Но в большинстве ситуаций вы хотите иметь круглые края на заданном изображении. (Предположим, ваше входное изображение - только один цвет на данный момент, но это может измениться, и это может быть фотография)

Итак, вы хотите добавить 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(); 
+0

Im рад, что вы пытаетесь помочь, но я просто попытался нарисовать 12x100 прямоугольник, и я увидел, что это не нужно скругленные углы, это настолько маленькое изображение, которое, похоже, он имеет закругленные углы .. –

+0

@ ArnasPečelis, код, который я дал выше работает сейчас;) –

Смежные вопросы