Mobile H5 upload Avatar

Source: Internet
Author: User

<! DOCTYPE html>

<meta 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 "/>
<meta name = "Apple-mobile-web-app-capable" content= "yes"/>
<meta name = "Apple-mobile-web-app-status-bar-style" content= "Black"/>
<meta name = "Format-detection" content= "Telephone=no" >
&LT;TITLE&GT;HTML5 call phone to take photos and compress uploads </title>
<style>
*{
padding:0;
margin:0;
}
h1{
height:42px;
line-height:42px;
Background: #3548cc;
Color: #fff;
font-size:21px;
font-weight:400;
Text-align:center;
}
div.wrapper{
position:relative;
height:200px;
Overflow:hidden;
}
. user-avatar{
margin-top:20px;
font-size:16px;
Color: #3548cc;
margin-left:20px;
Display:inline-block;
}
. input-upload-image,.upload-btn{
width:100px;
height:100px;
Display:block;
Position:absolute;
top:20px;
left:120px;
}
. input-upload-image{
Z-index:1;
Border:none;
-webkit-opacity:0;
opacity:0;
}
. upload-btn{
Z-index:2;
}
. events-pointer-none{
Pointer-events:none;
Cursor:pointer;
}
. show-result{
padding:20px;
font-size:14px;
line-height:24px;
Color: #3548cc;
}
. Show-result. small{
font-size:12px;
Color:green;
}
. overlay{
Background:rgba (0, 0, 0, 0.5);
height:100%;
width:100%;
position:fixed;
top:0;
left:0;
z-index:99999;
Justify-content:center;
Align-items:center;
border:0;
Display:none;
}
. Overlay img {
width:31px;
height:31px;
}
</style>

<body>
<div class = "wrapper" >
<span class= "User-avatar" > User avatar:</span>
<input type = "File" accept= "image/*" capture= "Camera" id= "img" class= "Input-upload-image"/>

</div>
<div id= "Showresult" class= "Show-result" >
</div>
<div class= "Overlay" id= "overlay" >

</div>

<script>
' Use strict ';

var result1 = ', Result2 = ', Result3 = ', Result4 = ', Result5 = ', Result6 = ';

$ (' img '). AddEventListener (' Change ', function () {
$ (' overlay '). Style.display = ' flex ';

var reader = new FileReader ();

Reader.onload = function (e) {
var compressimg = compress (this.result,filesize);
};

Reader.readasdataurl (This.files[0]);

RESULT1 = this.files[0].size + ' Bytes ';

var fileSize = Math.Round (this.files[0].size/1024/1024);
}, False);

var compress = function (res,filesize) {
var img = new Image (),
MAXW = 200; Set Maximum width

Img.onload = function () {
var cvs = document.createelement (' canvas '),
CTX = Cvs.getcontext (' 2d ');

RESULT2 = Img.width;
RESULT3 = Img.height;

if (Img.width > Maxw) {
Img.height *= maxw/img.width;
Img.width = MAXW;
}

Cvs.width = Img.width;
Cvs.height = Img.height;

RESULT4 = Cvs.width;
RESULT5 = Cvs.height;

Ctx.clearrect (0, 0, cvs.width, cvs.height);
Ctx.drawimage (IMG, 0, 0, img.width, img.height);

var compressrate = getcompressrate (1,filesize);

var dataurl = Cvs.todataurl (' image/jpeg ', compressrate);

$ (' Imginfo '). SetAttribute (' src ', dataurl);

$ (' overlay '). Style.display = ' None ';

$ (' Showresult '). InnerHTML = "<p> picture size before compression:" +result1+ "<br/><p> before compression picture width:" +result2+ "<br/> <p> image Height before compression: "+result3+" <br/><p> compressed picture width: "+result4+" <br/><p> compressed picture height: "+result5+" </p><p class= ' small ' > Compressed picture size, can be obtained through NODEJS or backstage! </p> ";
};

IMG.SRC = res;
};

function Getcompressrate (allowmaxsize,filesize) {//Compute compression ratio, size in MB
var compressrate = 1;

if (Filesize/allowmaxsize > 4) {
Compressrate = 0.5;
} else if (Filesize/allowmaxsize >3) {
Compressrate = 0.6;
} else if (Filesize/allowmaxsize >2) {
Compressrate = 0.7;
} else if (FileSize > Allowmaxsize) {
Compressrate = 0.8;
} else{
Compressrate = 0.9;
}

RESULT6 = compressrate;

return compressrate;
}

function $ (ID) {
if (typeof id = = = ' String ' && id.constructor = = = String) {
return document.getElementById (ID);
}else{
Return
}
}
</script>
</body>


Original http://my.oschina.net/zyxchuxin/blog/700381

Mobile H5 upload Avatar

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.