# 如何改变图像的真实大小,实现网页上传任意大小的图像?
我想从本地上传一张图片,上传之前我想对其降采样或者放大,我改变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);
};
});
canvas裁剪后,截屏再上传
http://segmentfault.com/a/1190000000754560
这里有个帖子:如何用JS改变图像的原始大小
不能吧 图片本身并不属于html的内容
js操作的应该是html的标签内容
function resizeTo(img,imgWidth,imgHeight)
{
img.style.width=imgWidth;
img.style.height=imgHeight;
}
resizeTo(img,800,600);
不知道有没有帮助
用html5 canvas或者用nginx image filter裁剪图片
context.drawImage(image,0,0,image.width,image.height)之后用canvas.toDataURL()把调整大小后的图片转换为url就可以了