Display Image From File In PHP (Simple Examples)

Welcome to a tutorial on how to display an image from a file in PHP. So you have a secured folder of images that is not publicly accessible, but still want registered users to be able to view? Yes, it is possible to do so.

To display an image from a file in PHP, simply output the necessary HTTP headers and read the image file:

  • header("Content-Type: image/jpeg");
  • header("Content-Length: " . filesize("IMAGE.JPG"));
  • readfile("IMAGE.JPG");

That should cover the basics, but read on for a few more examples!

 

 

TABLE OF CONTENTS

 

DOWNLOAD & NOTES

Here is the download link to the example code, so you don’t have to copy-paste everything.

 

EXAMPLE CODE DOWNLOAD

Source code on GitHub Gist

Just click on “download zip” or do a git clone. I have released it under the MIT license, so feel free to build on top of it or use it in your own project.

 

SORRY FOR THE ADS...

But someone has to pay the bills, and sponsors are paying for it. I insist on not turning Code Boxx into a "paid scripts" business, and I don't "block people with Adblock". Every little bit of support helps.

Buy Me A Coffee Code Boxx eBooks

 

 

PHP DISPLAY IMAGE

All right, let us now get into the examples of displaying an image file in PHP.

 

EXAMPLE 1) READ FILE AND OUTPUT

1a-output-img.php
<?php
// (A) GET IMAGE INFO
$file = __DIR__ . DIRECTORY_SEPARATOR . "cabbage-girl.jpg";
$fileData = exif_read_data($file);
 
// (B) READ & OUTPUT IMAGE
ob_start();
header("Content-Type: " . $fileData["MimeType"]);
header("Content-Length: " . $fileData["FileSize"]);
readfile($file);
ob_end_flush();

Yep, that’s how simple it is.

  • (A) Read the EXIF information from the image file.
  • (B) Output the appropriate HTTP headers, the bare minimum is Content-Type, and Content-Length is kind of optional. But since some browsers can get naggy about the length, it is better to just include it.
  • (B) Lastly, just use readfile() to read and output the image file itself.
  • (B) To ensure that the image data is “safely and completely” delivered, we use output buffering – ob_start() and ob_end_flush(). Will leave links below if you want to follow up on output buffers.

That’s all. Accessing this script in the browser will show the image, or we can point an image tag to this PHP script:

1b-php-img.html
<img src="1a-output-img.php">

 

 

EXAMPLE 2) BASE 64 ENCODED IMAGE

2-img-tag.php
<?php
// (A) READ IMAGE INFO
$file = __DIR__ . DIRECTORY_SEPARATOR . "cabbage-girl.jpg";
$fileData = exif_read_data($file);
$fileEncode = base64_encode(file_get_contents($file));

// (B) BASE 64 ENCODED IMAGE
echo "<img src=\"data:${fileData["MimeType"]};base64,${fileEncode}\">";
?>

This is an alternative to reading an image file and outputting it with PHP – Yes, we can read an image file, base 64 encode it, and “directly embed” it into an <img> tag.

 

 

EXTRAS

That’s all for the tutorial, and here is a small section on some extras and links that may be useful to you.

 

LINKS & REFERENCES

 

THE END

Thank you for reading, and we have come to the end. I hope that it has helped you to better understand, and if you want to share anything with this guide, please feel free to comment below. Good luck and happy coding!

2 thoughts on “Display Image From File In PHP (Simple Examples)”

  1. I can’t get any of this to run. What do you run first 1a or 1b or ??? I think 1b ends up showing an empty square box in the upper left corner of the screen but clicking on it does nothing. Do you run it with localhost ? firefox ? php ?

    Thanks in advance for help in getting this to run.
    R

Leave a Comment

Your email address will not be published. Required fields are marked *