如何用JS改变图像的原始大小

2024-12-04 05:42:31
推荐回答(2个)
回答1:

用JS改变图像的原始大小方法:
var canvas = document.getElementById("canvas"蠢谨);

var context = canvas.getContext("2d");

$('input[type=file]').change(function(){

var file=this.files[0];
var reader=new FileReader();
var image=new Image();

reader.readAsDataURL(file);
reader.onload=function(){
// 通过 reader.result 来访问生成的 DataURL
var url=reader.result;
image.src=url;
alert(image.width);
alert(image.height);
image.height /=4;
image.width /=4;
canvas.setAttribute("width"派拍, image.width+"px"带羡基);
canvas.setAttribute("height", image.height+"px");
alert(image.naturalWidth);
alert(image.naturalHeight);
context.drawImage(image,0,0,image.width,image.height);
};
});

回答2:

看canvas的用法,键滚context.drawImage(image,0,0,image.width,image.height)之后用哗凯canvas.toDataURL()把调整大小后的图片转换乱亮唤为url就可以了