Save an image to camanjs

Asked

Viewed 20 times

0

Good afternoon,

I am using the plugin camanjs and I am not able to save the image to the server, I have the code like this:

<img id="camanimage" src="image.jpg" style="height:200px;">

In javascript:

Caman("#camanimage", function () {  
  this.brightness(10);
  this.contrast(10);

  this.render(function(){

    //this.save(); Funciona

    var imagedata = this.toBase64();

solution: this line was missing to remove the initial part of the date image:

    var raw_image_data = imagedata.replace(/^data\:image\/\w+\;base64\,/, '');


    $.post("salvafoto.php", { imagedata: raw_image_data },
      function(info){
      }
    );
// ... 

Then in the salvafoto.php I have so:

$encodedData = base64_decode($_POST['imagedata']);
$result = file_put_contents( 'teste.jpg', $encodedData );

When opening the image it is always in black with error. Does anyone know why? If you use this.save() download but it works.

  • What is the value of imagedata? Maybe you have some character that the function in php does not recognize..

  • appears this code(not complete because it does not fit: data:image/png;Base64,iVBORw0KGgoAANSUhEUgAAPgAAAEgCAYAACZy779AAAAAgAElEQVR4Xuy9+3Oc13nneYBGA2igceUFvEikRNEWbVmKFNvKWDveZJKtzGztz/sH7i9TW5utmpqtTG0qO/bGlRpnJzU19lijWIolxaJFUSBIBtooC/Afj7poqdsm3jksqikhhvfg7db7/99vme5/Z9vs

  • So have you solved it? Write a reply and mark as solved to help other people with the same question. Abçs.

1 answer

1

solution: this line was missing to remove the initial part of the date:image:

 var raw_image_data = imagedata.replace(/^data\:image\/\w+\;base64\,/, '');

Browser other questions tagged

You are not signed in. Login or sign up in order to post.