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 ExchangeYou 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
Edit: apologies for blatantly wrong info. Acording to this Wikipedia article, it will be a bit larger:
Very roughly, the final size of Base64-encoded binary data is equal to 1.37 times the original data size + 814 bytes (for headers).
You can use ContentLength property of the request to determine what the size is in bytes, although if you are uploading more then one image, it might be trickier.
It will be approximately 37% larger:
Very roughly, the final size of Base64-encoded binary data is equal to 1.37 times the original data size
Source: http://en.wikipedia.org/wiki/Base64
It will be bigger in base64.
Base64 uses 6 bits per byte to encode data, whereas binary uses 8 bits per byte. Also, there is a little padding overhead with Base64. Not all bits are used with Base64 because it was developed in the first place to encode binary data on systems that can only correctly process non-binary data.
That means that the encoded image will be around 33%-36% larger (33% from not using 2 of the bits per byte, plus possible padding accounting for the remaining 3%).
php - Base64 decoded image file size - Software Engineering Stack Exchange
How to measure the size of base 64 image?
Is it a good idea to convert images into base64 string and save them in database?
How to resize or change resolution of base64 image through manipulating the base64 code?
Why is my base64 string larger than the original image?
Can I decode base64 back to an image?
What is base64 encoding?
I'm creating a dotnet Web API and need to store images. I've been converting the images to base64 and saving them in the database as string.
Is this a good idea or am I doing something wrong?
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>
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.