Conversion tables A-K
  Conversion tables L-Z
  SPAM database query
  Reverse WHOIS utility
  North America area codes
  PHP Example forums

PHP example repository.

Web www.web-max.ca
Back to examples
Drawing a rounded rectangle in PHP with GD

Currently GD does not provide a method to draw a rounded filled rectangle - ie a rectangle with rounded edges.

This is not a difficult thing to accomplish and we now have a custom function to do this - imagefillroundedrect.

The code is very simple. First we draw 2 filled rectangles to form a "cross" shape.

We leave spaces in the corners for the rounded edged. To create the edges we draw 4 filled circles in the spaces.

And thats it! The value passed in $rad is the radius. $col is the color of the rectangle. $x,$y are the coordinates of the top left (0,0 being topmost ). $cx,$cy are the end coordinates.

<?php 

function imagefillroundedrect($im,$x,$y,$cx,$cy,$rad,$col)
{

// Draw the middle cross shape of the rectangle

    
imagefilledrectangle($im,$x,$y+$rad,$cx,$cy-$rad,$col);
    
imagefilledrectangle($im,$x+$rad,$y,$cx-$rad,$cy,$col);

    
$dia $rad*2;

// Now fill in the rounded corners

    
imagefilledellipse($im$x+$rad$y+$rad$rad*2$dia$col);
    
imagefilledellipse($im$x+$rad$cy-$rad$rad*2$dia$col);
    
imagefilledellipse($im$cx-$rad$cy-$rad$rad*2$dia$col);
    
imagefilledellipse($im$cx-$rad$y+$rad$rad*2$dia$col);
}

?>
 
Example