crop()

public function crop(int $width, int $height, string $trim = 'center'): void

Description

Crops a loaded image to the specified dimensions. The method extracts a portion of the current image based on the provided width, height, and trim position, then replaces the loaded image with the cropped version.

Load First, Then Crop: This method requires an image to be loaded first using load() or upload(). Calling crop() without a loaded image will result in an error.

Parameters

Parameter Type Description Default Required
$width int The desired width of the cropped image in pixels. N/A Yes
$height int The desired height of the cropped image in pixels. N/A Yes
$trim string Determines which part of the image to keep. Options: 'left', 'center', 'right'. 'center' No

Understanding the Trim Parameter

The $trim parameter controls which portion of the image is preserved when cropping to dimensions smaller than the original:

  • 'left': Crops from the top-left corner, preserving the left side of the image
  • 'center': (default) Crops from the center, preserving the middle portion of the image
  • 'right': Crops from the top-right corner, preserving the right side of the image

How Trim Works: When the crop dimensions are smaller than the current image, crop() calculates the offset to determine which pixels to extract. For 'center' trim, the offset is half the difference between current and target dimensions. For 'right' trim, the offset is the full difference. For 'left' trim, there is no offset (starts at 0,0).

Return Value

Type Description
void This method does not return a value. It modifies the loaded image resource in place.

Example #1

The code sample below demonstrates the most basic image cropping operation.

The Stateful Workflow: The Image module follows a load → manipulate → save pattern. Always load an image first with load(), then apply operations like crop(), and finally either save() to disk or output() to the browser. Each operation modifies the internal image resource, so operations are cumulative.

Example #2

The example above shows how to create a square profile picture by cropping an uploaded image.

Example #3

The example above demonstrates creating multiple crop variations with different trim positions.

Multiple Operations on Same Source: When creating multiple variations from one source image, load the original image fresh before each operation. Image operations are cumulative - if you crop once and then crop again, the second crop operates on the already-cropped image, not the original.

Example #4

The example above shows combining resize and crop operations to create a thumbnail with exact dimensions.

Resize Then Crop Pattern: For perfect thumbnails with exact dimensions, use the resize-then-crop pattern. First resize the image so its smallest dimension matches your target (using resize_to_width() or resize_to_height()), then crop to exact dimensions. This ensures no distortion while filling the target area completely.