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>