keep original filename and don't show image

This commit is contained in:
Luca Conte 2025-05-05 20:15:13 +02:00
parent f370e6c57d
commit 5431a20463
3 changed files with 15 additions and 11 deletions

View File

@ -14,6 +14,7 @@
</div>
<input type="file" id="upload"><br>
Or drag & drop
<div id="filename"></div>
<div id="downloadButtons">
<button id="downloadPng">png</button>
<button id="downloadWebp">webp</button>

View File

@ -6,6 +6,7 @@ window.addEventListener("DOMContentLoaded", () => {
if (file.type.startsWith("image/")) {
image = URL.createObjectURL(file);
renderImageOnCanvas(image);
document.getElementById("filename").innerText = file.name;
} else {
alert("Please upload a valid image file.");
return;
@ -14,19 +15,11 @@ window.addEventListener("DOMContentLoaded", () => {
});
document.getElementById("downloadPng").addEventListener("click", () => {
const canvas = document.getElementById("cv");
const link = document.createElement("a");
link.download = "canvas.png";
link.href = canvas.toDataURL();
link.click();
downloadPictureFromCanvas("image/png");
});
document.getElementById("downloadWebp").addEventListener("click", () => {
const canvas = document.getElementById("cv");
const link = document.createElement("a");
link.download = "canvas.webp";
link.href = canvas.toDataURL("image/webp");
link.click();
downloadPictureFromCanvas("image/webp");
});
document.addEventListener("drag", () => {
@ -56,6 +49,7 @@ window.addEventListener("DOMContentLoaded", () => {
if (file.type.startsWith("image/")) {
image = URL.createObjectURL(file);
renderImageOnCanvas(image);
document.getElementById("filename").innerText = file.name;
} else {
alert("Please upload a valid image file.");
return;
@ -64,6 +58,15 @@ window.addEventListener("DOMContentLoaded", () => {
});
});
function downloadPictureFromCanvas(fileType) {
const canvas = document.getElementById("cv");
const link = document.createElement("a");
const originalFilename = document.getElementById("filename").innerText;
link.download = originalFilename.replace(/\.[^/.]+$/, "");
link.href = canvas.toDataURL(fileType);
link.click();
}
function renderImageOnCanvas(image) {
const canvas = document.getElementById("cv");
const ctx = canvas.getContext("2d");

View File

@ -14,5 +14,5 @@
}
#cv {
display: block;
display: none;
}