Home » Php » php – Can I embed a .png image into an html page?

php – Can I embed a .png image into an html page?

Posted by: admin April 23, 2020 Leave a comment


How can I embed a .png file into a blank “file.html” so that when you open that file in any browser you see that image? In this scenario the image file is not linked to from the HTML but rather the image data is embedded in the HTML itself.

How to&Answers:

There are a few base64 encoders online to help you with this, this is probably the best I’ve seen:


As that page shows your main options for this are CSS:

div.image {

Or the <img> tag itself, like this:

<img alt="My Image" src="data:image/png;base64,iVBORwA<MoreBase64SringHere>" />


The 64base method works for large images as well, I use that method to embed all the images into my website, and it works every time. I’ve done with files up to 2Mb size, jpg and png.


I don’t know for how long this post has been here. But I stumbled upon similar problem now. Hence posting the solution so that it might help others.

#!/usr/bin/env perl
use strict;
use warnings;
use utf8;

use GD::Graph::pie;
use MIME::Base64;
my @data = (['A','O','S','I'],[3,16,12,47]);

my $mygraph = GD::Graph::pie->new(200, 200);
my $myimage = $mygraph->plot(\@data)->png;

print <<end_html;
<html><head><title>Current Stats</title></head>
<p align="center">
<img src="data:image/png;base64,

print encode_base64($myimage);

print <<end_html;
" style="width: 888px; height: 598px; border-width: 2px; border-style: solid;" /></p>



Quick google search says you can embed it like this:

<img src="data:image/gif;base64,R0lGODlhEAAOALMAAOazToeHh0tLS/7LZv/0jvb29t/f3//Ub/
width="16" height="14" alt="embedded folder icon">

But you need a different implementation in Internet Explorer.



use mod_rewrite to redirect the call to file.html to image.png without the url changing for the user

Have you tried just renaming the image.png file to file.html? I think most browser take mime header over file extension 🙂