forked from Axure/WASM-ImageMagick
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
282 lines (249 loc) · 9.06 KB
/
index.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
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
<!doctype html>
<html lang="en-us">
<head>
<meta charset="utf-8">
<title>Wasm ImageMagick</title>
<script DEFER src="https://cdn.jsdelivr.net/g/filesaver.js"></script>
<script DEFER src="https://cdnjs.cloudflare.com/ajax/libs/jszip/3.1.5/jszip.min.js"></script>
<style>
table,
th,
td {
border: 1px solid black;
border-collapse: collapse;
}
</style>
</head>
<body>
For license info of this project and related components see <a href="https://github.com/knicknic/WASM-ImageMagick">https://github.com/knicknic/WASM-ImageMagick</a>
<br><p id="descriptionTxt"></p>
<div id="advancedOptions" hidden>
<table>
<tr>
<th>Advanced Options</th>
<th>Values</th>
</tr>
<tr>
<td>
<label for="descriptionInput">Description</label>
</td>
<td>
<input type="text" id="descriptionInput" size=120 value="Site lets you execute magick commands in your browser and share commands via url.">
</td>
</tr>
<tr>
<td>
<label for="magickCommand">Magick command</label>
</td>
<td>
<input type="text" id="magickCommand" size=120 value="convert * -flatten -crop x20% +repage -quality 100 -scene 0 -set filename:mysize %t %[filename:mysize]-%d.jpeg">
</td>
</tr>
<tr>
<td>
<label for="autoZip">Generate Zip when done processing</label>
</td>
<td>
<input type="checkbox" name="autoZip" id="autoZip" value="1">
</td>
</tr>
<tr>
<td>
<label for="magickEachPicture">Run command for each picture</label>
</td>
<td>
<input type="checkbox" name="" id="magickEachPicture" value="1" />
</td>
</tr>
<tr>
<td>
<label for="showAdvanced">Url shows advanced options</label>
</td>
<td>
<input type="checkbox" name="" id="showAdvanced" value="1" />
</td>
</tr>
</table>
<p>Share command url: <a id="shareUrl"></a></p>
<button id="GenerateZip" disabled>Generate Zip of Images</button>
<br>
<br>
</div>
<input type=file name=file id="your-files" aria-label="File Input" accept=".jpg,.jpeg,.png,.bmp,.tif,.tiff,.psd" multiple>
<button id="advancedOptionsButton" >Advanced Options</button>
<div id="Processing" hidden>
<H2>Processing pictures, ignore unresponsive webpage and wait up to 60 seconds per file, then give up.</H2>
<H2 id="files-remaining" hidden>0</H2>
</div>
<div id="imageHolder" class="image-holder" style="display:flex;flex-direction:column;align-items: flex-start"></div>
<script type='module'>
//import the library to talk to imagemagick
import * as Magick from './magickApi.js';
// various html elements
let advancedOptions = document.getElementById('advancedOptions');
let descriptionTxt = document.getElementById("descriptionTxt");
let descriptionInput = document.getElementById("descriptionInput");
let magickCommand = document.getElementById("magickCommand");
let autoZip = document.getElementById("autoZip");
let magickEachPicture = document.getElementById("magickEachPicture");
let showAdvanced = document.getElementById("showAdvanced");
let shareUrl = document.getElementById("shareUrl");
let generateZip = document.getElementById("GenerateZip")
let yourFiles = document.getElementById("your-files");
let advancedOptionsButton = document.getElementById('advancedOptionsButton');
let imageHolder = document.getElementById('imageHolder');
let filesRemaining = document.getElementById("files-remaining");
let processing = document.getElementById("Processing");
//service worker
if ('serviceWorker' in navigator) {
window.addEventListener('load', function () {
navigator.serviceWorker.register('sw.js').then(function (registration) {
let refreshing;
navigator.serviceWorker.addEventListener('controllerchange',
function () {
if (refreshing) return;
refreshing = true;
window.location.reload();
}
);
if (registration.active) {
registration.active.addEventListener('message', function (msg) {
alert(msg);
location.reload();
});
}
}, function (err) {
// registration failed :(
console.log('ServiceWorker registration failed: ', err);
});
});
}
// deal with input urls
{
let hash = window.location.hash;
if(hash.length >1)
{
try{
let options = JSON.parse(decodeURIComponent(hash.substring(2)));
descriptionInput.value = options['desc'];
magickCommand.value = options['cmd'];
autoZip.checked = options['autoZip'];
magickEachPicture.checked = options['magickEachPicture'];
advancedOptions.hidden = !options['showAdvanced'];
showAdvanced.checked = options['showAdvanced'];
}
catch(e){
advancedOptions.hidden = false;
}
}
else{
advancedOptions.hidden = false;
}
advancedOptionsButton.onclick = function(){advancedOptions.hidden = !advancedOptions.hidden;};
let calculateShareUrl = function(){
let shareUrlObj = {}
shareUrlObj['desc'] = descriptionInput.value;
shareUrlObj['cmd'] = magickCommand.value;
shareUrlObj['autoZip'] = autoZip.checked;
shareUrlObj['magickEachPicture'] = magickEachPicture.checked;
shareUrlObj['showAdvanced'] = showAdvanced.checked;
shareUrl.href=window.location.href.split('#')[0] + "#!" + encodeURIComponent(JSON.stringify(shareUrlObj));
shareUrl.innerText = shareUrl.href
}
magickCommand.oninput = calculateShareUrl;
descriptionInput.oninput = function(){
descriptionTxt.innerText = descriptionInput.value;
calculateShareUrl();
}
descriptionInput.oninput();
autoZip.oninput = calculateShareUrl;
magickEachPicture.oninput = calculateShareUrl;
showAdvanced.oninput = calculateShareUrl;
calculateShareUrl();
}
//generate zips
function GenerateZip() {
let zip = new JSZip();
if (imageHolder.hasChildNodes()) {
let children = imageHolder.childNodes;
for (let i = 0; i < children.length; i++) {
let child = children[i].wasmResponse;
zip.file(child['name'], child['blob'])
}
zip.generateAsync({ type: "blob" })
.then(function (content) {
// see FileSaver.js
saveAs(content, "out.zip");
});
}
};
generateZip.onclick = GenerateZip;
// helper to read user inputed files
function ReadFile(file){
let readFile = Magick.CreatePromiseEvent();
// read fileName & content
let fr = new FileReader();
fr.onload = function (txt) {
let fileName = file.name
let encoded_txt = txt.target.result;
let content = new Uint8Array(encoded_txt);
let sourceFilename = fileName
readFile['resolve']([sourceFilename, content]);
}
fr.readAsArrayBuffer(file);
return readFile;
};
let SetProcessing = function (isProcessing){
//filesRemaining.hidden = !isProcessing;
processing.hidden = !isProcessing;
}
// handle new images to process
let processFiles = async function () {
// When the yourFiles has changed, there are new files
let files = yourFiles.files;
if (files.length > 0) {
//clean up old images and show processing screen
SetProcessing(true);
imageHolder.innerHTML = '';
// for each file requested split them (files isn't an array)
let toProcess=[];
for (let fileIndex = 0; fileIndex < files.length; ++fileIndex) {
filesRemaining.innerText = "" + (files.length - fileIndex);
let [fileName,content] = await ReadFile(files[fileIndex]);
toProcess.push({'name': fileName, 'content': content})
}
let cmd = magickCommand.value;
let processedFiles = [];
if(magickEachPicture.checked)
{
for (let singleSourceImage of toProcess){
let newProcessedFiles = await Magick.Call([singleSourceImage], cmd.split(" "));
processedFiles.push(...newProcessedFiles)
}
}
else
{
processedFiles = await Magick.Call(toProcess, cmd.split(" "));
}
for (let file of processedFiles){
let img = document.createElement('img');
img.src = URL.createObjectURL(file['blob']);
img.name = file['name']
img.wasmResponse = file
imageHolder.appendChild(img);
}
SetProcessing(false);
if(autoZip.checked)
{
GenerateZip();
}
}
generateZip.disabled=!imageHolder.hasChildNodes();
};
yourFiles.addEventListener("change", function (event) {
event.preventDefault();
processFiles();
}, false);
</script>
</body>
</html>