Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fabricjs Resize after upload image

How I can resize uploaded image before apply in canvas? My Codes are as follow:

HTML:

<input type="file" id="imgLoader">

JS:

$("#imgLoader").change(function(){
                var file = this.files[0];               
                importImage(file);
            });
function importImage(e){
         var reader = new FileReader();
         reader.onload = function (event) { 
            var imgObj = new Image();
            imgObj.src = event.target.result;
            imgObj.onload = function () {
                var image = new fabric.Image(imgObj);
                image.set({
                    //left: 250,
                    //top: 250,
                    angle: 0,
                    padding: 5,
                    cornersize: 10
                });
                canvas.add(image);

            }
        }
        reader.readAsDataURL(e);
     }

How can I implement it so that images will be resize in fixed width & height before apply in canvas?

like image 898
user3850712 Avatar asked Nov 26 '25 21:11

user3850712


1 Answers

Try this:

function importImage(e){
         var reader = new FileReader();
         reader.onload = function (event) { 
            var imgObj = new Image();
            imgObj.src = event.target.result;
            imgObj.onload = function () {
                var image = new fabric.Image(imgObj);
                image.set({
                    //left: 250,
                    //top: 250,
                    angle: 0,
                    padding: 5,
                    cornersize: 10
                });
                image.filters.push(new fabric.Image.filters.Resize({scaleX: 0.2, scaleY: 0.2}));
                image.applyFilters(canvas.renderAll.bind(canvas));
                canvas.add(image);

            }
        }
        reader.readAsDataURL(e);
     }
like image 182
Jibon Avatar answered Nov 29 '25 12:11

Jibon



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!