您的位置:首页 > Web前端 > JavaScript

js 压缩并解决iphone上传头像偏转

2016-06-22 17:11 573 查看
download url : https://github.com/exif-js/exif-js

/* ======================================== 图片压缩方法 ======================================== */
function compressImage(maxWidth,maxHeight,srcOriginalImage,orientation,callback){
// 创建 canvas
var canvasId ="canvas_"+new Date().getTime()+""+parseInt(Math.random());
$("<canvas></canvas>").hide().attr("id",canvasId).appendTo("body");

var c=$("#"+canvasId)[0];
var ctx=c.getContext("2d");

// 释放canvas
function releaseCanvas(){
$("#"+canvasId).remove();
}

// 创建要绘制的Image对象
var img = new Image();
img.src = srcOriginalImage;
// 等待img加载完毕
img.onload = function(){
// 与backgournd-size:contain效果相同
if(img.width/img.height<maxWidth/maxHeight){
c.height = img.height;
if(img.height>maxHeight){
c.height = maxHeight;
}
c.width = img.width/img.height*c.height;
}else{
c.width = img.width;
if(img.width>maxWidth){
c.width = maxWidth;
}
c.height = img.height/img.width*c.width;
}

var compressImageWidth = c.width;
var compressImageHeight = c.height;

if(orientation==6){
var tmp = c.width;
c.width = c.height;
c.height= tmp;

ctx.translate(c.width,0);
ctx.rotate(Math.PI/180*90);
}
ctx.drawImage(img,0,0,img.width,img.height,0,0,compressImageWidth,compressImageHeight);

callback(c.toDataURL());

releaseCanvas();
};
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  html javascript web