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

发布网友

我来回答

2个回答

热心网友

用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);
};
});

热心网友

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

声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com