HTML5 image to base64 format asynchronous upload

Source: Internet
Author: User

Because of this demand (mobile side), I studied it and found it quite good.
This mainly uses the html5 API, does not require other JS plugins, but only browsers that support html5, and now most of them should be supported.
<!DOCTYPE html>
<html>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">
<head>
<title></title>
</head>
<body>

<img id="expImg"/>
<img id="img" style="display:none"/>
<div><input type="file" id="file"></div>
<div id="btn" style="font-size:48px; line-height:60px">OK</div>

</body>
<script type="text/javascript" src="http://libs.baidu.com/jquery/1.9.0/jquery.min.js"></script>
<script type="text/javascript">


$(document).ready(function(){
Web.main();
});

Var web={
Main : function(){
Web.init();
web.getImgUrl();
},
Init : function(){
this.file_Id=$("#file");
this.img_Id=$("#img");
this.expImg_Id=$("#expImg");
this.btn_Id=$("#btn");
This.c=document.createElement("canvas");
This.ctx=this.c.getContext("2d");
},
getImgUrl : function(){
Var fileUrl="", imgDataUrl="", reader=new FileReader(), imgW=0, imgH=0, expW=0, expH=0, imgMax=800;

web.btn_Id.click(function(){
imgW=0;
imgH=0;
expW=0;
expH=0;
web.img_Id[0].src="";

/ / Convert the picture to base64 format
fileUrl=web.file_Id[0].files[0];
reader.readAsDataURL(fileUrl);

Reader.onload=function(e){
web.img_Id[0].src=this.result;

imgW=web.img_Id.width();
imgH=web.img_Id.height();

/ / Change the image size, this is based on their actual needs to write algorithms
If(imgW>imgMax&&imgW>=imgH){
expW=imgMax;
expH=parseInt((imgMax*imgH)/imgW);
}else if(imgH>imgMax&&imgH>imgW){
expH=imgMax;
expW=parseInt((imgMax*imgW)/imgH);
}else{
expW=imgW;
expH=imgH;
}

Web.c.width=expW;
Web.c.height=expH;

web.ctx.drawImage(web.img_Id[0],0,0,expW,expH);

imgDataUrl=web.c.toDataURL(); //Default output PNG format
//imgDataUrl=web.c.toDataURL("image/jpeg",0.8); //Set the output jpg format, the second parameter is the image quality
web.expImg_Id[0].src=imgDataUrl;
}
})
}
}
</script>
</html>







Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.