Maybe string compression is the solution for you. This converts the data to byte arrays.
There are multiple implementations and algorithms around, for instance
LZMA-JS A standalone JavaScript implementation of the Lempel-Ziv-Markov chain (LZMA) compression algorithm.
my_lzma = new LZMA("./lzma_worker.js"); my_lzma.compress("This is my compression test.", 1, function on_compress_complete(result) { console.log("Compressed: " + result); my_lzma.decompress(result, function on_decompress_complete(result) { console.log("Decompressed: " + result); }, function on_decompress_progress_update(percent) { console.log("Decompressing: " + (percent * 100) + "%"); }); }, function on_compress_progress_update(percent) { console.log("Compressing: " + (percent * 100) + "%"); });lz-string: JavaScript compression, fast!
var str = "This is my compression test."; console.log("Size of sample is: " + str.length); var compressed = LZString.compress(str); console.log("Size of compressed sample is: " + compressed.length); str = LZString.decompress(compressed); console.log("Sample is: " + str);
Maybe string compression is the solution for you. This converts the data to byte arrays.
There are multiple implementations and algorithms around, for instance
LZMA-JS A standalone JavaScript implementation of the Lempel-Ziv-Markov chain (LZMA) compression algorithm.
my_lzma = new LZMA("./lzma_worker.js"); my_lzma.compress("This is my compression test.", 1, function on_compress_complete(result) { console.log("Compressed: " + result); my_lzma.decompress(result, function on_decompress_complete(result) { console.log("Decompressed: " + result); }, function on_decompress_progress_update(percent) { console.log("Decompressing: " + (percent * 100) + "%"); }); }, function on_compress_progress_update(percent) { console.log("Compressing: " + (percent * 100) + "%"); });lz-string: JavaScript compression, fast!
var str = "This is my compression test."; console.log("Size of sample is: " + str.length); var compressed = LZString.compress(str); console.log("Size of compressed sample is: " + compressed.length); str = LZString.decompress(compressed); console.log("Sample is: " + str);
I needed to generate thumbnails from larger pictures. I decided to solve my version of this problem with the HTML5 Canvas technique. I am using GWT so my code is:
//Scale to size
public static String scaleImage(Image image, int width, int height) {
Canvas canvasTmp = Canvas.createIfSupported();
Context2d context = canvasTmp.getContext2d();
canvasTmp.setCoordinateSpaceWidth(width);
canvasTmp.setCoordinateSpaceHeight(height);
ImageElement imageElement = ImageElement.as(image.getElement());
context.drawImage(imageElement, 0, 0, width, height);
//Most browsers support an extra option for: toDataURL(mime type, quality) where quality = 0-1 double.
//This is not available through this java library but might work with elemental version?
String tempStr = canvasTmp.toDataUrl("image/jpeg");
return tempStr;
}
If you are using JS you can probably get the idea:
- Make a canvas the size of the desired output image
- draw the input image on the canvas in the new size
- call canvas.toDataURL("mime-type", quality)
You can use any mime-type I think, but for me the jpeg one was the smallest and was comparable to results of my desktop image program.
My GWT would not let me do the quality parameter (and I'm not 100% sure how widely supported it is in which browsers), but that was OK because the resulting images were quite small. If you leave the mime-type blank it defaults to png which in my case was 3-4x larger than jpeg.
How to compress base64
how to compress a base64 image to custom size
How do we compress the size of a base64 image in Node.js?
c# - How to compress base64 string - Stack Overflow
» npm install compress-base64
Hai , i want to compress the size of base64 string , i can get the base 64 string and convert it into byte and then I can compress size of byte. But problem is this code work fine in my local when I move the code to dev environment it not work . Please share if anyone have idea on this issue
Well, base64 is just a text representation of some bytes.
First, you need to transform it into the buffer with bytes. E.g. like this:
const imageBuffer = Buffer.from(yourString, 'base64');
Then you should either save it on a disk and use some other tools like imagemagick to transform it:
fs.writeFileSync('your-image.png', imageBuffer); // in case you know it's PNG, ofc
// transform somehow, probably invoking third-party tool using child_process.execSync
Or you may use libs like Sharp to optimize the image in that buffer. Here's an example from the repo:
sharp(inputBuffer)
.resize(320, 240)
.toFile('output.webp', (err, info) => { ... });
Depending on the type of the image you manipulate and on the transformations you're trying to do, you may need different tools, not only Sharp, but also Gifsicle or Guetzli.
Years ago my team created a small CLI tool for optimizing images for our needs. The tool does not solve your problem, but maybe its code, where we work with different encoders, can help you somehow:
https://github.com/funbox/optimizt/blob/master/lib/optimize.js
Install library npm install sharp
Conversion to image, resize as you need, and then convert to base64
let arrData = `${imageBase64}`.split(',');
var imgBuffer = Buffer.from(arrData[1], 'base64');
const sharp = require('sharp');
let bufferImgCompressed = await sharp(imgBuffer)
.resize({ width: 100, height: 200 })
.toBuffer()
.then(data => { return data; })
.catch(err => { console.log('Error on compress'); });
let imgBase64Compressed = bufferImgCompressed.toString('base64');
The use of ${imageBase64}.split(',') is because to convert base64 to buffer the string should not to contains the 'data:image/...'
The simple answer: No - not without loosing the "printable string" nature
Usually PNG already uses sophisticated compression like it is used in ZIP files. Therefore compressing it before applying the base64 encoding will give you only very limited size reduction.
Applying the compression after the base64 encoding will make it to binary data again - in this case you could just skip the base64 encoding step.
If it is a problem with the network and not really the size of your string, this worked for me when I sent my images to a mongo database.
Using Express.js the limit of the bodyParser is defaulted to 1056k
so you can fix the problem by changing the limit as below.
app.use(bodyParser.urlencoded({ limit: '50mb',
extended: true
}));
app.use(bodyParser.json({ limit: '50mb' }));