Home | General Posts | Tutorials
PHP function to draw a regular rectangle is fairly easy to use:
imagefilledrectangle( $image, $x1, $y1, $x2, $y2, $color );
or for a non-filled rectangle:
imagerectangle( $image, $x1, $y1, $x2, $y2, $color );
where $image can be an image created from any of the image creation functions like:
$image = imagecreate( $width, $height );
or image from a file:
$image = imagecreatefromstring( file_get_contents( "path/to/picture.png" ) );
To draw a rounded cornered rectangle with PHP, we will use the following php function:
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);
}
In this function above, following parameters need to be provided:
1. $x1 -> This is the x position of the rectangle, where it starts horizontally
2. $x2 -> This is the x position, where rectangle ends horizontally
3. $y1 -> This is the y position where rectangle starts vertically
4. $y2 -> This is the y position where rectangle ends vertically
Now lets set the "radius" to draw rounded corner rectangle
$radius -> This sets the radius in the function above.
Radius needs to be "half" the difference between $y2 and $y1
Suppose your rectangle starts at a height of 50 px, and ends at 100 px, vertically, then radius will be:
$radius = ( $y2 - $y1 )/2
$radius = ( 100 - 50 )/2
$radius = 25
Now lets give those values to our PHP function and see what happens
$x1=50, $x2=200, $y1=50, $y2=100, $radius=25
Lets try some other values
$x1=150, $x2=200, $y1=50, $y2=200, $radius=25
For each of the images above, I used a white color for $color.
Color can be defined by any of the color functions of PHP
$color = imagecolorallocate( $image, 255, 255, 255 );
or with alpha supported color functions
$color = imagecolorallocatealpha( $image, 255, 255, 255, 60 );
This is how easy it is to draw a rectangle with rounded corners in PHP.
PHP function to draw rectangle with rounded corners
This PHP function can draw a rectangle with rounded corners
PHP function to draw a regular rectangle is fairly easy to use:
imagefilledrectangle( $image, $x1, $y1, $x2, $y2, $color );
or for a non-filled rectangle:
imagerectangle( $image, $x1, $y1, $x2, $y2, $color );
where $image can be an image created from any of the image creation functions like:
$image = imagecreate( $width, $height );
or image from a file:
$image = imagecreatefromstring( file_get_contents( "path/to/picture.png" ) );
Now lets round the corners of that rectangle
To draw a rounded cornered rectangle with PHP, we will use the following php function:
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);
}
How to use this function to draw rectangle with round corners
In this function above, following parameters need to be provided:
1. $x1 -> This is the x position of the rectangle, where it starts horizontally
2. $x2 -> This is the x position, where rectangle ends horizontally
3. $y1 -> This is the y position where rectangle starts vertically
4. $y2 -> This is the y position where rectangle ends vertically
Now lets set the "radius" to draw rounded corner rectangle
$radius -> This sets the radius in the function above.
Radius needs to be "half" the difference between $y2 and $y1
Suppose your rectangle starts at a height of 50 px, and ends at 100 px, vertically, then radius will be:
$radius = ( $y2 - $y1 )/2
$radius = ( 100 - 50 )/2
$radius = 25
Now lets give those values to our PHP function and see what happens
$x1=50, $x2=200, $y1=50, $y2=100, $radius=25
Lets try some other values
$x1=150, $x2=200, $y1=50, $y2=200, $radius=25
For each of the images above, I used a white color for $color.
Color can be defined by any of the color functions of PHP
$color = imagecolorallocate( $image, 255, 255, 255 );
or with alpha supported color functions
$color = imagecolorallocatealpha( $image, 255, 255, 255, 60 );
This is how easy it is to draw a rectangle with rounded corners in PHP.
You might also be interested in :
Posts
- Best Video Screen Recorder
- Logo Banner Image Maker
- Best Photo Slideshow Maker
- Free Box Cover Maker
- Free Ebook Cover Maker
- Games For Blind
Posted in