# ImageCropper

> Applies the crop rectangle stored by the Cropper.js editor to an image on the server with GD.

[API reference](/docs/api/index.md) › [FilePond](/docs/extensions/filepond/reference/index.md) › [file](/docs/extensions/filepond/reference/file/index.md) › ImageCropper

| final readonly class | `yii2\extensions\filepond\file\ImageCropper` |
| --- | --- |
| Source | [`src/file/ImageCropper.php`](https://github.com/yii2-extensions/filepond/blob/main/src/file/ImageCropper.php#L40) |

Applies the crop rectangle stored by the Cropper.js editor to an image on the server with GD.

Use it when the Image Transform plugin is disabled, so the browser uploads the original image and the server
produces the cropped file from `metadata.crop.rect`. JPEG orientation metadata is honored before cropping when the
`exif` extension is available, matching the orientation the editor displayed. Source byte, axis, and pixel limits
are required; configure them from the application's processing budget. They do not impose a hard process-memory cap.

## Methods

| Method | Description | Defined by |
| --- | --- | --- |
| [`__construct()`](#method-__construct) |  | `ImageCropper` |
| [`crop()`](#method-crop) | Returns a copy of the file cropped to its [`EncodedFile::getCropRectangle()`](/docs/extensions/filepond/reference/file/encodedfile/index.md#method-getCropRectangle) rectangle. | `ImageCropper` |

## Method details

### __construct() <span id="method-__construct"></span>

```php
public function __construct(int $maxPixels, int $maxDimension, int $maxInputBytes, int $quality = 90)
```

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$maxPixels` | `int` | Maximum source pixel count before decoding. |
| `$maxDimension` | `int` | Maximum source width or height, in pixels. |
| `$maxInputBytes` | `int` | Maximum source byte count before header parsing. |
| `$quality` | `int` | Output quality for lossy formats, from `0` to `100`. |

**Throws**

- [`InvalidArgumentException`](https://www.php.net/manual/en/class.invalidargumentexception.php): if any processing limit is not positive.
- [`RuntimeException`](https://www.php.net/manual/en/class.runtimeexception.php): if the `gd` extension is not loaded.

### crop() <span id="method-crop"></span>

```php
public function crop(yii2\extensions\filepond\file\EncodedFile $file): yii2\extensions\filepond\file\EncodedFile
```

Returns a copy of the file cropped to its [`EncodedFile::getCropRectangle()`](/docs/extensions/filepond/reference/file/encodedfile/index.md#method-getCropRectangle) rectangle.

The rectangle is intersected with the image bounds, keeping at least the nearest edge pixel when they do not
overlap. GIF, JPEG, PNG, and WebP keep their format; other formats, such as BMP, are encoded as PNG and the copy
receives the `image/png` type and a `.png` extension. Files without a rectangle are returned unchanged.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$file` | [`EncodedFile`](/docs/extensions/filepond/reference/file/encodedfile/index.md) | Image file carrying crop metadata. |

**Returns**

[`EncodedFile`](/docs/extensions/filepond/reference/file/encodedfile/index.md): Cropped copy, or the same instance when no crop rectangle is present.

**Throws**

- [`RuntimeException`](https://www.php.net/manual/en/class.runtimeexception.php): if the source exceeds a processing limit or cannot be decoded or encoded.