-
Notifications
You must be signed in to change notification settings - Fork 1
/
adjustImg.html
213 lines (201 loc) · 7.99 KB
/
adjustImg.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
<link rel="stylesheet" href="css/page-common.css">
<link rel="stylesheet" href="font-awesome/css/font-awesome.min.css"/>
<link rel="stylesheet" href="css/upload.css"/>
<title></title>
<style>
*{
margin: 0;
padding: 0;
box-sizing: border-box;
}
html{
height: 100%;
}
body{
background: skyblue;
}
body{
height: 100%;
position: relative;
}
</style>
</head>
<body>
<button id='fileChooseButton' class="button blue rarrow file-input-mask">加载图片</button>
<img id="previewResult" />
<img id="needCropImg" style="display:none" />
<div class="app" id="uploadPage">
<div class="upload-loading">
<span class="centerXY"><i class="fa fa-spinner fa-pulse fa-3x fa-fw"></i></span>
</div>
<div class="bar"><a class="back pull-left" id="closeCrop"><i class="fa fa-reply"></i></a><a id="getFile" class="pull-right">使用</a></div>
<div class="main">
<canvas class="upload-mask">
</canvas>
<div class="preview-box">
<img id="preview"/>
</div>
<canvas class="photo-canvas">
</canvas>
<a id="rotateBtn">
<i class="fa fa-rotate-right fa-3x"></i>
<div>旋转照片</div>
</a>
</div>
</div>
</body>
<script src="js/require.js" ></script>
<script src="js/main.js"></script>
<script src="js/canvas-toBlob.js"></script>
<script>
var myCrop;
//防require缓存
require.config({
urlArgs:"bust="+new Date
})
require(["jquery",'hammer','tomPlugin',"tomLib",'hammer.fake','hammer.showtouch'],function($,hammer,plugin,T){
document.addEventListener("touchmove",function(e){
e.preventDefault();
})
//初始化图片大小300*300
var opts={cropWidth:300,cropHeight:300},
$file=$("#file"),
previewStyle={x:0,y:0,scale:1,rotate:0,ratio:1},
transform= T.prefixStyle("transform"),
$previewResult=$("#previewResult"),
$previewBox=$(".preview-box"),
$rotateBtn=$("#rotateBtn"),
$getFile=$("#getFile"),
$preview=$("#preview"),
$uploadPage=$("#uploadPage"),
$mask=$(".upload-mask"),
$loading=$(".upload-loading"),
maskCtx=$mask[0].getContext("2d"),
$needCropImg=$("#needCropImg");
//这是插件调用主体
myCrop=T.cropImage({
// bindFile:$file,//绑定Input file
bindFile:$needCropImg[0],//绑定一个图片
enableRatio:true,//是否启用高清,高清得到的图片会比较大
canvas:$(".photo-canvas")[0], //放一个canvas对象
cropWidth:opts.cropWidth, //剪切大小
cropHeight:opts.cropHeight,
bindPreview:$preview, //绑定一个预览的img标签
useHammer:true, //是否使用hammer手势,否的话将不支持缩放
oninit:function(){
},
onChange:function(){
$loading.show();
resetUserOpts();
},
onLoad:function(data){
//用户每次选择图片后执行回调
previewStyle.ratio=data.ratio;
$preview.attr("src",data.originSrc).css({width:data.width,height:data.height}).css(transform,'scale('+1/previewStyle.ratio+')');
myCrop.setCropStyle(previewStyle)
$loading.hide();
}
});
function resetUserOpts(){
$(".photo-canvas").hammer('reset');
previewStyle={scale:1,x:0,y:0,rotate:0};
$previewResult.attr("src",'').hide();
$preview.attr("src",'')
}
$('#fileChooseButton').on('click',function(){
resetUserOpts();
//单独绑定图片时用到
//只是为了演示,需要你来放一个真实的图片地址
$needCropImg[0].src='./img/9-1.jpg?'+Math.random();
})
$(".photo-canvas").hammer({
gestureCb:function(o){
//每次缩放拖拽的回调
$.extend(previewStyle,o);
console.log("用户修改图片",previewStyle)
$preview.css(transform,"translate3d("+ previewStyle.x+'px,'+ previewStyle.y+"px,0) rotate("+previewStyle.rotate+"deg) scale("+(previewStyle.scale/previewStyle.ratio)+")")
}
})
//选择图片
$rotateBtn.on("click",function(){
previewStyle.rotate+=90;
if(previewStyle.rotate>=360){
previewStyle.rotate-=360;
}
$(".photo-canvas").hammer('setRotate',previewStyle.rotate)
myCrop.setCropStyle(previewStyle)
$preview.css(transform,"translate3d("+ previewStyle.x+'px,'+ previewStyle.y+"px,0) rotate("+previewStyle.rotate+"deg) scale("+(previewStyle.scale/previewStyle.ratio)+")")
})
//获取图片并关闭弹窗返回到表单界面
$getFile.on("click",function(){
var cropInfo;
$uploadPage.hide();
myCrop.setCropStyle(previewStyle)
//自定义getCropFile({type:"png",background:"red",lowDpi:true})
cropInfo=myCrop.getCropFile({type:"jpeg"});
$previewResult.attr("src",cropInfo.src).show();
//可选传base64或者file对象
//cropInfo.src cropInfo.dfd
//you can upload img base64 :cheers:)
console.info('拿到Base64了,传给后台吧?')//src.substr(22)
/* $.ajax({
url:'http://127.0.0.1/testAdjustImg/upload.php',
type:"post",
dataType:"json",
data:{base64:cropInfo.src.substr(22),uploadType:'base64'},//后端无需在过滤头
success:function(data){
if(data.result==1){
console.log(data.imgPath)
}
}
})*/
//you can upload new img file :cheers:)
cropInfo.dfd.done(function(blob){
if(blob){
var formData=new FormData;
blob.name='imgFile'
formData.append("imgFile",blob)
formData.append("uploadType",'imgFile');
$.ajax({
url:'http://127.0.0.1/testAdjustImg/upload.php',
type:"post",
data:formData,
processData:false,
contentType: false,
dataType:"json",
success:function(data){
console.log(data)
if(data.result==1){
console.log(data.imgPath)
}
}
})
}
})
})
//上传文件按钮&&关闭弹窗按钮
$(document).delegate("#file","click",function(){
$uploadPage.show();
}).delegate("#closeCrop","click",function(){
$uploadPage.hide();
resetUserOpts();
myCrop.setCropStyle(previewStyle)
})
$file.one("click",showCropModal)
function showCropModal(){
$uploadPage.show();
$mask.prop({width:$mask.width(),height:$mask.height()})
maskCtx.fillStyle="rgba(0,0,0,0.7)";
maskCtx.fillRect(0,0,$mask.width(),$mask.height());
maskCtx.fill();
maskCtx.clearRect(($mask.width()-opts.cropWidth)/2,($mask.height()-opts.cropHeight)/2,opts.cropWidth,opts.cropHeight)
}
$needCropImg[0].addEventListener("load",showCropModal)
})
</script>
</html>