# 如何改变图像的真实大小,实现网页上传任意大小的图像?
我想从本地上传一张图片,上传之前我想对其降采样或者放大,我改变image.width和image.height可以让图片以任意大小显示,但是上传的时候任然是上传的原图。
**想问哈各位大神如何才能真正的改变图片的大小,实现上传任意大小的图片?**
这是我调试的代码:
input id="input_upload" type="file"/ >
canvas id="canvas" style="border:1px solid #c3c3c3;" >
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);
};
});