<html>
<body>
<script>
function
getUrl(fil) {
var
Cnv = document.getElementById(
'myCanvas'
);
var
Cntx = Cnv.getContext(
'2d'
);
var
imgss =
new
Image();
var
agoimg=document.getElementById(
"ago"
);
for
(
var
intI = 0; intI < fil.length; intI++) {
var
tmpFile = fil[intI];
var
reader =
new
FileReader();
reader.readAsDataURL(tmpFile);
reader.onload =
function
(e) {
url = e.target.result;
imgss.src = url;
agoimg.src=url;
agoimg.onload =
function
() {
var
m = imgss.height / imgss.width;
Cnv.width = 300 ;
Cnv.height =300*m;
Cntx.drawImage(agoimg, 0, 0,300,300*m);
var
Pic = document.getElementById(
"myCanvas"
).toDataURL(
"image/png"
);
var
imgs =document.getElementById(
"press"
);
imgs.src =Pic ;
}
}
}
}
</script>
<input type=
"file"
id=
"fileId"
name=
"fileId"
value=
"上传图片"
hidefocus=
"true"
onchange=
"getUrl(this.files);"
/> <br/>
<canvas id=
"myCanvas"
style=
"display: block"
></canvas>
old img::<img src=
""
alt=
""
id=
"ago"
style=
"width: 500px;"
/>
new
img::<img src=
""
alt=
""
id=
"press"
/>
</body>
</html>