sorccu / node-jpeg-turbo

Limited libjpeg-turbo bindings for Node.js.
Other
39 stars 36 forks source link

recompress jpeg #6

Closed celevra closed 8 years ago

celevra commented 8 years ago

Hi,

thank you for your work! is it possible to recompress an allready compressed jpeg base64 str? I create a jpeg from a browser canvas element like canvas.toDataURL("image/jpeg", 1.0); then i send it to the server and send it to all other clients. Now it could be that there is one client with low bandwidth, so i want to compress the image more down like to a 0.5. is this possible with this library? can you guide me in the right direction?

regards

Celevra

sorccu commented 8 years ago

Decode the base64, then just use decompressSync followed by a compressSync.

celevra commented 8 years ago

thank you for your help this is working fine:

var jpg = require('jpeg-turbo')
var dataUriToBuffer = require('data-uri-to-buffer');
var Datauri = require('datauri');
var datauri = new Datauri();

var uri = 'data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAA...';
var img = dataUriToBuffer(uri);

var tmpImg = jpg.decompressSync(img, {format: jpg.FORMAT_RGBA})
var options = {
  format: jpg.FORMAT_RGBA,
  width: tmpImg.width,
  height: tmpImg.height,
  subsampling: jpg.SAMP_444,
}
var newImg = jpg.compressSync(tmpImg.data, options)

datauri.format('.jpeg', newImg);
console.log(datauri.content)