-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
72 lines (70 loc) · 2.11 KB
/
index.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
var images = [];
function processImages() {
var files = $("#image-upload").prop("files");
$.each(files, function (index, file) {
var reader = new FileReader();
reader.onload = function (event) {
var image = new Image();
image.onload = function () {
var canvas = $("<canvas>")[0];
var maxWidth = 1080;
var maxHeight = 1080;
var width = image.width;
var height = image.height;
if (width > maxWidth) {
height = (height * maxWidth) / width;
width = maxWidth;
}
if (height > maxHeight) {
width = (width * maxHeight) / height;
height = maxHeight;
}
canvas.width = maxWidth;
canvas.height = maxHeight;
var context = canvas.getContext("2d");
context.fillStyle = "black";
context.fillRect(0, 0, maxWidth, maxHeight);
var x = (maxWidth - width) / 2;
var y = (maxHeight - height) / 2;
context.drawImage(image, x, y, width, height);
var dataURL = canvas.toDataURL(file.type);
var imageData = {
name: file.name,
dataURL: dataURL,
};
images.push(imageData);
};
image.src = event.target.result;
};
reader.readAsDataURL(file);
});
$("#download-all").css("display", "inline-block");
$("#download-all").on("click", function (event) {
event.preventDefault();
saveAllImages();
});
}
function saveAllImages() {
var zip = new JSZip();
$.each(images, function (index, imageData) {
var dataURL = imageData.dataURL;
var name = imageData.name;
var data = dataURL.split(",")[1];
zip.file(name, data, { base64: true });
});
zip.generateAsync({ type: "blob" }).then(function (content) {
var url = window.URL.createObjectURL(content);
var link = document.createElement("a");
link.download = "images.zip";
link.href = url;
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
window.URL.revokeObjectURL(url);
});
}
$(document).ready(function () {
$("#process-images").click(function () {
processImages();
});
});