Skip to main content

How to Convert an Image to Base64 for Code

Convert an image to Base64 in four steps: upload, pick Data URL, raw, HTML, or CSS output, then copy or download. Works for PNG, JPG, SVG, GIF, and ICO.

CompressMyPic Editorial Team · Published Nov 17, 2026 · 10 min read

How to Convert an Image to Base64 for Code

You need an image as text: a logo for a stylesheet, an icon for an email, or a picture for a JSON payload. The conversion takes about a minute once you know the steps.

This guide walks you through converting an image to Base64 in four steps: upload the file, choose the output format, copy or download the string, and paste it into your code.

No software to install, no account to create, and nothing uploaded anywhere. The image is encoded locally in your browser from start to finish.

What You Need Before You Start

You need the image file itself, in PNG, JPG, WebP, GIF, SVG, BMP, or ICO format, up to 15 MB. Logos, icons, and small photos are the usual candidates.

Know where the string is going. An HTML page wants a Data URL, a stylesheet wants a CSS url() value, and an API usually wants the raw string without any prefix.

If the image is large, consider shrinking it first. A 2 MB photo becomes a 2.7 MB string, which is unwieldy to copy and slow to parse, while a compressed version may be a tenth of that.

A browser is all the software you need. The tool runs on Chrome, Safari, Firefox, and Edge, on desktop and on mobile, with no extension.

Step 1: Upload the Image or Paste a Screenshot

Open the image to Base64 converter and drag your image into the upload area. You can also click the area to choose a file from your device.

If the image is on your screen right now, copy it and paste it with Ctrl+V. The tool accepts clipboard images directly, so a screenshot can be encoded without saving a file first.

The tool reads the file and shows a preview with its dimensions and format. Check that the preview is the image you intended, because the string you copy will be this exact file.

If you uploaded the wrong file, encode another one from the tool. The previous output is replaced, and you can keep working without reloading the page.

Step 2: Choose the Output Format

Four tabs sit above the output area: Data URL, Raw, HTML, and CSS. Pick the one that matches where the string is going.

Choose Data URL when you need a complete URI for an image source or a stylesheet background. It includes the media type and the encoding marker, so it works anywhere a URL works.

Choose Raw when you are sending the image to an API or storing it in a database. Raw output is the string alone, with no prefix, which is the form most APIs expect.

Choose HTML when you want a ready image element. The tool generates the tag with the Data URL as the source and fills in the width and height attributes automatically. Choose CSS when you want a url() value for a background or a mask.

Step 3: Copy the Output or Download It

Click the Copy Output button to place the string on your clipboard. For short strings such as icons and logos, this is all you need.

For long strings, use the download button instead. Copying a multi-megabyte string can be slow or unreliable, while a downloaded text file carries the exact output with no truncation.

If your browser blocks clipboard access, the tool will tell you. Allow clipboard permission for the page, or use the download button as a workaround that needs no permission at all.

Check the overhead percentage shown beside the output. It tells you how much larger the string is than the original file, which is usually around 33 percent plus a little for the wrapper.

Step 4: Paste the String into Your Code

Paste the output where it belongs: the Data URL into an image source or a stylesheet, the raw string into a JSON field, the HTML snippet into a page, or the CSS value into a background property.

Test the result immediately. Open the page, send the test email, or call the API, and confirm the image appears. A string pasted into the wrong field will fail silently or show a broken image.

Keep the original image file. If the embedded version ever needs to change, you will re-encode from the original rather than decoding the string and re-encoding it.

For a logo or icon, consider saving the snippet in your project as a reusable variable or partial. That way the long string lives in one place instead of being pasted into every file that needs it.

SVG logos encode cleanly because they are already text. Upload the SVG file and the tool produces a Base64 string that decodes back into the same vector graphic.

The Data URL for an SVG starts with data:image/svg+xml;base64, followed by the string. Paste it into an image source and the logo renders sharply at any size.

Watch the size, though. A complex SVG with many paths can be large, and its Base64 form is a third larger again. A simple logo is fine; a detailed illustration may be better linked as a file.

For very small SVGs, consider a plain URL-encoded SVG instead of Base64. It is shorter, but it needs careful escaping, so Base64 remains the safer default for most people.

Encoding a Favicon

Favicon ICO files work in the converter just like any other format. Upload the .ico file and copy the Data URL for use in a single-file page or an email template.

Most sites should link the favicon as a file instead of embedding it. A linked favicon is cached by the browser, while an embedded one is downloaded with every page that contains it.

Embedding makes sense for single-file pages, offline documents, and email templates, where no separate file can be fetched. In those contexts the embedded icon is the only way the favicon appears at all.

If you do not have a favicon yet, the favicon generator can create one from any image, and then you can encode the result here.

Encoding Images for HTML Email

HTML emails cannot rely on external image files, because many clients block remote images by default. Embedding the image as a Data URL makes it appear immediately, even offline.

Keep embedded email images small. A logo or a small banner works well, but a large photograph makes the message heavy and slow to open on a phone.

Test before you send. Some email clients block inline Base64 images, and your message can arrive with broken pictures for those readers. Send a test to the clients your audience uses.

When a client blocks Base64, fall back to a hosted image with a normal URL. The hosted image needs the reader to load remote content, but it works in every client that allows it.

Encoding Images for an API JSON Payload

JSON cannot carry binary data, so image upload endpoints that accept JSON expect the image as a Base64 string. The raw output tab gives you exactly that form.

Copy the raw string into the JSON field the API documents, and send the media type in a separate field if the API asks for it. The server decodes the string and reconstructs the file.

Watch the payload size. A 1 MB image becomes a 1.4 MB JSON field, and some APIs reject payloads over a limit. Compress the image before encoding if the API has a size cap.

Test with a small image first. A successful round trip with a tiny icon proves the field names and the format are right before you send a large production image.

Shrinking an Image Before Encoding

Encoding a large image produces a very long string, and long strings are awkward to copy, slow to parse, and heavy to store. Shrinking first is almost always worth it.

Compress the image with the image compressor before you encode it. A photo reduced from 2 MB to 200 KB produces a string ten times shorter, with quality loss that is usually invisible.

Resize as well when the display size is small. An icon shown at 64 pixels does not need a 2000-pixel source, and the smaller source encodes into a far shorter string.

For photos, convert to JPG before encoding if transparency is not needed. JPG files are far smaller than PNG for photographs, and the smaller file means a shorter string.

Troubleshooting Clipboard Copy Problems

If the copy button does nothing, your browser may have blocked clipboard access. The tool will show a message explaining the block, and you can allow the permission for the page.

Some browsers only allow clipboard writes after a user gesture on a secure page. Clicking the button counts as a gesture, but an old browser or a restrictive setting can still refuse.

The download button is the reliable fallback. It saves the exact output as a text file with no clipboard involved, and you can open the file and copy from there.

On mobile, long strings can be slow to copy. Download the output file instead, then share it to the app where you need it, rather than fighting the clipboard.

Troubleshooting Very Long Strings

A long string is normal for a large image, but it can make your editor sluggish. Pasting a 2 MB string into a code file can freeze a lightweight editor for seconds.

Split the work: store the string in a separate file or variable, and reference it from the code that needs it. That keeps your main files readable and your editor responsive.

If the string breaks your build, check for line length limits. Some tools wrap or truncate very long lines, which corrupts the string. A downloaded file avoids that problem entirely.

When a string feels too long to manage, reconsider embedding at all. A linked image file is often the better choice for anything over a few hundred kilobytes.

Encoding Another Image After the First

To encode a second image, upload it from the tool without reloading the page. The new image replaces the old one, and the output updates to the new string.

Copy the first output before you switch, because replacing the image clears the previous string. A downloaded file is the safest record if you need both strings.

Working through a set of icons is fast this way. Upload, copy, upload the next, copy again, and the whole icon set is encoded in a few minutes.

Keep the original files organized while you work. A folder of source images and a matching folder of output text files prevents the mix-ups that happen when strings all look alike.

Encoding Images for Social Media and Messaging

Social posts and chat messages sometimes accept images only as text, especially in bots, webhooks, and automated workflows. A Base64 string lets an image travel inside a text-only message payload.

Keep these images tiny. A chat API may cap message size at a few dozen kilobytes, and a photo encoded to Base64 will blow past that limit before it even arrives.

For chatbots, a small icon or a thumbnail under 10 KB encodes into a string that fits comfortably in a message. Anything larger should be uploaded as a file and referenced by URL instead.

Some automation tools accept a Data URL where they would otherwise need a hosted image. That is useful when the image is generated on the fly and there is nowhere to host it yet.

Test the full round trip before you automate it. Send one message with the encoded image, confirm it renders on the receiving side, and only then build the workflow around it.

Remember that the string is visible to anyone who can see the message payload. Do not encode private photos into messages, logs, or webhooks where the text could be stored or forwarded.

Quick Reference Checklist

Upload the image, choose the output tab for your destination, and copy or download the string. Test it in place before you ship anything that depends on it.

Shrink large images before encoding, use raw Base64 for APIs, and prefer a linked file over an embedded string for anything large or shared across pages.

For the full background, read the Image to Base64: Complete Guide, and for the decision framework, see Base64 Images: When to Embed and When Not To. To decode a string back into a file, use the Base64 to image decoder.

Frequently asked questions

Can I convert an image to Base64 on my phone?

Yes. The converter runs in mobile browsers on iOS and Android. Upload the image from your gallery, choose the output tab, and copy the string. For very long strings, download the output file instead of copying, since mobile clipboards can struggle with large text.

Why does the HTML tab add width and height to the image tag?

The browser needs the dimensions to reserve space before the image decodes. Without them, the page layout shifts when the image appears, which hurts the reading experience and the layout shift score. The tool fills the attributes in automatically from the image dimensions.

Should I use Base64 or a normal image file for my website logo?

For a logo that appears on many pages, a normal linked file is usually better because the browser caches it once. Base64 wins when the page must be a single file, when the logo is tiny, or when no separate request is allowed. For most sites, link the logo.

What is the difference between a Data URL and raw Base64?

A Data URL is the complete URI with the data prefix, media type, and encoding marker, ready to use as an image source. Raw Base64 is the string alone, with no prefix, which is the form APIs and databases usually expect. The tool generates both from the same image.

Does converting to Base64 strip metadata from my image?

Encoding itself does not strip anything, because the string carries the exact original bytes. However, some workflows remove metadata before encoding to keep the string shorter. If privacy matters, strip the metadata first with the remove metadata tool, then encode the clean file.

Can I encode a screenshot directly without saving a file?

Yes. Copy the screenshot and press Ctrl+V in the tool, and it will be encoded immediately. This is the fastest way to get a Data URL for a screen capture, a UI element, or a diagram you are looking at right now.

Related guides

Developer Tools

How to Extract a Colour Palette

Extract a colour palette from any photo in five steps: upload, pick 3–12 swatches, read the percentages, and copy CSS or JSON — free, in your browser.

Nov 17, 2026 · 10 min readRead the guide →

Share this guide

← Back to the blog