-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathimage.js
116 lines (94 loc) · 3.08 KB
/
image.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
let imageName = 'image';
function readFile(input) {
if (input.files && input.files[0] && input.files[0].name) {
imageName = input.files[0].name;
let imageLower = imageName.toLowerCase();
if (imageLower.endsWith('.png') ||
imageLower.endsWith('.jpg') ||
imageLower.endsWith('.jpeg')) {
let reader = new FileReader();
reader.onload = function (e) {
let isPng = false;
if (imageLower.endsWith('.png')) {
isPng = true;
let button = $('.downloadJpg');
button.removeClass('hidden');
} else {
let button = $('.downloadPng');
button.removeClass('hidden');
}
const img = new Image();
img.onload = function () {
let canvas = document.getElementById("convertCanvas");
let ctx = canvas.getContext("2d");
canvas.width = this.width;
canvas.height = this.height;
if (isPng) {
ctx.fillStyle = "white";
ctx.fillRect(0, 0, canvas.width, canvas.height);
}
ctx.drawImage(img, 0, 0);
}
img.src = e.target.result
let htmlPreview =
'<img class="image" src="' + e.target.result + '" />' +
'<p>' + input.files[0].name + '</p>';
let wrapperZone = $(input).parent();
let previewZone = $('.preview-zone');
let boxZone = $('.preview-zone').find('.box').find('.box-body');
wrapperZone.removeClass('dragover');
previewZone.removeClass('hidden');
boxZone.empty();
boxZone.append(htmlPreview);
// drawImage();
};
reader.readAsDataURL(input.files[0]);
}
}
}
function triggerDownload(imgURI, name) {
let a = document.createElement('a');
a.setAttribute('download', name);
a.setAttribute('href', imgURI);
a.setAttribute('target', '_blank');
// a.dispatchEvent(evt);
a.click();
a.remove();
}
$(".dropzone").change(function () {
readFile(this);
});
$('.dropzone-wrapper').on('dragover', function (e) {
e.preventDefault();
e.stopPropagation();
$(this).addClass('dragover');
});
$('.dropzone-wrapper').on('dragleave', function (e) {
e.preventDefault();
e.stopPropagation();
$(this).removeClass('dragover');
});
$('.remove-preview').on('click', function () {
let boxZone = $(this).parents('.preview-zone').find('.box-body');
let previewZone = $(this).parents('.preview-zone');
let dropzone = $(this).parents('.form-group').find('.dropzone');
boxZone.empty();
previewZone.addClass('hidden');
let jpgButton = $('.downloadJpg');
jpgButton.addClass('hidden');
let pngButton = $('.downloadPng');
pngButton.addClass('hidden');
$('#imageForm').trigger('reset');
});
$('.downloadPng').on('click', function () {
let canvas = document.getElementById('convertCanvas');
let imgURI = canvas
.toDataURL('image/png');
triggerDownload(imgURI, imageName) + '.png';
});
$('.downloadJpg').on('click', function () {
let canvas = document.getElementById('convertCanvas');
let imgURI = canvas
.toDataURL('image/jpeg', 0.8);
triggerDownload(imgURI, imageName + '.jpg');
});