meltingice / CamanJS

Javascript HTML5 (Ca)nvas (Man)ipulation
http://camanjs.com
BSD 3-Clause "New" or "Revised" License
3.56k stars 406 forks source link

Server-side result saving in JPEG? #139

Open Renatico opened 10 years ago

Renatico commented 10 years ago

Please, anybody! Who can answer my question: Can I save the modified image via CamanJS in a format of JPG/JPEG? By default, the module saves the pictures using the algorithm of PNG. It would be wrong if we just save files with the extension of .jpg because the file's algorithm is PNG. Can anybody help me?

coagulant commented 10 years ago

Here is what I managed to come up with

var fs = require('fs');
Caman(src, function () {
    var caman = this;
    this.render(function () {
        var out = fs.createWriteStream(dst);
        var stream = caman.canvas.createJPEGStream({
            quality : 90
        });
        stream.pipe(out);
    });
});

src and dst are source and destination files.

Probably this should be built-in Caman API, becasue this low-lvel abstraction directly from canvas library.

Renatico commented 10 years ago

Thank you, Coagulant, very much! Your solution works for me!