get_header()
public function get_header(): string
Description
Returns the MIME type of the currently loaded image. This method determines the appropriate Content-Type header value for serving images directly to browsers, returning values like 'image/jpeg', 'image/png', 'image/gif', or 'image/webp' based on the loaded image format.
Image Must Be Loaded: This method requires an image to be loaded first using load() or upload(). Calling get_header() without a loaded image will throw an InvalidArgumentException with the message "No image has been loaded, or image type is unset."
Parameters
This method takes no parameters.
Return Value
| Type | Description |
|---|---|
| string | The MIME type of the loaded image. Possible values: 'image/jpeg', 'image/png', 'image/gif', 'image/webp'. |
Example #1
The code sample below demonstrates the most basic use of get_header().
// Load an image
$this->image->load('modules/gallery/photos/photo.jpg');
// Get the MIME type
$mime_type = $this->image->get_header();
echo $mime_type; // Outputs: image/jpegPrimary Use Case: The get_header() method is primarily used when serving images directly to browsers. It provides the correct Content-Type header value needed for the browser to properly interpret and display the image data.
Example #2
The example above shows how to serve a dynamically processed image directly to the browser.
public function serve_thumbnail(): 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 and process the image
$this->image->load($image_path);
$this->image->resize_to_width(200);
// Set the correct Content-Type header
header('Content-Type: ' . $this->image->get_header());
// Output the image directly to browser
$this->image->output();
// Free memory
$this->image->destroy();
}The Dynamic Image Serving Pattern: When serving images on-the-fly, always set the Content-Type header using get_header() before calling output(). This tells the browser what type of image data to expect. Without the correct header, browsers may fail to display the image or attempt to download it as a file.
Example #3
The example above demonstrates serving protected images with authentication checks.
public function serve_private_document(): void {
$document_id = segment(3, 'int');
$user_id = $this->user->get_id();
// Verify user is logged in
if (!$user_id) {
redirect('login');
}
// Get document record
$document = $this->db->get_where($document_id, 'private_documents');
if ($document === false) {
redirect('documents/not_found');
}
// Verify user has access to this document
if ($document->user_id !== $user_id) {
redirect('documents/access_denied');
}
$image_path = 'modules/documents/private/' . $document->filename;
if (!$this->file->exists($image_path)) {
redirect('documents/file_missing');
}
// Load the image
$this->image->load($image_path);
// Set appropriate headers for direct image serving
header('Content-Type: ' . $this->image->get_header());
header('Content-Disposition: inline; filename="' . $document->filename . '"');
header('Cache-Control: private, max-age=3600');
// Serve the image
$this->image->output();
$this->image->destroy();
}Protected Image Serving: When serving images from protected directories (outside the public folder), use controller methods to enforce authentication and authorization. The get_header() method ensures the browser receives the correct MIME type, while your controller logic controls who can access the image.
Example #4
The example above shows how to use get_header() when creating an image download endpoint with proper headers.
public function download_processed_image(): void {
$order_id = segment(3, 'int');
// Get order record
$order = $this->db->get_where($order_id, 'print_orders');
if ($order === false) {
redirect('orders/not_found');
}
// Verify order belongs to current user
$user_id = $this->user->get_id();
if ($order->user_id !== $user_id) {
redirect('orders/access_denied');
}
$processed_image_path = 'modules/print_orders/processed/' . $order->processed_filename;
if (!$this->file->exists($processed_image_path)) {
set_flashdata('Processed image not yet ready');
redirect('orders/view/' . $order_id);
}
// Load the processed image
$this->image->load($processed_image_path);
// Get MIME type for Content-Type header
$mime_type = $this->image->get_header();
// Determine file extension from MIME type
$extension_map = [
'image/jpeg' => '.jpg',
'image/png' => '.png',
'image/gif' => '.gif',
'image/webp' => '.webp'
];
$extension = $extension_map[$mime_type] ?? '.jpg';
$download_filename = 'order_' . $order_id . '_processed' . $extension;
// Set headers for download
header('Content-Type: ' . $mime_type);
header('Content-Disposition: attachment; filename="' . $download_filename . '"');
header('Content-Length: ' . filesize($processed_image_path));
header('Cache-Control: no-cache, must-revalidate');
header('Pragma: no-cache');
// Output the image
$this->image->output();
$this->image->destroy();
}Download vs. Inline Display: The Content-Disposition header controls browser behavior. Use inline to display images in the browser, or attachment to trigger a download. In both cases, get_header() provides the correct MIME type for the Content-Type header.