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().
// Load an image
$this->image->load('modules/gallery/photos/photo.jpg');
// Resize to 600 pixels tall (width calculated automatically)
$this->image->resize_to_height(600);
// Save the resized image
$this->image->save('modules/gallery/photos/photo_resized.jpg');
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.
public function normalise_gallery_heights(): void {
$gallery_id = segment(3, 'int');
// Get all images in the gallery
$images = $this->model->get_many_where("gallery_id = {$gallery_id}", 'gallery_images');
if ($images === false) {
redirect('gallery/view/' . $gallery_id);
}
$target_height = 400;
$processed = 0;
foreach ($images as $image) {
$source_path = 'modules/gallery/images/' . $image->filename;
if ($this->file->exists($source_path)) {
// Load the image
$this->image->load($source_path);
// Only resize if taller than target
if ($this->image->get_height() > $target_height) {
$this->image->resize_to_height($target_height);
// Save the normalised version
$output_path = 'modules/gallery/normalised/' . $image->filename;
// Ensure directory exists
if (!$this->file->exists('modules/gallery/normalised')) {
$this->file->create_directory('modules/gallery/normalised', 0755);
}
$this->image->save($output_path, 85);
$processed++;
}
$this->image->destroy();
}
}
set_flashdata("Normalised {$processed} gallery images to {$target_height}px height");
redirect('gallery/view/' . $gallery_id);
}
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.
public function create_portrait_preview(): void {
$photo_id = segment(3, 'int');
// Get photo record
$photo = $this->db->get_where($photo_id, 'photos');
if ($photo === false) {
redirect('photos/not_found');
}
$source_path = 'modules/gallery/originals/' . $photo->filename;
if (!$this->file->exists($source_path)) {
redirect('photos/file_missing');
}
// Load the image
$this->image->load($source_path);
// Get current dimensions
$width = $this->image->get_width();
$height = $this->image->get_height();
// Determine if portrait orientation
if ($height > $width) {
// Portrait: resize to standard preview height
$preview_height = 800;
if ($height > $preview_height) {
$this->image->resize_to_height($preview_height);
// Save portrait preview
$preview_dir = 'modules/gallery/previews/portrait';
if (!$this->file->exists($preview_dir)) {
$this->file->create_directory($preview_dir, 0755);
}
$output_path = $preview_dir . '/' . $photo->filename;
$this->image->save($output_path, 85);
// Update photo record
$update_data = [
'preview_path' => $output_path,
'preview_created_at' => date('Y-m-d H:i:s')
];
$this->model->update($photo_id, $update_data, 'photos');
set_flashdata('Portrait preview created successfully');
} else {
set_flashdata('Image already smaller than preview size');
}
} else {
set_flashdata('Image is not portrait orientation');
}
$this->image->destroy();
redirect('gallery/view/' . $photo_id);
}
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.
public function generate_responsive_heights(): void {
$article_id = segment(3, 'int');
// Get article record
$article = $this->db->get_where($article_id, 'articles');
if ($article === false) {
redirect('articles/not_found');
}
$hero_image = 'modules/articles/images/' . $article->hero_image;
if (!$this->file->exists($hero_image)) {
redirect('articles/image_missing');
}
// Define height breakpoints for different devices
$height_breakpoints = [
'mobile' => 400,
'tablet' => 600,
'desktop' => 800,
'large' => 1200
];
// Load source to check if it's tall enough
$this->image->load($hero_image);
$source_height = $this->image->get_height();
$this->image->destroy();
foreach ($height_breakpoints as $device => $target_height) {
// Only create this size if source is taller
if ($source_height > $target_height) {
// Load source fresh for each size
$this->image->load($hero_image);
// Resize to target height
$this->image->resize_to_height($target_height);
// Save the device-specific version
$output_dir = 'modules/articles/responsive/' . $device;
if (!$this->file->exists($output_dir)) {
$this->file->create_directory($output_dir, 0755);
}
$output_path = $output_dir . '/' . $article->hero_image;
$this->image->save($output_path, 85);
$this->image->destroy();
}
}
set_flashdata('Responsive height variations generated');
redirect('articles/edit/' . $article_id);
}
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.