cropper下载地址:https://github.com/fengyuanchen/cropperjs/tree/master/src
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<link rel="stylesheet" type="text/css" href="css/cropper.css"/>
</head>
<style type="text/css">
/*S 修改头像*/
.l-btn{
display: inline-block;
outline: none;
resize: none;
border: none;
padding:10px 20px;
color: #fff;
font-size: 16px;
cursor: pointer;
background-image: linear-gradient(to bottom right, #2aaffc, #1269fa);
}
.hidden{
display: none;
}
.tailoring-container, .tailoring-container div, .tailoring-container p{
margin: 0;
padding: 0;
box-sizing: border-box;
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
}
.tailoring-container{
position: fixed;
width: 100%;
height: 100%;
z-index: 1000;
top: 0;
left: 0;
}
.tailoring-container .black-cloth{
position: fixed;
width: 100%;
height: 100%;
background: #111;
opacity: .3;
z-index: 1001;
}
.tailoring-container .tailoring-content{
position: absolute;
width: 768px;
height: 577px;
background: #fff;
z-index: 1002;
left: 0;
top: 0;
box-shadow: 1px 1px 50px rgba(0,0,0,.3);
padding: 10px;
}
.tailoring-content-one{
height: 47px;
width: 100%;
border-bottom: 1px solid #DDD ;
}
.tailoring-content .choose-btn{
float: left;
}
.tailoring-content .close-tailoring{
display: inline-block;
height: 30px;
width: 30px;
border-radius: 100%;
background-image: linear-gradient(to bottom right, #2aaffc, #1269fa);
color: #fff;
font-size: 22px;
text-align: center;
line-height: 27.6px;
float: right;
cursor: pointer;
}
.tailoring-content .close-tailoring:hover{
background-image: linear-gradient(to bottom right, #2aaffc, #1269fa);
}
.tailoring-content .tailoring-content-two{
width: 100%;
height: 460px;
position: relative;
padding: 5px 0;
}
.tailoring-content .tailoring-box-parcel{
width: 520px;
height: 450px;
position: absolute;
left: 0;
border: solid 1px #ddd;
}
.tailoring-content .preview-box-parcel{
display: inline-block;
width: 228px;
height: 450px;
position: absolute;
right: 0;
padding: 4px 14px;
}
.preview-box-parcel p{
color: #555;
}
.previewImg{
width: 200px;
height: 200px;
overflow: hidden;
}
.preview-box-parcel .square{
margin-top: 10px;
border: solid 1px #ddd;
}
.preview-box-parcel .circular{
border-radius: 100%;
margin-top: 10px;
border: solid 1px #ddd;
}
.tailoring-content .tailoring-content-three{
width: 100%;
height: 40px;
border-top: 1px solid #DDD ;
padding-top: 10px;
}
.sureCut{
float: right;
}
.avatarbox{
width: 85px;
height: 85px;
border-radius:50%;
overflow:hidden;
position: relative;
float: left;
}
.avatarbox p{
position: absolute;
top: 0;
left: 0;
z-index: 1;
text-align: center;
line-height: 85px;
width: 100%;
height: 100%;
font-size: 12px;
color: #fff;
cursor: pointer;
background-color: rgba(0,0,0,.5);
border-radius:50%;
margin-top: 0;
}
.avatarbox:hover p{
display: inherit;
}
/*E 修改头像*/
</style>
<body>
<!--S 头像上传-->
<div class="avatarbox" id="replaceImg">
<p>点击更改头像</p>
<img id="finalImg" class="imgPart pic" src="img/HBuilder.png" width="100%">
</div>
<!--S 图片裁剪框-->
<div style="display: none" class="tailoring-container">
<div class="black-cloth" οnclick="closeTailor(this)"></div>
<div class="tailoring-content">
<div class="tailoring-content-one">
<label title="上传图片" for="chooseImg" class="l-btn choose-btn trans-bg">
<input type="file" accept="image/jpg,image/jpeg,image/png" name="file" id="chooseImg" class="hidden" οnchange="selectImg(this)">
选择图片
</label>
<div class="close-tailoring" οnclick="closeTailor(this)">×</div>
</div>
<div class="tailoring-content-two">
<div class="tailoring-box-parcel">
<img id="tailoringImg">
</div>
<div class="preview-box-parcel">
<p>图片预览:</p>
<div class="square previewImg"></div>
</div>
</div>
<div class="tailoring-content-three">
<button class="l-btn sureCut trans-bg" id="sureCut">确定</button>
</div>
</div>
</div>
<!--E 图片裁剪框-->
<!--E 头像上传-->
</body>
<script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js" type="text/javascript" charset="utf-8"></script>
<script src="js/cropper.js" type="text/javascript" charset="utf-8"></script>
</html>
<script type="text/javascript">
//修改头像
function previewFile() {
var preview = document.querySelector('.uc_pic');
var file = document.querySelector('.up_pic').files[0];
var reader = new FileReader();
reader.addEventListener("load", function() {
preview.src = reader.result;
}, false);
if (file) {
reader.readAsDataURL(file);
}
}
//弹出框水平垂直居中
(window.onresize = function () {
var win_height = $(window).height();
var win_width = $(window).width();
if (win_width <= 768){
$(".tailoring-content").css({
"top": (win_height - $(".tailoring-content").outerHeight())/2,
"left": 0
});
}else{
$(".tailoring-content").css({
"top": (win_height - $(".tailoring-content").outerHeight())/2,
"left": (win_width - $(".tailoring-content").outerWidth())/2
});
}
})();
//弹出图片裁剪框
$("#replaceImg").on("click",function () {
$(".tailoring-container").toggle();
});
//图像上传
function selectImg(file) {
if (!file.files || !file.files[0]){
return;
}
var reader = new FileReader();
reader.onload = function (evt) {
var replaceSrc = evt.target.result;
//更换cropper的图片
$('#tailoringImg').cropper('replace', replaceSrc,false);//默认false,适应高度,不失真
}
//上传图片的大小,不能超过,100M
var f = file.files[0];
var size = parseInt(f.size) / 102400;
if (size > 100) {
alert('文件大于100M,请重新上传!');
return false;
}
reader.readAsDataURL(file.files[0]);
}
//cropper图片裁剪
$('#tailoringImg').cropper({
aspectRatio: 1/1,//默认比例
preview: '.previewImg',//预览视图
guides: false, //裁剪框的虚线(九宫格)
autoCropArea: 1, //0-1之间的数值,定义自动剪裁区域的大小,默认0.8
movable: false, //是否允许移动图片
dragCrop: true, //是否允许移除当前的剪裁框,并通过拖动来新建一个剪裁框区域
movable: true, //是否允许移动剪裁框
resizable: true, //是否允许改变裁剪框的大小
zoomable: false, //是否允许缩放图片大小
mouseWheelZoom: false, //是否允许通过鼠标滚轮来缩放图片
touchDragZoom: true, //是否允许通过触摸移动来缩放图片
rotatable: true, //是否允许旋转图片
crop: function(e) {
// 输出结果数据裁剪图像。
}
});
// 将base64转为blob
function dataURItoBlob(base64Data) {
var byteString;
if (base64Data.split(',')[0].indexOf('base64') >= 0)
byteString = atob(base64Data.split(',')[1]);
else
byteString = unescape(base64Data.split(',')[1]);
var mimeString = base64Data.split(',')[0].split(':')[1].split(';')[0];
var ia = new Uint8Array(byteString.length);
for (var i = 0; i < byteString.length; i++) {
ia[i] = byteString.charCodeAt(i);
}
return new Blob([ia], {
type: mimeString
});
}
//裁剪后的处理
$("#sureCut").on("click",function () {
if ($("#tailoringImg").attr("src") == null ){
return false;
}else{
var cas = $('#tailoringImg').cropper('getCroppedCanvas');//获取被裁剪后的canvas
var base64url = cas.toDataURL('image/png'); //转换为base64地址形式
$("#finalImg").prop("src",base64url);//显示为图片的形式
//关闭裁剪框
closeTailor();
var url = "";
$.ajax({
type: "post",
url: url,
async: true,
data: { _token: '{{csrf_token()}}',src:base64url},
success: function(r) {
window.location.reload();
}
});
}
});
//关闭裁剪框
function closeTailor() {
$(".tailoring-container").toggle();
}
</script>