-
Notifications
You must be signed in to change notification settings - Fork 0
/
tim.js
157 lines (127 loc) · 3.85 KB
/
tim.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
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
$(function() {
var img1_set;
var img2_set;
var img3_set;
function spacing() {
if (img1_set == false) { img1_width = 0; }
else { img1_width = $('#img1').width(); }
if (img2_set == false) { img2_width = 0; }
else { img2_width = $('#img2').width(); }
$('#img2').css('margin-left', img1_width);
$('#img3').css('margin-left', img1_width+img2_width);
}
spacing();
function imageImport(val, name) {
if (val.length > 0) {
// resets
$('#final_image').hide();
$('[name='+name+']').siblings('label').text('');
if (val.indexOf(".jpg") >= 0 || val.indexOf(".png") >= 0 || val.indexOf(".gif") >= 0 ||
val.indexOf(".JPG") >= 0 || val.indexOf(".PNG") >= 0 || val.indexOf(".GIF") >= 0) {
$('#'+name).attr('src', val).show();
switch(name) {
case 'img1': img1_set = true; break;
case 'img2': img2_set = true; break;
case 'img3': img3_set = true; break;
default: break; }
spacing();
ready();
} else {
$('[name='+name+']').siblings('label').text('No image detected');
$('#'+name).hide();
$('[name='+name+']').val('');
spacing();
}
} else {
$('#'+name).hide();
switch(name) {
case 'img1': img1_set = false; break;
case 'img2': img2_set = false; break;
case 'img3': img3_set = false; break;
default: break; }
spacing();
}
};
$('.img_input').change(function() {
$(this).siblings('label').text('');
var val = $(this).val();
var name = $(this).attr('name');
imageImport(val, name);
});
function ready() {
if (img1_set && img2_set && img3_set) {
submit_form();
}
}
function show_final_image(result) {
$('#img1, #img2, #img3').fadeOut(200);
$('#final_image').show();
$('#final_url').fadeIn(100).attr('value', result);
}
$('#submit').click(function () {
submit_form();
});
function submit_form() {
$('#loader').fadeIn(200);
$('#submit').text('Working...').addClass('working');
var img1 = $('input[name=\'img1\']').val();
var img2 = $('input[name=\'img2\']').val();
var img3 = $('input[name=\'img3\']').val();
$.ajax({
url: 'image_merge.php',
type: 'GET',
data: 'img1=' + img1 + '&img2=' + img2 + '&img3=' + img3,
success: function(result) {
$('#loader').fadeOut(200);
$('#submit').text('Done').removeClass('working');
if (result.indexOf(".jpg") >= 0) {
$('#final_image').attr('src', result);
$('#img2').animate({
marginLeft: 30,
}, 600, 'swing');
$('#img3').animate({
marginLeft: 60,
}, 600, 'swing', function() {
show_final_image(result);
});
} else if (result.indexOf("Bad original image size") >= 0 && $('aside p').length == 0) {
$('aside').append('<p>Error: One or more of the Images could not be accessed.<br><a href=\"\">Reset form</a></p>')
} else if ($('aside p').length == 0) {
$('aside').append('<p>'+result+'<br><a href=\"\">Reset form</a></p>')
}
}
});
}
// DRAG AND DROP YO
function handleURL(evt) {
evt.stopPropagation();
evt.preventDefault();
// assume text
var data = evt.dataTransfer.getData('text');
if (data !== '') {
url = data.toString();
} else {
// parse html
data = evt.dataTransfer.getData('text/html');
var html = data.split('img src=\"');
var url = html[1].split('\"')[0];
}
if (img1_set && img2_set) {
$('[name=img3]').val(url);
imageImport(url, 'img3');
} else if (img1_set) {
$('[name=img2]').val(url);
imageImport(url, 'img2');
} else {
$('[name=img1]').val(url);
imageImport(url, 'img1');
}
}
function handleDragOver(evt) {
evt.stopPropagation();
evt.preventDefault();
evt.dataTransfer.dropEffect = 'copy';
}
document.addEventListener('dragover', handleDragOver, false);
document.addEventListener('drop', handleURL, false);
});