Blog · Images
Why Won't My Base64 Image Show Up?
The HTML email template looks right in the editor. The test send arrives, and where the logo should be there is a small broken image icon. In the source, the img tag holds a long run of letters and digits that is meant to be the picture. Why does this long string show a broken image instead of a picture?
Most of the time the image data is fine and something around it is wrong. Check four things in turn, the data URI prefix, the type label, whether the string was cut off, and whether stray characters crept in.
The string has no prefix
The symptom is an img tag whose src holds only the Base64 text, starting with something like iVBORw0KGgo.
An img src is a URL. Without the data scheme in front, the browser reads the string as an ordinary file address, looks for a file by that name and finds nothing. A data URL needs the scheme, an optional media type, the word base64 and a comma, and only then the data. MDN shows the pattern as "data:[<media-type>][;base64],<data>" and warns that "it's easy to forget to put a comma before the "data" segment".
The fix is to put "data:image/png;base64," in front of the string, with the type that matches the image. A JPEG takes "data:image/jpeg;base64," instead.
Base64 to Image accepts raw Base64 without the prefix too. With no prefix, it reads the type from the data itself.
The label names the wrong type
The symptom is a label that says image/png on data that is really a JPEG, or the other way round.
In a web page this often still works. The WHATWG MIME Sniffing standard tells browsers to check the first bytes of an image and go by what they find, so a JPEG labelled PNG will usually still show. SVG is the exception. It has no byte pattern in that standard, so an SVG needs the image/svg+xml label to show as an image. The wrong label also causes trouble later, when the data is saved with the extension the label suggests.
The fix is to change the label to the real type. To find the real type, paste the string into the converter. Its FAQ explains how it decides.
"A data URI starts with a label that names the image type, such as PNG, and that label can be wrong. The converter reads the first bytes of the data, which show what the file really is. When the two disagree, the data wins, so a file labelled PNG that holds JPEG data is shown and saved as JPEG."
When that happens, a note under the image reads "Labelled PNG, but the data is JPEG. Shown and saved as JPEG."
The string was cut off
The symptom is a string copied from a log line, a database field or a form with a length limit. The start looks right, but the end is missing.
Base64 packs 3 bytes into every 4 characters. When the string loses its end, the bytes at the end of the file go with it. The browser's decoder rejects a string whose length leaves 1 character over after dividing by 4. Other lengths still decode, but the file is incomplete, and the browser may not be able to draw it.
The fix is to copy the string again from the full source. To check whether the copy matches the original, paste each one into Character Counter and compare the counts. Base64 to Image also says so when the data looks cut off, with the note "The data looks cut off. The image may be incomplete."
Stray characters got in
The symptom is a string that came out of JSON, code or a URL and picked up extra characters on the way.
- Quotes. A string copied out of JSON or code often brings its quote marks along. The browser's Base64 decoder allows only letters, digits, + and /, plus = padding at the end, so a quote makes it fail. Remove them.
- Line breaks and spaces. The same decoder removes spaces and line breaks before it starts, so in a browser they usually do no harm. Other software can be stricter, so take them out if the string goes anywhere else.
- URL-safe characters. RFC 4648 defines a URL and filename safe alphabet that uses - and _ in place of + and /. The browser's decoder does not accept - or _ in a data URL, so change each - back to + and each _ back to /.
Base64 to Image cleans these up when it reads the string. Its FAQ says "Spaces, line breaks and surrounding quotes are removed, URL-safe - and _ are converted back to + and /, and missing = padding is added. A note under the image says what was fixed." Any other character that does not belong in Base64 is highlighted in the box, with its position.
Still broken after all four
Paste the whole string into Base64 to Image and read what it reports.
- If the image appears with its type, size and dimensions, the data is good, and the problem is in the page or the email around it. Email clients differ in how they handle Base64 images, and caniemail.com tracks which ones show them.
- If it says "This isn't valid Base64.", fix the highlighted characters and paste again.
- If it says "Decoded, but this isn't an image", the data is another kind of file, such as a PDF. It can still be downloaded with its own extension.
- If it says the data looks cut off, go back to the full source and copy the string again.
To go the other way and make a Base64 string from a picture, use Image to Base64. For Base64 that holds text rather than an image, use Base64 Encode and Decode.
Frequently Asked Questions
Does a Base64 image need the data:image prefix? In an img tag, yes. The browser needs the data scheme and the type in front of the Base64, such as "data:image/png;base64,". Base64 to Image accepts the string with or without it.
Why is a Base64 image bigger than the file? Base64 turns every 3 bytes into 4 characters. MDN puts it as "typically roughly a third larger than its source", so a 30 KB image becomes about 40 KB of text.
Can I turn Base64 back into a file I can save? Yes. Paste it into Base64 to Image and press Download image. The file is saved with the extension of the type found in the data, such as .png or .jpg.
Related tools
- Base64 to Image shows the image and reads its real type from the data.
- Image to Base64 turns any image into a Base64 string.
- Base64 Encode and Decode encodes text to Base64 or decodes it back to text.
- Character Counter counts characters, so you can compare two copies of a string.
- Remove EXIF Data strips hidden metadata and GPS location from a photo before you encode it.
Ready? Paste your Base64 and see the image.
Sources
- Data URLs. MDN Web Docs. developer.mozilla.org/
en- US/ docs/ Web/ URI/ Reference/ Schemes/ data - Base64. MDN Web Docs glossary. developer.mozilla.org/
en- US/ docs/ Glossary/ Base64 - MIME Sniffing Standard, section 8.2 Sniffing in an image context. WHATWG. mimesniff.spec.whatwg.org
- Infra Standard, forgiving-base64 decode. WHATWG. infra.spec.whatwg.org
- RFC 4648, The Base16, Base32, and Base64 Data Encodings, section 5. IETF. rfc-editor.org/rfc/rfc4648
- Base64 image format support in email clients. Can I email. caniemail.com/
features/ image- base64