<html>
<input type="file" id="file" onchange="upload()"/>
</html>
<script>
function upload(){
var file = document.getElementById('file');
var reader = new FileReader(file);
reader.onload = function(e){
var image = new Image();
image.src = this.result;
image.onload = function(){
var canvas = document.createElement('canvas'),
context = canvas.getContext('2d'),
imageWidth = image.width / 2, //压缩后图片的大小
imageHeight = image.height / 2,
data = ''
canvas.width = imageWidth
canvas.height = imageHeight
context.drawImage(image, 0, 0, imageWidth, imageHeight)
data = canvas.toDataURL('image/jpeg') //这里是图片
}
}
}
</script>
input上传图片压缩
最新推荐文章于 2023-10-08 16:39:29 发布