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> </div>
<input type="file" id="upload"><br> <input type="file" id="upload"><br>
Or drag & drop Or drag & drop
<div id="filename"></div>
<div id="downloadButtons"> <div id="downloadButtons">
<button id="downloadPng">png</button> <button id="downloadPng">png</button>
<button id="downloadWebp">webp</button> <button id="downloadWebp">webp</button>

View File

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

View File

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