首先要说明,该方式我没有实现成功,主要问题点是压缩后能拿到压缩后图片地址,确不知道怎么上传上去,如果有哪位大牛可以指点一二,万分感谢。
图片压缩代码:
1 compress: function(source_img_obj, quality, output_format){ 2 var mime_type = "image/jpeg"; 3 if(output_format!=undefined && output_format=="png"){ 4 mime_type = "image/png"; 5 } 6 var cvs = document.createElement('canvas'); 7 //naturalWidth真实图片的宽度 8 cvs.width = source_img_obj.naturalWidth; 9 cvs.height = source_img_obj.naturalHeight; 10 console.log(source_img_obj); 11 var ctx = cvs.getContext("2d").drawImage(source_img_obj, 0, 0); 12 var newImageData = cvs.toDataURL(mime_type, quality/100); 13 console.log(newImageData); 14 var result_image_obj = new Image(); 15 result_image_obj.src = newImageData; 16 console.log(result_image_obj); 17 return result_image_obj; 18 },