resize_to_height()

public function resize_to_height(int $height): void

Description

Resizes a loaded image to the specified height whilst automatically calculating and maintaining the correct width to preserve the original aspect ratio. This method ensures proportional scaling with no distortion.

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

Parameters

Parameter Type Description Default Required
$height int The target height in pixels. Must be greater than zero. N/A Yes

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_to_height().

Aspect Ratio Preservation: The resize_to_height() method automatically calculates the proportional width needed to maintain the original aspect ratio. You only specify the height - the width adjusts accordingly, ensuring no distortion or stretching of the image.

Example #2

The example above shows how to create uniform height images for a horizontal gallery.

Horizontal Gallery Pattern: When creating horizontal galleries or sliders, using resize_to_height() ensures all images have the same height whilst allowing widths to vary naturally. This creates a clean, aligned presentation without forcing images into unnatural aspect ratios.

Example #3

The example above demonstrates processing portrait images for a consistent display height.

Orientation-Specific Processing: Use resize_to_height() for portrait images and resize_to_width() for landscape images to ensure optimal scaling. Check orientation with get_width() and get_height(), then apply the appropriate resize method.

Example #4

The example above shows creating multiple height variations for responsive delivery.

Responsive Height Strategy: For vertically scrolling content or mobile-first designs, creating height-based responsive images can be more appropriate than width-based variations. This approach ensures images fit within viewport heights whilst maintaining aspect ratios, improving the mobile reading experience.