forked from BlackElias/needit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
new_post.js
55 lines (49 loc) · 1.93 KB
/
new_post.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
function getImage(input) {
if (input.files && input.files[0]) {
var reader = new FileReader();
reader.onload = function (e) {
let upload = e.target.result;
let extension = upload.slice(11, 14);
let image = document.createElement("IMG");
document.querySelector(".previewImage").appendChild(image);
document.querySelector(".previewImage img").setAttribute("src", upload);
if (extension === "png") {
document.querySelectorAll(".filter img").forEach((filter) => {
const formData = new FormData();
let type = filter.dataset.type;
formData.append("image", upload);
formData.append("type", `${type}`);
fetch("ajax/getpreviewimage.php", {
method: "POST",
body: formData,
})
.then((response) => response.blob())
.then((blob) => {
filter.src = URL.createObjectURL(blob);
})
.catch((error) => {
console.error("Error:", error);
});
});
document.querySelector(".postFilters").classList.remove("hidden");
document.querySelector(".postFilters").classList.add("flex");
} else {
document.querySelector(".postFilters").classList.add("hidden");
document.querySelector(".postFilters").classList.remove("flex");
}
};
reader.readAsDataURL(input.files[0]);
}
}
document.querySelectorAll(".filter img").forEach((filter) => {
filter.addEventListener("click", (e) => {
let src = e.target.src;
document.querySelector(".previewImage img").setAttribute("src", src);
document
.querySelector(".selectedFilter")
.setAttribute("value", filter.dataset.type);
});
});
function getFileExtension(filename) {
return /[.]/.exec(filename) ? /[^.]+$/.exec(filename)[0] : undefined;
}