resize_and_crop()

public function resize_and_crop(int $width, int $height): void

Description

Resizes and crops a loaded image to exact dimensions in a single operation. This method intelligently determines the optimal resize strategy based on aspect ratios, then crops to achieve the target dimensions precisely. The result is an image that exactly matches the specified width and height with no distortion.

Load First, Then Process: This method requires an image to be loaded first using load() or upload(). Calling resize_and_crop() without a loaded image will throw an Exception with the message "No image is loaded to resize and crop."

Parameters

Parameter Type Description Default Required
$width int The target width in pixels. N/A Yes
$height int The target height in pixels. N/A Yes

How It Works

The resize_and_crop() method automatically calculates the optimal strategy based on aspect ratios:

  • If aspect ratios match: Simply resizes to target dimensions
  • If target is wider: Resizes to target width, then crops height from centre
  • If target is taller: Resizes to target height, then crops width from centre

This intelligent approach ensures the image fills the target dimensions completely with minimal cropping, preserving as much of the original image as possible whilst maintaining the exact size requirements.

Single Operation Convenience: The resize_and_crop() method combines what would otherwise require separate resize_to_width()/resize_to_height() and crop() calls. It's particularly useful when you need images to be exactly a certain size, such as thumbnails, profile pictures, or grid layouts.

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 use of resize_and_crop().

Perfect Thumbnails Pattern: The resize_and_crop() method is ideal for creating thumbnails that must be exact dimensions. Unlike simple resizing (which may produce varied sizes) or manual resize-then-crop sequences, this single method guarantees precise dimensions with optimal cropping.

Example #2

The example above shows how to create uniform profile avatars from uploaded images.

Consistent Display Sizes: Using resize_and_crop() ensures all avatars, thumbnails, or grid images have identical dimensions regardless of the original image's aspect ratio. This creates clean, uniform layouts without the need for CSS tricks or complex frontend code.

Example #3

The example above demonstrates creating multiple standard sizes for a product gallery.

Multi-Size Generation Pattern: When creating multiple size variations, resize_and_crop() simplifies the process significantly. Each size is guaranteed to match specifications exactly, making responsive image delivery predictable and layouts consistent across all devices.

Example #4

The example above shows creating social media sharing images with platform-specific dimensions.

Platform-Specific Images: Different social media platforms require different image dimensions for optimal display. The resize_and_crop() method makes it straightforward to generate properly sized images for Facebook, Twitter, Instagram, LinkedIn, Pinterest, and other platforms from a single source image.