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

# Image::fit

## Cut out and resize a rectangular part of the current image



> public Intervention\Image\Image fit(int $width, [[int $height], [Closure $callback, [string $position]]])

Combine cropping and resizing to format image in a smart way. The method will find the best fitting aspect ratio of your given **width and height** on the current image automatically, cut it out and resize it to the given dimension. You may pass an optional Closure **callback** as third parameter, to prevent possible upsizing and a custom **position** of the cutout as fourth parameter.

### Parameters

#### width
The width the image will be resized to after cropping out the best fitting aspect ratio.

#### height (optional)
The height the image will be resized to after cropping out the best fitting aspect ratio. If no height is given, method will use same value as width.

#### callback (optional)
Closure callback defining constraint to prevent unwanted **upsizing** of the image. See examples below.

> public Intervention\Image\Size upsize()

Keep image from being upsized.

#### position (optional)
Set a position where cutout will be positioned. By default the best fitting aspect ration is centered.

The possible values are:

- top-left
- top
- top-right
- left
- center (default)
- right
- bottom-left
- bottom
- bottom-right


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

### Examples

```php
// open file a image resource
$img = Image::make('public/foo.jpg');

// crop the best fitting 5:3 (600x360) ratio and resize to 600x360 pixel
$img->fit(600, 360);

// crop the best fitting 1:1 ratio (200x200) and resize to 200x200 pixel
$img->fit(200);

// add callback functionality to retain maximal original image size
$img->fit(800, 600, function ($constraint) {
    $constraint->upsize();
});
```

### See also

- [resize](https://image.intervention.io/v2/api/resize.md)
- [resizeCanvas](https://image.intervention.io/v2/api/resize-canvas.md)
- [crop](https://image.intervention.io/v2/api/crop.md)
- [trim](https://image.intervention.io/v2/api/trim.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)