Image Base64 and data URLs: format, size and recovery

Separate bare Base64 from data URLs, calculate encoded length and recover PNG, JPEG, GIF, WebP or BMP with its original bytes and correct extension.

Content reviewed ·

The problem and context

Small icons embedded in HTML or CSS often use Base64 inside a data URL. Base64 represents the binary content; the URL adds a media type and encoding marker. A data:image/png;base64, prefix alone does not prove that the following bytes form a PNG. Inspecting the bytes and decoding the image are separate checks.

Method and calculation

Base64 produces four characters for each group of three bytes. Its padded length is 4 × ceil(n / 3), plus a separate prefix for a data URL. Bare JPEG must be recognized as image/jpeg rather than labelled PNG. Encoding bytes directly avoids canvas re-encoding, preserving animation, transparency and metadata.

Worked example

Twelve bytes produce sixteen Base64 characters; one byte needs four. A restored JPEG downloads as .jpg, while BMP uses .bmp. Copy a complete data URL from the encoder, paste it into the reverse mode and compare the downloaded bytes with the original file. A matching picture alone is a weaker check.

Checklist

Reduce files larger than 10 MiB first. A separate file URL is often more practical for large images than a long string inside a page. Remove line wrapping when needed, and obtain the original again when its type is inconsistent or decoding fails. Base64 does not hide content or remove location metadata.

Image Base64 converter

Up to 10 MiB: PNG, JPEG, GIF, WebP or BMP. Preview: at most 16384 pixels per side and 32 million pixels in total.

Image upload

Drop an image here, or focus a button in this area and paste an image.

Use these tools with this article

  • File Hash Calculator

    Generate several checksums for one file, copy one or all, download results and compare an expected value. Choose any of MD5, SHA-1, SHA-256 and SHA-512.

  • Image Base64 Converter

    Encode PNG, JPEG, GIF, WebP and BMP, or restore the original image from Base64.

  • Image Compressor

    Resize images in your browser, export JPEG, PNG or WebP, compare actual file sizes and choose a JPEG background for transparent areas.

Sources