I have been working a lot lately with image manipulation in node using the sharp library and having my service return a base64 encoding of the new image. My question seems simple: does a base64 encoded image always end with
==
=
sharp.max()
const sharp = require('sharp');
const request = require('request');
const bufferRequest = request.defaults({ encoding: null });
function imgFormatter(url, args) {
return new Promise((resolve, reject) => {
bufferRequest.get(url, args, function (err, res, body) {
let imgFormat = url.includes('png') ? 'png' : 'jpeg';
let resized = (args.crop)
? sharp(body).resize(args.width, args.height)
: sharp(body).resize(args.width, args.height).max()
resized.toFormat(imgFormat)
.toBuffer()
.then((output) => {
let newImage = "data:" + res.headers["content-type"] + ";base64," + new Buffer(output).toString('base64');
console.log(newImage);
resolve(newImage);
})
.catch((error) => {
reject(error);
})
});
})
}
data:image/jpeg;base64,/9j/.....RgX7p57pbYTvawQC580N4QcJtygGGgG/SLplthgBV2KCfCgIa3wWTVA3HySPmgP//Z
data:image/jpeg;base64,/9j/.....4qUhXQD5S5HzQhAM8KRz80IQH//2Q==
==
The =
at the end isn't a terminator, it's padding:
https://en.wikipedia.org/wiki/Base64#Output_padding
So whether you see it or not depends on the size in bytes of the object you are encoding.