PHP 101
  • Home
  • Deploy
  • How-to
  • Tutorial
No Result
View All Result
PHP 101
  • Home
  • Deploy
  • How-to
  • Tutorial
No Result
View All Result
PHP 101
No Result
View All Result
Home How-to

How to display image from file using PHP

September 8, 2022
Reading Time: 4 mins read
PHP101.Net - How to display image from file using PHP

PHP101.Net - How to display image from file using PHP

Share on FacebookShare on Twitter

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:

  1. 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.
  2. 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 the mime_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 PHP
    A 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.

You might also like

  • How to convert image to base64 using PHP

References

  • PHP.net Official Documentation – GD extension functions list
  • PHP.net Official Documentation – ImageMagick manual
  • PHP.net Official Documentation – mime_content_type() function
Tags: display image from fileimage from filephp display imagephp display image from file
Previous Post

How to convert image to base64 using PHP

Next Post

How to install ImageMagick on Windows

Related Posts

PHP101.net - How to set timezone with PHP

How to set timezone with PHP easily (multiple methods)

March 9, 2023
PHP101.net - How to redirect to another page using PHP

How to redirect to another page using PHP

March 8, 2023

Table of Contents

    • Quick Solution
    • To display image from file using PHP
    • Prerequisites
    • 1. Display image file directly to the browser
    • 1.1. Set header Content-Type as image
    • Tips: Determining image mimetype automatically
    • 1.2. Extract image data as resource
    • 1.3. Output image resource to browser
    • 2. Extract and display image as base64 data
    • 2.1. Extract image data as base64
    • 2.2. Output image data to <img> tag
    • Final thoughts
    • You might also like
    • References
Next Post
PHP101.Net - Tutorial - Install ImageMagick on Windows

How to install ImageMagick on Windows

We deliver free and helpful resources for learning
and using PHP programming language.

About
Contact Us
Privacy Policy
Main Sections

Home
Deploy
How-to
Tutorial
You might be interested in...

How to redirect to another page using PHP

How to install Composer on Linux

How to install PHP GeoIP extension & GeoLite2 library (databases included)

Deploy a webserver with XAMPP on Windows for PHP development

How to set timezone with PHP easily (multiple methods)

© 2022 PHP101.Net - Helpful PHP Tutorials, How-to, Tips and Tricks for everyone.

No Result
View All Result
  • Home
  • Deploy
  • How-to
  • Tutorial

© 2022 PHP101.Net - Helpful PHP Tutorials, How-to, Tips and Tricks for everyone. | Privacy Policy

This website uses cookies. By continuing to use this website you are giving consent to cookies being used. Visit our Privacy and Cookie Policy.