MattKetmo / darkroomjs

Extensible image editing tool in your browser
https://mattketmo.github.io/darkroomjs
MIT License
1.42k stars 405 forks source link

Save cropped data as a file #100

Open yadavpeoplelink opened 8 years ago

yadavpeoplelink commented 8 years ago

Can we save cropped image canvas data as a image file??

melefabrizio commented 8 years ago

I use this in the plugin options:

plugins: {
     ...
     save:{
              callback: function(){
                               this.darkroom.selfDestroy();
                               editorSaveCallback(this.darkroom.sourceCanvas.toDataURL());
                               }
     }
 }

The elaborated image in base64 is passed to my editorSaveCallback function, where I POST it via AJAX to a php script and do something similar to this:

    $file = fopen($fileName, "wb");

    $data = explode(',', $_POST['imagedata']);

    fwrite($file, base64_decode($data[1]));
    fclose($file);

This converts the image back to binary and writes it to a file, the format is the same of the image you called Darkroom on.