Reham Fahmy
Reham Fahmy

Reputation: 5073

function for imagefilledrectangle with rounded corners

I'm willing to create an image with certain text using the following code

<?PHP
header('Content-Type: image/png');

$im = imagecreatetruecolor(320, 80);

$blue = imagecolorallocate($im, 59, 89, 152);
$sky = imagecolorallocate($im, 219, 241, 255);

imagefilledrectangle($im, 0, 0, 399, 79, $sky);

$font = 'arial.ttf';

$text = "Hello world"; 

imagettftext($im, 15, 0, 10, 20, $blue, $font, $text); 
imagepng($im);
imagedestroy($im);
?>

Now the out put will be as following image

this is rectangle

rectangle image

Now What if i want to made to it a rounded corner by refer to php manual about function imagefilledrectangle I've found a good comment with function said to be able to make it rounded corner

<?

function ImageRectangleWithRoundedCorners(&$im, $x1, $y1, $x2, $y2, $radius, $color)
{
// draw rectangle without corners
imagefilledrectangle($im, $x1+$radius, $y1, $x2-$radius, $y2, $color);
imagefilledrectangle($im, $x1, $y1+$radius, $x2, $y2-$radius, $color);
// draw circled corners
imagefilledellipse($im, $x1+$radius, $y1+$radius, $radius*2, $radius*2, $color);
imagefilledellipse($im, $x2-$radius, $y1+$radius, $radius*2, $radius*2, $color);
imagefilledellipse($im, $x1+$radius, $y2-$radius, $radius*2, $radius*2, $color);
imagefilledellipse($im, $x2-$radius, $y2-$radius, $radius*2, $radius*2, $color);
}

?>

But didn't mention how to use ! ~ anyhelp

Upvotes: 3

Views: 4631

Answers (3)

Andrej
Andrej

Reputation: 736

In case anybody needs the background behind the radius to be transparent, this function can be used (I modified the one posted by OP) =>

function image_rectangle_w_rounded_corners(&$im, $x1, $y1, $x2, $y2, $radius, $color) {
    $alpha = imagecolorallocatealpha($im, 0, 0, 0, 127);
    
    // draw rectangle without corners
    imagefilledrectangle($im, $x1+$radius, $y1, $x2-$radius, $y2, $color);
    imagefilledrectangle($im, $x1, $y1+$radius, $x2, $y2-$radius, $color);
    
    // draw circled corners
    imagefilledellipse($im, $x1+$radius, $y1+$radius, $radius*2, $radius*2, $color);
    imagefilledellipse($im, $x2-$radius, $y1+$radius, $radius*2, $radius*2, $color);
    imagefilledellipse($im, $x1+$radius, $y2-$radius, $radius*2, $radius*2, $color);
    imagefilledellipse($im, $x2-$radius, $y2-$radius, $radius*2, $radius*2, $color);
    
    // alpha radius bg
    $width = imagesx($im);
    $height = imagesy($im) - 0.01;
    
    imagefill($im, 0, 0, $alpha);
    imagefill($im, $width, 0, $alpha);
    imagefill($im, 0, $height, $alpha);
    imagefill($im, $width, $height, $alpha);
}

Of course - transparency can be switched for any other color.

Upvotes: 2

Sumit Kumar
Sumit Kumar

Reputation: 781

Its an old question, but may be someone will benefit from this answer. This is a very nice function! Here is how to use it:

x1 = the horizontal x point where you want rectangle to start
x2 = the horizontal x point where you want rectangle to end
y1= the vertical y point where rectangle starts
y2 = the vertical y point where rectangle ends

Now the radius. Set the radius to "half" of difference between y2 and y1

Suppose your y2 is 100, and y1 is 50, then radius will be:

( y2 - y1 )/2
( 100 - 50 )/2 = 25

Couple of examples are here: http://vagminetech.com/rect.htm

For a wide rectangle: x1=50 x2=250 y1=50 y2=100 r=25

For a tall rectangle x1=50 x2=150 y1=50 y2=150 r=50

Regards

Upvotes: 0

Saiyam Patel
Saiyam Patel

Reputation: 1161

let me try

probably

$x1 = top left point's x coordinate
$y1 = top left point's y coordinate
$x2 = bottom right point's x coordinate
$y2 = bottom right point's y coordinate
$radius = radius of the curve of rounded corner

set this data and try... good luck :)

Upvotes: 0

Related Questions