1

Im using this jQuery image cropper and I'm trying to send the crop image to the back end so after crop, I have to get the crop data (the result is canvas, using 'getCroppedCanvas') and then I tried to convert the canvas output to a blob

$("#image").cropper('getCroppedCanvas').toBlob(function (blob){})

but it throws me this error (refer below)

Uncaught TypeError: $(...).cropper(...).toBlob is not a function

Any help, clues, ideas, suggestions, recommendations to fix my issue please?

function readURL(input) {
    if (input.files && input.files[0]) {
        var reader = new FileReader();

        reader.onload = function (e) {
         $("#gallery_image").remove();
         $('input[name="image"]').next('#gallery_image').remove();
            $('input[name="image"]').after('<div id="gallery_image" style="margin-top:15px;"><div><img src="'+e.target.result+'" style="max-width:100%;" id="image"></div><div style="display:table;margin:15px auto 0px auto;"><button id="crop">Crop</button></div></div>');

            $(function () {
        var $image = $('#image');
        $image.cropper({
         viewMode: 1,
          dragMode: 'move',
          autoCropArea: 0.65,
          restore: false,
          guides: false,
          highlight: false,
          cropBoxMovable: false,
          cropBoxResizable: false,
          built: function () {
              $image.cropper('setCropBoxData', {
                width : 231,
                height : 180
              });
            // }
          }
        });
      });
        }

        reader.readAsDataURL(input.files[0]);
    }
}

$('input[name="image"]').change(function(){
    readURL(this);
});

 $(document).on("click","#crop", function(e){
    e.preventDefault();
  $("#image").cropper('getCroppedCanvas').toBlob(function (blob) {
      $("form").append('<input type="hidden" name="image" value="'+blob+'">');
    });
 });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/cropper/2.3.0/cropper.min.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/cropper/2.3.0/cropper.min.js"></script>


<form>
  <input type="file" name="image">
</form>
Juliver Galleto
  • 8,831
  • 27
  • 86
  • 164
  • 1
    You are using a browser that doesn't support the `HTMLCanvas.toBlob` method. Update your browser, or prefer some manual [dataURI_to_Blob conversion](http://stackoverflow.com/questions/4998908/convert-data-uri-to-file-then-append-to-formdata/5100158#5100158). – Kaiido Mar 08 '16 at 12:03
  • @Kaiido is exactly right: `.toBlob` browser support is not universal yet. Here's a few shims ([shim#1](https://github.com/eligrey/canvas-toBlob.js), [shim#2](https://github.com/blueimp/JavaScript-Canvas-to-Blob)) for browsers that don't support it. – markE Mar 08 '16 at 18:09
  • I have updated my browser (chrome) to a latest version but still the problem persist. Any ideas? – Juliver Galleto Mar 09 '16 at 01:13
  • ah i see, I just need to restart my browser, thank you. – Juliver Galleto Mar 09 '16 at 01:27
  • @CodeDemon. Glad you got it sorted :-) Since your question involves a non-reproducible situation, you might delete your question – markE Mar 10 '16 at 07:13

0 Answers0