You can calculate the file size (in bytes) using below formula:

x = (n * (3/4)) - y

Where:

1. x is the size of a file in bytes

2. n is the length of the Base64 String

3. y will be 2 if Base64 ends with '==' and 1 if Base64 ends with '='.

You can read the algorithm here Base 64 wiki

Answer from Harish Moyal on Stack Exchange
Discussions

php - Base64 decoded image file size - Software Engineering Stack Exchange
I know base64 encoded binary data is equal to 1.37 times the original data according to Wikipedia. What if you decode that string with php function base64_decode($encoded_string); and then make i... More on softwareengineering.stackexchange.com
🌐 softwareengineering.stackexchange.com
How to measure the size of base 64 image?
I have an image already encoded in base64, how can I determin how big this object is. I want to tell the user of an application, if this file will take very long to transfer. ... If you have encoded to a Base64 string, then the size that you're transferring across the wire is simply the count ... More on stackoverflow.com
🌐 stackoverflow.com
Is it a good idea to convert images into base64 string and save them in database?
You will get many people saying don't because if not implemented well it can bite ya. Don't be concerned about "optimal", tell yourself "good enough" and "don't let perfect be the enemy of good". But I have done this for several projects (enterprise, mission critical systems) and it works just fine. From the developer standpoint I love it. When I yank the "byte array" from the DB I can just pass it right to the client as-is, just adding on the mime type, super simple. Right now I have a system that stores photos of students at a university and we serve those out with an API. All done in C# and SQL Server. We have probably 50,000 photos and it is lighting fast. That being said I have also stored files in S3 and just stored the name in the DB. I will usually rename the file so S3 stores it efficiently. I like things like s3 better than a file system since you can dedicate a bucket just to this and some sysadmin won't go mucking around. I agree with you on storing the unstructured binary data along with your structured data for simplicity. I had a project once that wrote images to a filesystem. Years later older images were lost due to some file system cleanup process. Over my 25 years programming I have found you cannot guess where the issues will be. Don't optimize until you measure it. If you can build a proof of concept then hammer it, do it. Write a script that inserts and reads images and loop it for a few hours. See what happens. Also if you implement something and a few years later it starts to falter, then refactor. But you need to monitor! This is another place where people get bit, they do something like this and just let it run in a corner until it "just dies for no reason" one day. You or someone needs to care and feed for it. I am going to assume on-prem Microsoft SQL Server, your usage may vary. Use "VARBINARY(MAX)" for the field type https://docs.microsoft.com/en-us/sql/t-sql/data-types/binary-and-varbinary-transact-sql?view=sql-server-ver15 Put it in a table by itself with an id to look it up. Store filename, and filetype(mime-type) too if needed. Slap an index on the id, reference that id in other tables. Critical: Put the table in its own filegroup separate from the other tables. This will allow for faster restores since you can restore the primary filegroup and get your DB back online ASAP, then as it is up and running restore the filegroup with the images. So instead of the whole DB being down for hours, it is up in minutes with only the images being unavailable for hours. https://docs.microsoft.com/en-us/sql/relational-databases/databases/database-files-and-filegroups?view=sql-server-ver15 As with all DBs the filegroups should not be on the system (C) drive, maybe even put the images table filegroup on its own drive with nothing else. Do set a limit on the incoming filesize in your code. Someone could send in a few Blu-ray ISOs and cripple your system. In .NET code you store the image data in a byte array "byte[] " variable. That byte array is what is stored in the varbinary(max) DB field. It is easy to convert the incoming image/file to byte[], just google around for that there are many easy to do it. Design and implement a cleanup process and purge old unneeded records. No one ever seems to do this and the data grows forever until the system is retired/migrated. For small DBs it's not a problem but for this you must go back to your stakeholder and get in writing when you can delete old records then set an automated process or scheduled manual progress to do it. If they need the data long term maybe older data can be moved out of the online system and to an archive or warehouse. Microsoft has some other methods that I have not used. Check those out. https://docs.microsoft.com/en-us/sql/relational-databases/blob/binary-large-object-blob-data-sql-server?view=sql-server-ver15 -- Good luck More on reddit.com
🌐 r/dotnet
111
46
April 28, 2022
How to resize or change resolution of base64 image through manipulating the base64 code?
There are lots of examples of encoding images to Base64. Is there a way of changing the size or resolution of that image by simply manipulating the actual base64 encoded content? Your base64 code m... More on stackoverflow.com
🌐 stackoverflow.com
People also ask

Why is my base64 string larger than the original image?
Base64 encoding increases file size by approximately 33% because it represents 3 bytes of binary data as 4 ASCII characters.
🌐
base64-image.de
base64-image.de › home › faq
FAQ - Base64 Image Encoder
Can I decode base64 back to an image?
Yes. Base64 encoding is fully reversible. You can decode any base64 string back to the original image without quality loss.
🌐
base64-image.de
base64-image.de › home › faq
FAQ - Base64 Image Encoder
What is base64 encoding?
Base64 is a binary-to-text encoding scheme that converts binary data (like images) into a string of ASCII characters. This allows images to be embedded directly in HTML, CSS, or JSON without a separate file.
🌐
base64-image.de
base64-image.de › home › faq
FAQ - Base64 Image Encoder
🌐
Kutia
kutia.net › resizing-base64-images-with-javascript-a-snap-to-scale
Resizing Base64 Images with JavaScript: A Snap to Scale - Kutia
January 31, 2023 - This function takes in a single parameter, the base64 encoded image, and returns a promise that resolves with the resized image in the form of a data URL. The function also sets a maximum image size limit of 4MB in bytes, which ensures that ...
🌐
Base64 Image Encoder
base64-image.de › home › faq
FAQ - Base64 Image Encoder
For example, a 100 KB image becomes roughly 133 KB as a base64 string. Despite this overhead, base64 eliminates a separate HTTP request, which can improve perceived load time for small images (under 10 KB).
🌐
Base64 Image Encoder
base64-image.de
Convert Images to Base64 Online — Free Encoder & Optimizer | base64-image.de
This tool supports all common web image formats: JPEG, PNG, GIF, WebP, SVG, BMP, ICO, TIFF, AVIF, and HEIC/HEIF. Each format can be compressed before encoding to reduce the base64 output size.
🌐
Lioncoding
lioncoding.com › calculate-a-file-size-from-base64-string
Calculate a file size from Base64 string
February 20, 2022 - By using the same example we saw earlier and adding the MIME type, we get this: data:text/plain;base64,SGVsbG8gd29ybGQgIQ== If the MIME-type is not removed first, the total size will be completely wrong, we should get something around 30 bytes.
Find elsewhere
🌐
Bunny.net
bunny.net › blog › why-optimizing-your-images-with-base64-is-almost-always-a-bad-idea
Why optimizing images with Base64 is almost always a bad idea
June 6, 2025 - Take, for example, a 1x1 pixel transparent PNG. Despite the original image being only 68 bytes in size, factoring in the HTTP headers, it still ends up being bigger than the Base64-encoded string:
🌐
Laracasts
laracasts.com › discuss › channels › javascript › how-to-get-size-width-x-height-from-base64-image
How to get size (width x height) from base64 image?
I would like to get the size of an image loaded using axios : const arrayBufferToBase64 = (buffer)=> { let binary = ''; let bytes = [].slice.call(new Uint8Array(buffer)); bytes.forEach((b) => binary += String.fromCharCode(b)); return window.btoa(binary); }; (...) const response = await axios.get( api + '/image/'+params.name, {headers: headers, responseType: 'arraybuffer'} ).then((res)=>{ let base64Flag = 'data:image;base64,'; let imageStr = arrayBufferToBase64(res); setPictureContent(base64Flag + imageStr); setNoPictureAvailable(''); }) ; The image is in the variable pictureContent and I can display it correctly when src={pictureContent} The question now is : how to get the pictureContent width and height ?
🌐
Daniel Lemire's blog
lemire.me › blog › 2019 › 01 › 30 › what-is-the-space-overhead-of-base64-encoding
What is the space overhead of Base64 encoding? – Daniel Lemire's blog
October 31, 2022 - Thus the base64 version of a file is at least 4/3 larger than the the original: we use at least 33% more storage than the original file size when encoding a file as base64. That sounds bad.
🌐
GitHub
gist.github.com › ORESoftware › ba5d03f3e1826dc15d5ad2bcec37f7bf
resizing an image on the front-end before sending to a server · GitHub
I've done a few myself and got slightly better results by passing the native type and 1 to toDataUrl method (canvas.toDataURL('image/png', 1). ... function resizeImage(base64Str, maxWidth = 400, maxHeight = 350) { return new Promise((resolve) ...
🌐
Microsoft Fabric Community
community.fabric.microsoft.com › t5 › Desktop › Getting-image-size-perfect-for-base64 › td-p › 2523935
Getting image size perfect for base64 - Microsoft Fabric Community
May 24, 2022 - So I've been running into a clipping issue with my base64-converted images. Some images are too large, some have too much quality, and some are the wrong extension for the application, and I'm trying to find a happy medium, a ballpark estimate of number of pixels LxW to streamline the base64 ...
🌐
Base64.Guru
base64.guru › home › base64 converter › base64 encode
Image to Base64 | Base64 Encode | Base64 Converter | Base64
Sometimes you have to send or output ... can encode image to Base64 and embed it using the data URI. Please note that the image to Base64 encoder accepts any images types with a size of up to 50 MB....
Top answer
1 of 2
17

It is possible by using the <canvas> element

var canvas = document.createElement("canvas");
var ctx = canvas.getContext("2d");

canvas.width = 32; // target width
canvas.height = 32; // target height

var image = new Image();
document.getElementById("original").appendChild(image);

image.onload = function(e) {
    ctx.drawImage(image, 
        0, 0, image.width, image.height, 
        0, 0, canvas.width, canvas.height
    );
    // create a new base64 encoding
    var resampledImage = new Image();
    resampledImage.src = canvas.toDataURL();
    document.getElementById("resampled").appendChild(resampledImage);
};

image.src = "data:image/png; base64,iVBORw0KGgoAAAANSUhEUgAAADAAAAAwCAMAAABg3Am1AAABaFBMVEVMoUNNoURNokROokVPokZQo0dRpEhSpElTpUpUpUtVpk1Wpk1Xp09ZqFBZqFFaqFJbqVJbqVNdqlVeqlVeq1Zgq1hgrFhirFpirVtlrl5mr15nr2BpsGFqsWNssmRus2dvs2hwtGlxtGlytWtztWx4uHF5uXJ6uXR+u3d/vHiAvHqBvXqCvXyDvn2Evn6HwIGMw4aPxImRxYuUx46Vx5CWyJGbypacy5egzZuizp2kz5+kz6Cm0KGn0aKp0qSp0qWs1Kiu1Kmw1ayy1q602LC12LG627e83Lm+3bvF4cPI4sXJ48bK48jL5MjM5MrN5cvP5szQ5s7R58/S59DU6dLV6dPa7Nnf7t7g79/h79/i8OHj8OLk8ePm8uXn8ubp9Ojq9Onr9ers9evt9ezu9u3v9+7w9+/x+PDy+PHy+PL0+fP0+fT1+vX2+vX3+/f4+/j5/Pj5/Pn6/Pr7/fv9/v3+/v7+//7////VxbUKAAABVElEQVR4Ae3L65cKcQDH4e9YYe2utMnFsuu+rCyFKDVyKSTG/X6RVO5Ums+/r3HQOZz5zcyb7U3P+0drbcLKVRVF/DacVQSbnkL/oCJY+Axv4orgpAvOlMJYvJXU0GWgqBBSDd4ekhR7CINjClYGellJ21rwfocCWYUBcDUmHfkBj2IKlv4EPEhI54GKQlh4DjQOyKoBp2Syf1qemZtAN6PZV/Btr/xNdz5cnNeQVXKBK+uXvsNd+TsH9K4taujEF+D+1iw35uTP7gK4zlFL2vMCeL1vWUaJC208jzMbNFsHzijIxtPP8LzLz62z3UsKwTp+D8/Xyq7DUzKZ36nflq73AXpbZFSi49irKXm2lz9CVWZ3+KVVL6aT0ubcy90ye8JIs1ZYiStIYqVQa/JXXr4A/ZFMF+stPMsSYAojqVXbac+EDiPmwD/GH/431mAwhmA28RMGFbXqgVfHnwAAAABJRU5ErkJggg==";
<p>Original (48x48)</p>
<div id="original"></div>
<p>Resampled (32x32)</p>
<div id="resampled"></div>

2 of 2
0

Well this is very old but you can manipulate it using it as background with an url and background-size:contain; That way the image should show inside the height and with you establish and not be clipped by the container.

🌐
KeyCDN
keycdn.com › support › image-base64-encoding
Image Base64 Encoding - KeyCDN Support
February 25, 2023 - Base64 is only useful for very small images. This is because when it comes to larger images, the encoded size of a picture in bytes will end up being much larger than JPEGs or PNG files.
🌐
xjavascript
xjavascript.com › blog › how-to-find-the-base64-image-width-and-height
How to Find Original Width and Height of a Base64 Image: Solving Thumbnail vs Original Size Issues — xjavascript.com
Decode Base64 to binary: Convert the Base64 string into a byte stream. Open the image with Pillow: Use Image.open() to load the binary data. Extract dimensions: The size property returns a tuple (width, height).
🌐
Stack Overflow
stackoverflow.com › questions › 36352184 › what-is-the-ratio-between-an-image-size-and-the-length-of-its-base64-string-conv
What is the ratio between an image size and the length of its base64 string conversion?
I have a 200Kb jpg and I want to convert it into a base64 string. How long will that base64 string be approximately ? The reason I'm asking is because I'd like to store that image as a base64 str...