cstefanache / angular2-img-cropper

Angular 2 Image Cropper
MIT License
364 stars 135 forks source link

Resampling #136

Closed soupman99 closed 7 years ago

soupman99 commented 7 years ago

Any chance of incorporating a resampling function to alleviate jagged edges when very large images are scaled & cropped?

I'd do it myself, but haven't been able to figure out how to incorporate a function like that.

cstefanache commented 7 years ago

Added sampleFn(canvas) as function property on the cropper settings where you can manipulate the output image - see example #3 from the sample app (or) : this.cropperSettings3.resampleFn = (buffer:HTMLCanvasElement) => {

        var canvasContext = buffer.getContext('2d');
        var imgW = buffer.width;
        var imgH = buffer.height;
        var imgPixels = canvasContext.getImageData(0, 0, imgW, imgH);

        for(let y = 0; y < imgPixels.height; y++){
            for(let x = 0; x < imgPixels.width; x++){
                var i = (y * 4) * imgPixels.width + x * 4;
                var avg = (imgPixels.data[i] + imgPixels.data[i + 1] + imgPixels.data[i + 2]) / 3;
                imgPixels.data[i] = avg;
                imgPixels.data[i + 1] = avg;
                imgPixels.data[i + 2] = avg;
            }
        }

        canvasContext.putImageData(imgPixels, 0, 0, 0, 0, imgPixels.width, imgPixels.height);
    };