Quick Solution
Assuming we want to display image from file using PHP, here is a quick solution for achieving that using PHP GD extension.
// Set header to PNG image type
header('Content-Type: image/png');
// Initilize the image resource from PNG image file using GD
$im = imagecreatefrompng($file);
// Display the image
imagepng($im);
// Close image resource to save memory
imagedestroy($im);
These PHP codes will output and display the image from file to the browser as if it’s an image file.
For more details about other methods to display image from file using PHP with GD extension, and also with ImageMagick library, display image using <img>
tag, we will go through the steps below.
To display image from file using PHP
There are multiple methods to display image from an image file using PHP. This article will guide you on the two popular methods that we usually want to achieve:
- Display an image file directly to the browser.
This method is used when you want to use PHP to output the image file to the browser directly. - Extract an image file to base64 data and display it on the browser using
<img>
tag.
This method is more preferred for displaying image inline as base64 data with<img>
tag.
For both methods, we can manipulate the image (for example, resize, add watermark) before displaying.
Prerequisites
A PHP image manipulation extension must be installed. The most popular extensions are GD and ImageMagick.
- GD extension is usually built-in, installed and enabled with most default PHP installation on most web hosting.
- ImageMagick is an advanced image processing software that is better and faster than GD, has good PHP support, but it has to be installed separately. Modern web hosting could have ImageMagick installed and enabled by request.
1. Display image file directly to the browser
To display image from file to the browser directly using PHP, there are the primary steps:
- Set header
Content-Type
as image - Extract image data as an image resource
- Output the image data to the browser
Assuming we have an PNG image file as image.png
, here are the steps:
1.1. Set header Content-Type as image
This is to tell the browser that the content to display is an image. The header()
function will be used:
header('Content-Type: image/png');
Depends on the image extension/mimetype, the Content-Type
will be different. Below are the most popular extensions:
- png extension: image/png
- jpg and jpeg extension: image/jpeg
- gif extension: image/gif
Tips: Determining image mimetype automatically
We can use PHP mime_content_type()
function to get the image mimetype quickly. It can be helpful when the image file extension is wrongly renamed from the actual content mimetype:
// Returns image/png
// Can returns differently (eg. image/jpeg) if the file extension is wrongly renamed
$mimetype = mime_content_type('image.png');
// Add detected mimetype to header
header('Content-Type: ' . $mimetype);
1.2. Extract image data as resource
We will extract the content of the image as a resource, using one of GD or ImageMagick extension:
// Option 1: Extract image data using GD
$im = imagecreatefrompng('image.png');
// Option 2: Extract image data using ImageMagick
$im = new Imagick('image.png');
1.3. Output image resource to browser
The final step is to output the image resource to browser, and it will display the PHP codes as an image.
For GD extension:
// Display image resource using GD
imagepng($im);
// Additionally, use imagedestroy() to clean up resource and save memory
imagedestroy($im);
For ImageMagick library:
// Display image resource echo ImageMagick getImageBlob() resource
echo $im->getImageBlob();
// Additionally, use destroy() method to clean up resource and save memory
$im->destroy();
2. Extract and display image as base64 data
To display image from file as base64 data with inline <img>
tag, the steps are pretty simple:
- Extract image data as base64
- Output the image data to the
<img>
tag
Here are the steps to do that.
2.1. Extract image data as base64
The below scripts will get image data using file_get_contents()
function and convert it to base64.
// Get image data
$imageData = file_get_contents('image.png');
// Encode image data to base64
$base64 = base64_encode($imageData)
2.2. Output image data to <img> tag
To output and display the image base64 data to <img>
format, here is the syntax:
echo '<img src="data:{$image_mime_type};base64,{$base64_data}">';
Where
{$image_mime_type}
is the mimetype of the image, which we can use themime_content_type()
function to fetch from the image file.{$base64_data}
is the extracted base64 data.
For example:
echo '<img src="data:' . mime_content_type('image.png') . ';base64,' . $base64 . '">';
In the rendered browser page, the image will be displayed properly.
For more in-depth information about converting image to base64 data, we have another useful article:
- How to convert image to base64 using PHPA useful article on how to convert and display image as base64 data using PHP.
Final thoughts
It’s useful to know how to display image from file using PHP, which is used a lot in most PHP development projects. We hope this guide is clear and helpful to you.