1

I'm trying to implement an image cropper on my website, but I'm getting some issues that I don't know how to get rid off.

Expected behavior : Once the users select their picture from the disk via the file input, it shows a modal where you have to crop the picture (with a pre-defined aspect ratio), and then they have to click on a "Crop" button to confirm what they have crop, in order to display the cropped picture into a img tag.

What's happening : The selected image appears for a millisecond on the modal and then pops away and prints a 404 error on the console.

I'm actually using Cropper

Here is my attempt :

    var image = document.getElementById('imageToCrop');
    document.getElementById("customFile").onchange = function (e) { // customFile is the id of my input file !
        if (typeof FileReader !== "undefined") {
            let size = e.target.files[0].size;
            if (size > 2048000) {
                $('#fileTooBigErrorModal').modal({
                    show: true
                })
                this.value = "";
                document.getElementById("customFileLabel").innerHTML = "Choose file";
            } else {
                document.getElementById("customFileLabel").innerHTML = e.target.files[0].name;
                image.src = ""
                var reader = new FileReader();
                var cropBoxData;
                var canvasData;
                var cropper;
                reader.onload = function (evt) {
                    image.src = evt.target.result;
                };
                reader.readAsDataURL(e.target.files[0]);
                $('#cropModal').on('shown.bs.modal', () => {
                    image.setAttribute('src', e.target.files[0].name);
                    cropper = new Cropper(image, {
                        viewMode: 3,
                        aspectRatio: 4 / 1,
                        ready: function () {
                            //Should set crop box data first here
                            cropper.setCropBoxData(cropBoxData).setCanvasData(canvasData);
                        },
                        crop: function (event) {
                            //test
                        }
                    });
                    $('#cropBtn').click((e) => {
                        cropper.getCroppedCanvas({ width: 800, height: 200 }).toBlob((blob) => {
                            $('#result').append('<img src="' + blob + '" alt="Result picture">');
                        });
                        $('#cropModal').modal('hide');
                    })
                }).on('hidden.bs.modal', function () {
                    cropBoxData = cropper.getCropBoxData();
                    canvasData = cropper.getCanvasData();
                    cropper.destroy();
                });
                $('#cropModal').modal('show');
            }
    
        }
    };
        <div class="modal fade" id="cropModal" tabindex="-1" role="dialog" aria-labelledby="cropModalLabel" aria-hidden="true">
            <div class="modal-dialog" role="document">
                <div class="modal-content">
                    <div class="modal-header">
                        <h5 class="modal-title" id="cropModalLabel">Crop</h5>
                        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                            <span aria-hidden="true">&times;</span>
                        </button>
                    </div>
                    <div class="modal-body">
                        <div>
                            <img id="imageToCrop" src="#" alt="Picture to crop">
                        </div>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
                        <button type="button" id="cropBtn" class="btn btn-primary">Crop</button>
                    </div>
                </div>
            </div>
        </div>

I was looking for solution for about 3 days now. Still can't figure it out. :/

Eliferd
  • 11
  • 8

1 Answers1

0

Instantiate the cropper Object after the image has been loaded.

Here's how I solved:

reader.onloadend = e => {
    this.imgTarget.attr("src", (e.target as any).result);
    this.cropperObj = new Cropper(this.imgTarget[0] as HTMLImageElement, { // option attibutes here
    });
}