-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathscript.js
107 lines (100 loc) · 3.9 KB
/
script.js
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
const PROXY = 'http://cors.pmmlabs.ru/';
var endpoints = {
'list': 'http://vinci.camera/list',
'preload': 'http://vinci.camera/preload',
'process': 'http://vinci.camera/process'
}
, big = false
, cropImg
, filters
, hidden_filters
, row;
function add_images(filter_set, image_id, sup) {
for (var i in filter_set) {
var src = PROXY + endpoints['process'] + '/' + image_id + '/' + filter_set[i].id;
row.append($('<div class="col-xs-6 col-md-3">\
<a href="' + src + '" class="thumbnail" data-lightbox="image" data-title="' + filter_set[i].name + '">\
<img onerror="if (!~this.src.indexOf(\'?????\')) this.src+=\'?\'" src="' + src + '">\
</a>\
<p class="text-center">' + filter_set[i].name + (sup ? '<sup>'+sup+'</sup>' : '') + '</p>\
</div>'));
}
}
function renderResults(result) {
row.empty();
$('input').val('');
add_images(filters, result.preload);
add_images(hidden_filters, result.preload, '👁');
if (!lightbox.containerBottomPadding)
lightbox.init();
}
function onError() {
alert('Ошибка загрузки! Попробуйте файл поменьше.');
}
if (!HTMLCanvasElement.prototype.toBlob) {
Object.defineProperty(HTMLCanvasElement.prototype, 'toBlob', {
value: function (callback, type, quality) {
var binStr = atob( this.toDataURL(type, quality || 0.6).split(',')[1] ),
len = binStr.length,
arr = new Uint8Array(len);
for (var i=0; i<len; i++ ) {
arr[i] = binStr.charCodeAt(i);
}
callback( new Blob( [arr], {type: type || 'image/jpeg'} ) );
}
});
}
$(function () {
row = $('#thumbs');
$.getJSON(PROXY + endpoints['list'], function (_filters) {
filters = _filters;
});
$.getJSON('hidden_filters.json', function(_hidden_filters) {
hidden_filters = _hidden_filters;
});
$('#fileUpload').attr('action', PROXY + endpoints['preload']).find('input').change(function () {
var reader = new FileReader();
reader.onload = function (e) {
cropImg = document.createElement('img');
cropImg.onload = function () {
big = (this.width > 1080 || this.height > 1440) && (this.height > 1080 || this.width > 1440);
if (this.width < 200 || this.height < 200) {
alert('Изображение должно быть больше чем 200x200')
} else if (big) {
$('#crop').html(this);
$(this).cropper({
viewMode: 2,
autoCropArea: 1,
minCropBoxWidth: 200,
minCropBoxHeight: 200
});
$(cropImg).cropper('scale', (this.width ? $('#crop').width() / this.width : $('#crop').height() / this.height)).cropper('zoomTo', 1);
}
};
cropImg.src = e.target.result;
};
reader.readAsDataURL(this.files[0])
});
$('#submit').click(function () {
if (!big)
$('#fileUpload').ajaxSubmit({
success: renderResults,
error: onError
});
else {
$(cropImg).cropper('getCroppedCanvas').toBlob(function (blob) {
var formData = new FormData();
formData.append('photo.jpg', blob, 'photo.jpg');
$('#crop').empty();
$.ajax(PROXY + endpoints['preload'], {
method: "POST",
data: formData,
processData: false,
contentType: false,
success: renderResults,
error: onError
});
}, 'image/jpeg');
}
});
});