您的位置:首页 > 运维架构 > 网站架构

网站用户头像剪裁上传完整案例

2016-08-23 18:28 537 查看
做为网站前段开发人员来说,用户头像剪裁和上传是一个很常用的功能,一般这个功能涉及到图片的放大,缩小,移动,旋转,和剪裁。下面我们来做一个完整的demo,剪裁后的图片以base64的形式返回,base64怎么上传到后台服务器,很简单,这里不做介绍。图片的操作:手机端操作和其他手机图片应用操作没有任何区别。
PC端:通过鼠标的滚轮是实现图片的放大缩小,长按左键移动鼠标实现图片的移动,双击图片现实图片的旋转。demo下载地址:下载1 下载2

在这个demo中,我们使用Jquery的插件(jquery.photoClip.js)完成。【在我的下一个博客我们分析下photoClip的源码实现】。在使用jquery.photoClip.js,我们还得添加几个依赖插件:iscroll-zoom.js(实现图片的移动)、hammer.js、lrz.all.bundle.js。(这3个js扩展库,在我给出的demo下载地址一并给出)。下面是简单实现的源码:
<!doctype html>
<html lang="zh-CN" id="index">
<head>
<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, minimal-ui" />
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="format-detection" content="telephone=no, email=no" />
<meta name="keywords" content="">
<meta name="description" content="">
<title>图片裁剪</title>
<style>
body {
margin: 0;
text-align: center;
}
#clipArea {
margin: auto;
height: 400px;
width: 400px;
}
#file,
#clipBtn {
margin: 20px;
}
#view {
margin: 0 auto;
width: 200px;
height: 200px;
}
</style>
</head>
<body ontouchstart="">
<div id="clipArea"></div>
<input type="file" id="file">
<button id="clipBtn">截取</button>
<div id="view"></div>

<script src="http://libs.baidu.com/jquery/2.1.1/jquery.min.js"></script>
<script src="js/iscroll-zoom.js"></script> <!--实现图片的移动-->
<script src="js/hammer.js"></script>
<script src="js/lrz.all.bundle.js"></script>
<script src="js/jquery.photoClip.js"></script> <!--实现图片的剪裁-->
<script>
//document.addEventListener('touchmove', function (e) { e.preventDefault(); }, false);
var clipArea = new bjj.PhotoClip("#clipArea", {
size: [260, 260],
outputSize: [640, 640],
file: "#file",
view: "#view",
ok: "#clipBtn",
loadStart: function() {
console.log("照片读取中");
},
loadComplete: function() {
console.log("照片读取完成");
},
clipFinish: function(dataURL) {
console.log(dataURL);
}
});
//clipArea.destroy();
</script>

</body>
</html>
如有兴趣可以加我的Q群一起讨论学习js,css,Python爬虫等技术。(QQ群:512245829)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  WEB 前端 图片剪裁
相关文章推荐