---
application: "Intervention Image"
version: "Version 2"
status: "eol"
---

# Image::rectangle

## Draw a rectangle to the current image



> public Intervention\Image\Image rectangle(int $x1, int $y1, int $x2, int $y2, [Closure $callback])

Draw a colored rectangle on current image with top-left corner on **x,y point 1** and bottom-right corner at **x,y point 2**. Define the overall appearance of the shape by passing a Closure **callback** as an optional parameter.

### Parameters

#### x1
x-coordinate of the top-left point of the rectangle.

#### y1
y-coordinate of the top-left point of the rectangle.

#### x2
x-coordinate of the bottom-right point of the rectangle.

#### y2
y-coordinate of the bottom-right point of the rectangle.

#### callback (optional)
Define appearance of rectangle. See examples below. Use the following methods to pass details.

> public Intervention\Image\AbstractShape background(string $color)

Define the background-color of the rectangle in one of the available [color formats](https://image.intervention.io/v2/getting-started/formats.md).

> public Intervention\Image\AbstractShape border(integer $width, string $color)

Define the border of the rectangle. Set width as pixels in the first and the border-color in one of the available [color formats](https://image.intervention.io/v2/getting-started/formats.md) as second parameter.


### Return Values
Instance of `Intervention\Image\Image`

### Examples

```php
// create empty canvas with background color
$img = Image::canvas(100, 100, '#ddd');

// draw an empty rectangle border
$img->rectangle(10, 10, 190, 190);

// draw filled red rectangle
$img->rectangle(5, 5, 195, 195, function ($draw) {
    $draw->background('#ff0000');
});

// draw transparent rectangle with 2px border
$img->rectangle(5, 5, 195, 195, function ($draw) {
    $draw->background('rgba(255, 255, 255, 0.5)');
    $draw->border(2, '#000');
});
```

### See also

- [pixel](https://image.intervention.io/v2/api/pixel.md)
- [line](https://image.intervention.io/v2/api/line.md)
- [circle](https://image.intervention.io/v2/api/circle.md)
- [ellipse](https://image.intervention.io/v2/api/ellipse.md)
- [polygon](https://image.intervention.io/v2/api/polygon.md)

---

## Become a Sponsor

### Intervention Image needs your help to keep the project going

Intervention Image is non-commercial, open source licensed and completely free to use. The considerable
effort required to maintain and develop the software is only possible with the financial support
of sponsors. There are two ways in which you can support this project.

- Support via [GitHub Sponsors](https://github.com/sponsors/Intervention)
- Support via [Ko-Fi](https://ko-fi.com/interventionphp)