get_height()
public function get_height(): int
Description
Returns the height in pixels of the currently loaded image. This method uses PHP's GD library imagesy() function to retrieve the height of the image resource.
Image Must Be Loaded: This method requires an image to be loaded first using load() or upload(). Calling get_height() without a loaded image will throw an Exception with the message "No image is loaded."
Parameters
This method takes no parameters.
Return Value
| Type | Description |
|---|---|
| int | The height of the loaded image in pixels. |
Example #1
The code sample below demonstrates the most basic use of get_height().
// Load an image
$this->image->load('modules/gallery/photos/photo.jpg');
// Get the height
$height = $this->image->get_height();
echo $height; // Outputs: 1200 (for example)Common Use Cases: The get_height() method is typically used to determine aspect ratios, validate image dimensions, calculate resize operations, or make decisions about how to process an image based on its dimensions.
Example #2
The example above shows how to use get_height() to calculate and display image dimensions.
public function display_image_info(): void {
$photo_id = segment(3, 'int');
// Get photo record
$photo = $this->db->get_where($photo_id, 'photos');
if ($photo === false) {
redirect('photos/not_found');
}
$image_path = 'modules/gallery/photos/' . $photo->filename;
if (!$this->file->exists($image_path)) {
redirect('photos/file_missing');
}
// Load the image
$this->image->load($image_path);
// Get dimensions
$width = $this->image->get_width();
$height = $this->image->get_height();
// Calculate aspect ratio
$aspect_ratio = round($width / $height, 2);
// Determine orientation
if ($width > $height) {
$orientation = 'Landscape';
} elseif ($height > $width) {
$orientation = 'Portrait';
} else {
$orientation = 'Square';
}
// Pass data to view
$data = [
'photo' => $photo,
'width' => $width,
'height' => $height,
'aspect_ratio' => $aspect_ratio,
'orientation' => $orientation
];
$this->image->destroy();
$this->view('photo_info', $data);
}Dimension Analysis Pattern: Combining get_width() and get_height() allows you to analyze image characteristics before processing. This is useful for determining appropriate resize operations, validating uploads, or displaying metadata to users.
Example #3
The example above demonstrates using get_height() to intelligently resize images based on their orientation.
public function smart_resize(): 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();
// Define maximum dimensions
$max_dimension = 800;
// Resize based on orientation
if ($width > $height) {
// Landscape: constrain by width
if ($width > $max_dimension) {
$this->image->resize_to_width($max_dimension);
}
} else {
// Portrait or square: constrain by height
if ($height > $max_dimension) {
$this->image->resize_to_height($max_dimension);
}
}
// Save the resized version
$output_path = 'modules/gallery/resized/' . $photo->filename;
// Ensure directory exists
if (!$this->file->exists('modules/gallery/resized')) {
$this->file->create_directory('modules/gallery/resized', 0755);
}
$this->image->save($output_path, 85);
$this->image->destroy();
set_flashdata('Image resized intelligently based on orientation');
redirect('gallery/view/' . $photo_id);
}Orientation-Aware Processing: By checking dimensions with get_width() and get_height(), you can apply different processing strategies for landscape vs. portrait images. This ensures optimal results regardless of the original image orientation.
Example #4
The example above shows using get_height() to validate image dimensions before allowing upload.
public function process_banner_upload(): void {
// Validate file upload
$this->validation->set_rules('userfile', 'Banner Image', 'required|allowed_types[jpg,png]|max_size[5000]');
if ($this->validation->run() === true) {
// Configure temporary upload
$config = [
'destination' => 'temp',
'upload_to_module' => true,
'target_module' => 'banners',
'make_rand_name' => true
];
// Ensure temp directory exists
$temp_dir = 'modules/banners/temp';
if (!$this->file->exists($temp_dir)) {
$this->file->create_directory($temp_dir, 0755);
}
// Upload to temporary location
$file_info = $this->image->upload($config);
// Load the uploaded image to check dimensions
$this->image->load($file_info['file_path']);
// Get dimensions
$width = $this->image->get_width();
$height = $this->image->get_height();
// Validate banner dimensions (must be 1200x400)
if ($width !== 1200 || $height !== 400) {
// Delete the invalid upload
$this->file->delete($file_info['file_path']);
$this->image->destroy();
set_flashdata('Banner must be exactly 1200x400 pixels. Your image was ' . $width . 'x' . $height);
redirect('banners/upload_form');
}
// Dimensions are valid - move to permanent location
$permanent_path = 'modules/banners/images/' . $file_info['file_name'];
// Ensure permanent directory exists
if (!$this->file->exists('modules/banners/images')) {
$this->file->create_directory('modules/banners/images', 0755);
}
// Copy to permanent location
$this->file->copy($file_info['file_path'], $permanent_path);
// Delete temp file
$this->file->delete($file_info['file_path']);
$this->image->destroy();
// Store in database
$data = [
'filename' => $file_info['file_name'],
'file_path' => $permanent_path,
'uploaded_at' => date('Y-m-d H:i:s')
];
$this->model->insert($data, 'banners');
set_flashdata('Banner uploaded successfully');
redirect('banners/manage');
} else {
$this->upload_form();
}
}Dimension Validation Pattern: For images that require exact dimensions (banners, headers, thumbnails), upload to a temporary location first, then use get_width() and get_height() to validate dimensions before moving to permanent storage. This prevents invalid images from entering your system while providing clear feedback to users.