-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathlc-image-cropper.php
More file actions
418 lines (320 loc) · 13.1 KB
/
lc-image-cropper.php
File metadata and controls
418 lines (320 loc) · 13.1 KB
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
<?php
/**
* LC Image Cropper
*
* Handle cropping image
*
* PHP version 7.2.5
*
* @category Main_App
* @package UnlockED
* @author UnlockedLabs <developers@unlockedlabs.org>
* @license https://www.gnu.org/licenses/gpl.html GPLv3
* @link http://unlockedlabs.org
*/
namespace unlockedlabs\unlocked;
$pic = isset($_POST['imageDataURL']) ? $_POST['imageDataURL'] : die('ERROR: missing picture.');
?>
<div id="cropper" class="text-center">
<div class="image-cropper-container mb-1">
<img src="<?php echo $pic; ?>" alt="" class="cropper cropper-hidden" id="demo-cropper-image">
</div>
<div class="form-group demo-cropper-toolbar">
<div class="btn-group btn-group-justified">
<div class="btn-group">
<button type="button" class="btn bg-blue btn-icon" data-method="setDragMode" data-option="move" title="Move">
<span class="icon-move"></span>
</button>
</div>
<div class="btn-group">
<button type="button" class="btn bg-blue btn-icon" data-method="setDragMode" data-option="crop" title="Crop">
<span class="icon-crop2"></span>
</button>
</div>
<div class="btn-group">
<button type="button" class="btn bg-blue btn-icon" data-method="move" data-option="-10" data-second-option="0" title="Move Left">
<span class="icon-arrow-left13"></span>
</button>
</div>
<div class="btn-group">
<button type="button" class="btn bg-blue btn-icon" data-method="move" data-option="10" data-second-option="0" title="Move Right">
<span class="icon-arrow-right14"></span>
</button>
</div>
<div class="btn-group">
<button type="button" class="btn bg-blue btn-icon" data-method="move" data-option="0" data-second-option="-10" title="Move Up">
<span class="icon-arrow-up13"></span>
</button>
</div>
<div class="btn-group">
<button type="button" class="btn bg-blue btn-icon" data-method="move" data-option="0" data-second-option="10" title="Move Down">
<span class="icon-arrow-down132"></span>
</button>
</div>
</div>
</div>
<div class="form-group demo-cropper-toolbar">
<div class="btn-group btn-group-justified">
<div class="btn-group">
<button type="button" class="btn bg-blue btn-icon" data-method="zoom" data-option="0.1" title="Zoom In">
<span class="icon-zoomin3"></span>
</button>
</div>
<div class="btn-group">
<button type="button" class="btn bg-blue btn-icon" data-method="zoom" data-option="-0.1" title="Zoom Out">
<span class="icon-zoomout3"></span>
</button>
</div>
<div class="btn-group">
<button type="button" class="btn bg-blue btn-icon" data-method="rotate" data-option="-45" title="Rotate Left">
<span class="icon-rotate-ccw3"></span>
</button>
</div>
<div class="btn-group">
<button type="button" class="btn bg-blue btn-icon" data-method="rotate" data-option="45" title="Rotate Right">
<span class="icon-rotate-cw3"></span>
</button>
</div>
<div class="btn-group">
<button type="button" class="btn bg-blue btn-icon" data-method="scaleX" data-option="-1" title="Flip Horizontal">
<span class="icon-flip-vertical4"></span>
</button>
</div>
<div class="btn-group">
<button type="button" class="btn bg-blue btn-icon" data-method="scaleY" data-option="-1" title="Flip Vertical">
<span class="icon-flip-vertical3"></span>
</button>
</div>
</div>
<div>
<button class="btn btn-success mt-2" id="set_img" data-method="getCroppedCanvas">Set Image</button>
<button class="btn btn-warning mt-2" id="change_img">Change Image</button>
</div>
</div>
</div>
<script>
// --------------------------------------------------------------------------------
// IMAGE CROPPER - COPIED FROM LIMITLESS JS file....MAY NOT NEED EVERYTHING IN HERE
var ImageCropper = function() {
//
// Setup module components
//
// Image cropper
var _componentImageCropper = function() {
if (!$().cropper) {
console.warn('Warning - cropper.min.js is not loaded.');
return;
}
// Default initialization
$('.crop-basic').cropper();
// Define variables
var $cropper = $('.cropper'),
$image = $('#demo-cropper-image'),
$download = $('#download'),
$dataX = $('#dataX'),
$dataY = $('#dataY'),
$dataHeight = $('#dataHeight'),
$dataWidth = $('#dataWidth'),
$dataScaleX = $('#dataScaleX'),
$dataScaleY = $('#dataScaleY'),
options = {
aspectRatio: 1,
strict: true,
preview: '.preview',
crop: function (e) {
$dataX.val(Math.round(e.detail.x));
$dataY.val(Math.round(e.detail.y));
$dataHeight.val(Math.round(e.detail.height));
$dataWidth.val(Math.round(e.detail.width));
$dataScaleX.val(e.detail.scaleX);
$dataScaleY.val(e.detail.scaleY);
}
};
// Initialize cropper with options
$cropper.cropper(options);
//
// Toolbar
//
$('.demo-cropper-toolbar').on('click', '[data-method]', function () {
var $this = $(this),
data = $this.data(),
$target,
result;
if ($image.data('cropper') && data.method) {
data = $.extend({}, data);
if (typeof data.target !== 'undefined') {
$target = $(data.target);
if (typeof data.option === 'undefined') {
data.option = JSON.parse($target.val());
}
}
result = $image.cropper(data.method, data.option, data.secondOption);
switch (data.method) {
case 'scaleX':
case 'scaleY':
$(this).data('option', -data.option);
break;
case 'getCroppedCanvas':
if (result) {
//NOTE: we are saving the course as a png to take advantage of its transparent background
var img = result.toDataURL("image/png"); // CONVERTS IMG TO DATA STRING
$('#course_img_url').val(img); // SETS img AS VALUE OF HIDDEN INPUT
setImageAlert(); // FUNCTION TO LET USER KNOW IMAGE IS SET
// Init modal
//$('#getCroppedCanvasModal').modal().find('.modal-body').html(result);
// Download image
//$download.attr('href', result.toDataURL('image/jpeg'));
}
break;
}
}
});
//
// Aspect ratio
//
$('.demo-cropper-ratio').on('change', 'input[type=radio]', function () {
options[$(this).attr('name')] = $(this).val();
$image.cropper('destroy').cropper(options);
});
//
// Switching modes
//
/*
// Crop and clear
var cropClear = document.querySelector('.clear-crop-switch');
var cropClearInit = new Switchery(cropClear);
cropClear.onchange = function() {
if (cropClear.checked) {
// Crop mode
$cropper.cropper('crop');
// Enable other options
enableDisableInit.enable();
destroyCreateInit.enable();
}
else {
// Clear move
$cropper.cropper('clear');
// Disable other options
enableDisableInit.disable();
destroyCreateInit.disable();
}
};
// Enable and disable
var enableDisable = document.querySelector('.enable-disable-switch');
var enableDisableInit = new Switchery(enableDisable);
enableDisable.onchange = function() {
if (enableDisable.checked) {
// Enable cropper
$cropper.cropper('enable');
// Enable other options
cropClearInit.enable();
destroyCreateInit.enable();
}
else {
// Disable cropper
$cropper.cropper('disable');
// Disable other options
cropClearInit.disable();
destroyCreateInit.disable();
}
};
// Destroy and create
var destroyCreate = document.querySelector('.destroy-create-switch');
var destroyCreateInit = new Switchery(destroyCreate);
destroyCreate.onchange = function() {
if (destroyCreate.checked) {
// Initialize again
$cropper.cropper({
aspectRatio: 1,
preview: '.preview',
data: {
x: 208,
y: 22
}
});
// Enable other options
cropClearInit.enable();
enableDisableInit.enable();
}
else {
// Destroy cropper
$cropper.cropper('destroy');
// Disable other options
cropClearInit.disable();
enableDisableInit.disable();
}
};
*/
//
// Methods
//
// Get data
$('#getData').on('click', function() {
$('#showData1').val(JSON.stringify($cropper.cropper('getData')));
});
// Set data
$('#setData').on('click', function() {
$cropper.cropper('setData', {
x: 291,
y: 86,
width: 158,
height: 158
});
$('#showData1').val('Image data has been changed');
});
// Get container data
$('#getContainerData').on('click', function() {
$('#showData2').val(JSON.stringify($cropper.cropper('getContainerData')));
});
// Get image data
$('#getImageData').on('click', function() {
$('#showData2').val(JSON.stringify($cropper.cropper('getImageData')));
});
// Get canvas data
$('#getCanvasData').on('click', function() {
$('#showData3').val(JSON.stringify($cropper.cropper('getCanvasData')));
});
// Set canvas data
$('#setCanvasData').on('click', function() {
$cropper.cropper('setCanvasData', {
left: -50,
top: 0,
width: 750,
height: 750
});
$('#showData3').val('Canvas data has been changed');
});
// Get crop box data
$('#getCropBoxData').on('click', function() {
$('#showData4').val(JSON.stringify($cropper.cropper('getCropBoxData')));
});
// Set crop box data
$('#setCropBoxData').on('click', function() {
$cropper.cropper('setCropBoxData', {
left: 395,
top: 68,
width: 183,
height: 183
});
$('#showData4').val('Crop box data has been changed');
});
};
//
// Return objects assigned to module
//
return {
init: function() {
_componentImageCropper();
}
}
}();
$('#change_img').on('click', function() {
$('#courseImage').val(''); //RESET COURSE IMAGE VALUE TO NONE
$('#course_img_url').val(''); //RESET URL IMAGE VALUE TO NONE
$('#lc-crop-image').html(""); // REMOVES CROPPER DIV
$('.fileinput-remove-button').click(); // RESETS UPLOADER
$('#img_preview img').attr('src', ''); //reset preview image
$('#course-img-upload').show();
})
ImageCropper.init(); // INITIALIZE CROPPER
</script>