联系我们 - 广告服务 - 联系电话:
您的当前位置: > 综合 > > 正文

每日速讯:HTML+CSS布局制作 web前端期末大作业

来源:CSDN 时间:2022-12-23 10:38:57


(资料图片仅供参考)

在通过js对要上传图片进行压缩时,我利用的是html5中的canvas元素进行辅助,具体思路如下:

var reader = new FileReader();    //files[0]对象是从input type=file中获取的file对象    reader.readAsDataURL(files[0]);    //将file对象base64转码成功后进入    reader.onload = function () {        //声明img标签,并将未压缩的图片赋值给src属性        var image = $("");        image.attr("src", this.result);                image.on("load", function () {            //定义canvas的宽、高为72px            var square = 72;            var canvas = document.createElement("canvas");            canvas.width = square;            canvas.height = square;            var context = canvas.getContext("2d");            context.clearRect(0, 0, square, square);            var imageWidth;            var imageHeight;            var offsetX = 0;            var offsetY = 0;            //判断是根据原图片的宽压缩还是高压缩            if (this.width > this.height) {                imageWidth = Math.round(square * this.width / this.height);                imageHeight = square;                offsetX = - Math.round((imageWidth - square) / 2);            } else {                imageHeight = Math.round(square * this.height / this.width);                imageWidth = square;                offsetY = - Math.round((imageHeight - square) / 2);            }            context.drawImage(this, offsetX, offsetY, imageWidth, imageHeight);            //data即为压缩为72x72的压缩图片的base64内容            var data = canvas.toDataURL("image/jpeg");        });    };

当图片经过base64转码后获得的字符串是可以被当作图片使用,如:

data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAMCAgICAgMCAgIDAwMDBAYEBAQEBAgGBgUGCQgKCgkICQkKDA8MCgsOCwkJDRENDg8QEBEQCgwSExIQEw8QEBD/2wBDAQMDAwQDBAgEBAgQCwkLEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBD/wAARCABIAEgDASIAAhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQAAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJic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