How To 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!

ⓘ I have included a zip file with all the source code at the start of this tutorial, so you don’t have to copy-paste everything… Or if you just want to dive straight in.

 

 

QUICK SLIDES

 

TABLE OF CONTENTS

Download & Notes Display Image Useful Bits & Links
The End

 

DOWNLOAD & NOTES

Firstly, here is the download link to the example code as promised.

 

QUICK NOTES

If you spot a bug, feel free to comment below. I try to answer short questions too, but it is one person versus the entire world… If you need answers urgently, please check out my list of websites to get help with programming.

 

EXAMPLE CODE DOWNLOAD

Click here to download all the example source code, I have released it under the MIT license, so feel free to build on top of it or use it in your own project.

 

 

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 = "cate.jpg";
$fileData = exif_read_data($file);
 
// (B) OUTPUT HTTP HEADERS
header("Content-Type: " . $fileData["MimeType"]);
header("Content-Length: " . $fileData["FileSize"]);
 
// (C) READ FILE
readfile($file);

Yep, that’s how simple it is.

  1. Read the EXIF information from the image file.
  2. 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.
  3. Lastly, just read and output the image file itself.

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 = "cate.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.

 

 

USEFUL BITS & LINKS

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

 

INFOGRAPHIC CHEAT SHEET

How To Display Images In PHP (click to enlarge)

 

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!

Leave a Comment

Your email address will not be published.