Convert an Image to Base64
Encode an image as raw Base64, a complete data URI, an HTML image tag or CSS code. Preview the file, inspect its details and copy the required output. All processing happens locally in your browser.
Convert an Image to Base64
Convert an Image to Base64
Encode JPG, PNG, WebP, GIF, or SVG images into Data URIs, HTML img tags, or CSS code
The selected image is encoded inside your browser.
What Is Image-to-Base64 Conversion?
Image-to-Base64 conversion represents the bytes of an image as a text string. This encoded text can be stored inside HTML, CSS, JSON, XML, test data, or another text-based format that cannot directly contain binary image data.
Base64 is an encoding method, not an image compressor or format converter. It does not improve image quality or reduce the original file size. The encoded text is usually larger than the original binary image because each group of three input bytes is represented by four Base64 characters.
How to Convert an Image to Base64
- Choose an image: Select a supported JPG, PNG, WebP, GIF, SVG, or other image file enabled in the workspace.
- Review the file details: Check the detected media type, original byte size, dimensions when available, encoded character count, and calculated size overhead.
- Select an output mode: Choose Raw Base64, Data URI, HTML image tag, CSS background image, or JSON string.
- Adjust the display: Enable line wrapping when you want to inspect a long encoded string without horizontal scrolling.
- Copy or download: Copy the generated output to the clipboard or save it as a text file.
Raw Base64 Versus a Data URI
Raw Base64 and a Data URI contain related information, but they are used differently.
-
Raw Base64:
Contains only the encoded character data, such as
iVBORw0KGgoAAAANSUhEUgAA.... An application using this value must already know the original file type. -
Data URI:
Adds the media type and Base64 marker before the encoded content, such as
data:image/png;base64,iVBORw....
Browsers can use a complete Data URI directly in supported HTML and CSS properties. Raw Base64 is more suitable when a program, database field, or API expects only the encoded payload.
Using Base64 in HTML
To display an inline image in HTML, place the complete Data URI inside the src attribute of an <img> element.
<img
src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAA..."
alt="Embedded graphic"
>
Always include useful alternative text when the image communicates information. For decorative images, use an empty alt value where appropriate.
Large Base64 strings can make an HTML document difficult to read and maintain. External image files are normally a better choice for photographs and other large assets.
Using Base64 in CSS
A complete Data URI can also be used inside the CSS url() function.
.icon {
background-image: url("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAA...");
background-repeat: no-repeat;
background-position: center;
background-size: contain;
}
This can be practical for a small icon or a self-contained component. Embedding many large images inside a stylesheet can increase its size and make individual assets harder to update or cache separately.
Using Base64 in JSON
Base64 can represent image data inside JSON when an application or API accepts encoded binary content.
{
"mediaType": "image/jpeg",
"image": "/9j/4AAQSkZJRgABAQ..."
}
Some systems expect raw Base64, while others expect a complete Data URI. Check the API or application documentation before choosing the output mode.
Large encoded images can make JSON payloads slow to transmit, parse, log, and store. File uploads or object-storage URLs are generally more practical for large media.
Why Is the Base64 String Larger?
Base64 converts each three-byte group of binary data into four text characters. The encoded length can be calculated as:
4 × ceil(original byte length ÷ 3)
For sufficiently large files, this produces an expansion of about one third before adding a Data URI prefix or other surrounding markup. Very small files may have a slightly different percentage because Base64 output is padded to groups of four characters.
HTTP compression such as Brotli or gzip may reduce the transferred size of text containing Base64, but the uncompressed encoded representation is still larger than the original binary file.
Does Base64 Change Image Quality?
Base64 encoding preserves the original file bytes. It does not resize, recompress, sharpen, or otherwise edit the image.
When the encoded string is decoded correctly, the resulting file should match the original input bytes. Any quality change would come from another image-processing or format-conversion step, not from Base64 encoding itself.
When Inline Images Can Be Useful
Base64-encoded images can be useful when a project needs a small asset to remain inside one text file.
- Small interface icons: Keep a tiny image inside a component, prototype, or test fixture.
- Self-contained demonstrations: Share a single HTML file without separate image dependencies.
- API testing: Add an encoded sample image to a mock request when the API expects Base64.
- Offline documents: Include small assets inside a file that must work without loading external resources.
- Generated previews: Use a Data URI temporarily when an application needs an inline browser preview.
There is no universal file-size threshold that makes Base64 the best choice. Consider the application, caching behaviour, document size, browser support, and maintenance requirements.
When Not to Use Base64 Images
External image files are normally better for large photographs, banners, illustrations, and frequently reused assets.
- Large strings increase the size of HTML, CSS, or JSON documents.
- Embedded images cannot always be cached independently from the parent document.
- Long encoded output makes source files harder to inspect and edit.
- Reusing the same inline image in several files duplicates its encoded data.
- Large JSON payloads may increase network, parsing, logging, and storage costs.
For normal website images, use an image URL with suitable caching and responsive-image markup instead of embedding large Base64 strings.
MIME Types and Supported Formats
A Data URI must include a media type that matches the encoded file. Common examples include:
-
PNG:
data:image/png;base64,... -
JPEG:
data:image/jpeg;base64,... -
WebP:
data:image/webp;base64,... -
GIF:
data:image/gif;base64,... -
SVG:
data:image/svg+xml;base64,...
Only list a format as supported when the workspace accepts it, identifies its type correctly, and generates valid output. Files with missing or mismatched media types should show a clear warning rather than generating an incorrect Data URI.
Working With SVG Files
SVG is a text-based image format, but it can still be encoded as Base64 when an application requires a Data URI or text payload.
An uploaded SVG should not be executed as unrestricted page markup during preview. The tool should use a safe preview method and preserve the original file bytes when generating the encoded result.
Browser Encoding
The selected image is encoded inside the browser workspace. The generated Base64 string, Data URI, and code examples are prepared from the selected file.
Large files can produce very long strings and may require more browser memory. The tool should warn before displaying or copying an unusually large output.
Related Developer Tools
Frequently Asked Questions
How do I convert an image to Base64?
Choose a supported image, select Raw Base64, Data URI, HTML, CSS, or JSON output, and copy or download the generated text.
What is the difference between Base64 and a data URI?
Raw Base64 contains only the encoded payload. A Data URI also includes the media type and Base64 prefix needed for direct use in supported HTML and CSS properties.
Does Base64 reduce image file size?
No. Base64 is an encoding method rather than compression. Its uncompressed text representation is normally about one third larger than the original binary data.
Can I use Base64 inside HTML?
Yes. A complete Data URI can be placed in the src attribute of an HTML image element. Large images are generally better served as separate files.
Can I use Base64 inside CSS?
Yes. A complete Data URI can be used inside CSS url() values, although large embedded images can make the stylesheet harder to cache and maintain.
Which image formats are supported?
The supported formats depend on the files accepted and correctly identified by the workspace. Only advertise JPG, PNG, WebP, GIF, SVG, BMP, or other formats after each one has passed production testing.
Why is the Base64 string larger than the image file?
Base64 represents every three input bytes with four text characters. This causes an expansion approaching 33.3% for larger files, before adding a Data URI prefix or surrounding markup.
Is my image uploaded to a server?
The selected image is encoded inside the browser workspace. Use a stronger no-upload statement only after confirming the production network behaviour.