-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
51 lines (46 loc) · 1.19 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
/* ---------------------------------------------
</> with 💛 by Vishwa Gaurav
GitHub : https://github.com/VishwaGauravIn
Website : https://itsvg.in
------------------------------------------------ */
module.exports = imgConverter = (
Dataurl,
width = 500,
height = 500,
format = "png",
scale = 1
) =>
new Promise((resolve, reject) => {
let canvas;
let ctx;
let img;
img = new Image();
img.src = Dataurl;
img.onload = () => {
canvas = document.createElement("canvas");
canvas.width = img.width * scale;
canvas.height = img.height * scale;
ctx = canvas.getContext("2d");
ctx.drawImage(
img,
0,
0,
img.width,
img.height,
0,
0,
width * scale,
height * scale
);
img = new Image();
img.src = canvas.toDataURL(`image/${format}`);
img.onload = () => {
canvas = document.createElement("canvas");
canvas.width = width * scale;
canvas.height = height * scale;
ctx = canvas.getContext("2d");
ctx.drawImage(img, 0, 0);
resolve(canvas.toDataURL(`image/${format}`));
};
};
});