質問編集履歴

1

edit_avatarのソースを追記。

2017/11/05 12:32

投稿

pharuq
pharuq

スコア6

test CHANGED
File without changes
test CHANGED
@@ -24,7 +24,9 @@
24
24
 
25
25
  ###該当のソースコード
26
26
 
27
- ```edit.html.erb
27
+ edit.html.erb
28
+
29
+ ```
28
30
 
29
31
  <% if @user.persisted? && @user.picture? %>
30
32
 
@@ -52,7 +54,9 @@
52
54
 
53
55
 
54
56
 
55
- ``` application.js
57
+ application.js
58
+
59
+ ```
56
60
 
57
61
  //= require rails-ujs
58
62
 
@@ -70,9 +74,9 @@
70
74
 
71
75
  ```
72
76
 
73
-
74
-
75
- ```assert.rb
77
+ assert.rb
78
+
79
+ ```
76
80
 
77
81
  Rails.application.config.assets.precompile += %w(
78
82
 
@@ -88,6 +92,230 @@
88
92
 
89
93
 
90
94
 
95
+ edit_avarar.js
96
+
97
+ ```
98
+
99
+ $(function(){
100
+
101
+
102
+
103
+ var fileName;
104
+
105
+
106
+
107
+ // 画像ファイル選択後のプレビュー処理
108
+
109
+ $('form').on('change', 'input[type="file"]', function(event) {
110
+
111
+ var file = event.target.files[0];
112
+
113
+ fileName = file.name;
114
+
115
+ var reader = new FileReader();
116
+
117
+ var $crop_area_box = $('#crop_area_box');
118
+
119
+ // 画像ファイル以外の場合は何もしない
120
+
121
+ if(file.type.indexOf('image') < 0){
122
+
123
+ return false;
124
+
125
+ }
126
+
127
+ // ファイル読み込みが完了した際のイベント登録
128
+
129
+ reader.onload = (function(file) {
130
+
131
+ return function(event) {
132
+
133
+ //既存のプレビューを削除
134
+
135
+ $crop_area_box.empty();
136
+
137
+ // .prevewの領域の中にロードした画像を表示するimageタグを追加
138
+
139
+ $crop_area_box.append($('<img>').attr({
140
+
141
+ src: event.target.result,
142
+
143
+ id: "crop_image",
144
+
145
+ title: file.name
146
+
147
+ }));
148
+
149
+ // プレビュー処理に対して、クロップ出来る処理を初期化設定
150
+
151
+ initCrop();
152
+
153
+ };
154
+
155
+ })(file);
156
+
157
+ reader.readAsDataURL(file);
158
+
159
+ });
160
+
161
+
162
+
163
+ var cropper;
164
+
165
+ function initCrop() {
166
+
167
+ cropper = new Cropper(crop_image, {
168
+
169
+ dragMode: 'move', // 画像を動かす設定
170
+
171
+ aspectRatio: 1 / 1, // 正方形やで!
172
+
173
+ restore: false,
174
+
175
+ guides: false,
176
+
177
+ center: false,
178
+
179
+ highlight: false,
180
+
181
+ cropBoxMovable: false,
182
+
183
+ cropBoxResizable: false,
184
+
185
+ toggleDragModeOnDblclick: false,
186
+
187
+ minCropBoxWidth: 240,
188
+
189
+ minCropBoxHeight: 240,
190
+
191
+ ready: function () {
192
+
193
+ croppable = true;
194
+
195
+ }
196
+
197
+ });
198
+
199
+ // 初回表示時
200
+
201
+ crop_image.addEventListener('ready', function(e){
202
+
203
+ cropping(e);
204
+
205
+ });
206
+
207
+ // 画像をドラッグした際の処理
208
+
209
+ crop_image.addEventListener('cropend', function(e){
210
+
211
+ cropping(e);
212
+
213
+ });
214
+
215
+ // 画像を拡大・縮小した際の処理
216
+
217
+ crop_image.addEventListener('zoom', function(e){
218
+
219
+ cropping(e);
220
+
221
+ });
222
+
223
+ }
224
+
225
+
226
+
227
+ // クロップ処理した画像をプレビュー領域に表示
228
+
229
+ var croppedCanvas;
230
+
231
+ function cropping(e) {
232
+
233
+ croppedCanvas = cropper.getCroppedCanvas({
234
+
235
+ width: 240,
236
+
237
+ height: 240,
238
+
239
+ });
240
+
241
+ // `$('<img>'{src: croppedCanvas.toDataURL()});` 的に書きたかったけど、jQuery力が足りず・・・
242
+
243
+ var croppedImage = document.createElement('img');
244
+
245
+ croppedImage.src = croppedCanvas.toDataURL();
246
+
247
+ crop_preview.innerHTML = '';
248
+
249
+ crop_preview.appendChild(croppedImage);
250
+
251
+ }
252
+
253
+
254
+
255
+ // Submit時に実行するPOST処理
256
+
257
+ $('#submitBtn').on('click', function(event){
258
+
259
+ // クロップ後のファイルをblobに変換し、AjaxでForm送信
260
+
261
+ croppedCanvas.toBlob(function (blob) {
262
+
263
+ const fileOfBlob = new File([blob], fileName);
264
+
265
+ var formData = new FormData();
266
+
267
+ // `employee[avatar]` は `employee` modelに定義した `mount_uploader :avatar, AvatarUploader` のコト
268
+
269
+ formData.append('user[picture]', fileOfBlob);
270
+
271
+ // EmployeeのID取得
272
+
273
+ const user_id = $('#user_id').val();
274
+
275
+ $.ajax('/picture/' + user_id + '/update', {
276
+
277
+ method: "PATCH", // POSTの方が良いのかな?
278
+
279
+ data: formData,
280
+
281
+ processData: false, // 余計な事はせず、そのままSUBMITする設定?
282
+
283
+ contentType: false,
284
+
285
+ success: function (res) {
286
+
287
+ // DOM操作にしたほうがいいのかな?その場合、アップロード後に実行するなどのポーリング処理的なサムシングが必要になりそう・・・
288
+
289
+ // なので、とりあえず簡単に`location.reload`しちゃう
290
+
291
+ location.reload();
292
+
293
+ },
294
+
295
+ error: function (res) {
296
+
297
+ console.error('Upload error');
298
+
299
+ }
300
+
301
+ });
302
+
303
+ // S3にアップロードするため画質を50%落とす
304
+
305
+ }, 'image/jpeg', 0.5);
306
+
307
+ });
308
+
309
+
310
+
311
+ });
312
+
313
+
314
+
315
+ ```
316
+
317
+
318
+
91
319
  ###試したこと
92
320
 
93
321
  https://github.com/cristianbica/cropper-rails