Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I embed a .png image into an HTML page?

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.

like image 828
Rella Avatar asked May 11 '10 00:05

Rella


People also ask

Can you embed PNG in HTML?

You can use PNG, JPEG or GIF image file based on your comfort but make sure you specify correct image file name in src attribute. Image name is always case sensitive.

Can you embed an image in HTML?

Images can be easily inserted at any section in an HTML page. To insert image in an HTML page, use the <img> tags. It is an empty tag, containing only attributes since the closing tag is not required. Just keep in mind that you should use the <img> tag inside <body>…


2 Answers

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

http://www.greywyvern.com/code/php/binary2base64

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

div.image {   width:100px;   height:100px;   background-image:url(data:image/png;base64,iVBORwA<MoreBase64SringHere>); } 

Or the <img> tag itself, like this:

<img alt="My Image" src="data:image/png;base64,iVBORwA<MoreBase64SringHere>" /> 
like image 195
Nick Craver Avatar answered Oct 06 '22 20:10

Nick Craver


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 it with files up to 2 MB size, JPEG and PNG.

like image 40
Diamax Avatar answered Oct 06 '22 20:10

Diamax