-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathImageUploader.js
105 lines (99 loc) · 3.49 KB
/
ImageUploader.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
import { imgutil } from "./imgutil.js";
import { getExtension } from "./getExtension.js";
import { EXIF } from "https://taisukef.github.io/exif-js/EXIF.js";
class ImageUploader extends HTMLElement {
constructor(uploadurl) {
super();
this.uploadurl = uploadurl || "/data/";
this.style.display = "inline-block";
const cr = (tag) => document.createElement(tag);
const c = cr("div");
this.appendChild(c);
c.style.display = "grid"; // inline-block";
c.style.placeItems = "center";
c.style.width = "30vw";
c.style.height = "30vw";
c.style.margin = ".2vw";
c.style.border = "1px solid gray";
c.style.padding = "5px";
const tf = document.createElement("input");
tf.type = "text";
this.appendChild(tf);
tf.value = null; // "https://url ...";
tf.style.position = "relative";
tf.style.top = "-2.5em";
tf.style.left = ".5em";
tf.style.width = "25vw";
tf.style.fontSize = "80%";
this.tf = tf;
this.c = c;
}
set value(val) {
this.tf.value = val;
}
get value() {
return this.tf.value;
}
// this.onload (callback)
async setFile(item, maxwidth, maxsize) {
//const img = await imgutil.loadResizedImage(file, maxwidth, maxsize);
const file = item.file;
const bin = await ImageUtil.readAsArrayBufferAsync(file);
const exif = EXIF.readFromBinaryFile(bin);
this.colorSpace = exif.ColorSpace == "sRGB" || imgsrgb.checked ? "srgb" : "display-p3";
const img0 = await ImageUtil.getImageFromArrayBuffer(bin);
console.log(img0, colorSpace);
const img = await ImageUtil.resizeImage(img0, "image/jpeg", maxwidth, this.colorSpace);
// maxsize?
img.orgwidth = img.width; // img.width が変わってしまうので保存 getArrayBufferFromImageで使う
img.orgheight = img.height;
this.c.appendChild(img);
const iw = 28;
if (img.width > img.height) {
img.style.width = iw + "vw";
img.style.height = (iw / img.width * img.height) + "vw";
} else {
img.style.height = iw + "vw";
img.style.width = (iw / img.height * img.width) + "vw";
}
img.style.display = "block";
this.upload(file, img).then((url) => {
if (this.onload) {
this.onload(url);
}
});
}
async setImage(url) {
const img = new Image();
img.src = url;
await imgutil.waitImageLoad(img);
img.orgwidth = img.width; // img.width が変わってしまうので保存 getArrayBufferFromImageで使う
img.orgheight = img.height;
this.c.appendChild(img);
const iw = 28;
if (img.width > img.height) {
img.style.width = iw + "vw";
img.style.height = (iw / img.width * img.height) + "vw";
} else {
img.style.height = iw + "vw";
img.style.width = (iw / img.height * img.width) + "vw";
}
img.style.display = "block";
this.value = url;
}
async upload(file, img) {
const isjpg = getExtension(file.name, ".jpg").toLowerCase() === ".jpg";
const mimeType = isjpg ? "image/jpeg" : "image/png"; // image/webp も?
const quality = .92;
const bin = await ImageUtil.getArrayBufferFromImage(img, mimeType, quality, this.colorSpace);
//const img2 = await imgutil.getImageFromArrayBuffer(bin);
//this.appendChild(img2);
const url = this.uploadurl + file.name;
const res = await fetch(url, { method: "POST", body: bin });
const json = await res.json();
this.tf.value = this.uploadurl + json.name;
return this.tf.value;
}
}
customElements.define("img-uploader", ImageUploader);
export { ImageUploader };