Skip to content

Commit

Permalink
Add image compression
Browse files Browse the repository at this point in the history
  • Loading branch information
agatemosu committed Dec 29, 2024
1 parent 7645025 commit 2474e4d
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 7 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
},
"dependencies": {
"@simonwep/pickr": "1.9.1",
"browser-image-compression": "^2.0.2",
"sortablejs": "1.15.6"
},
"devDependencies": {
Expand Down
15 changes: 15 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 12 additions & 7 deletions src/main.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Sortable from "sortablejs";
import TierRow from "./tier-row.js";
import imageCompression from "browser-image-compression";

// #region Document events

Expand Down Expand Up @@ -75,13 +76,17 @@ function uploadImages(files) {
imageEl.classList.add("tier-element");
imagesBar.appendChild(imageEl);

const image = new Image();
image.onload = () => {
imageEl.style.aspectRatio = `${image.width} / ${image.height}`;
imageEl.style.backgroundImage = `url("${image.src}")`;
imageEl.style.minHeight = `${Math.min(image.height, 80)}px`;
};
image.src = URL.createObjectURL(file);
imageCompression(file, {
maxWidthOrHeight: 480,
}).then((compressedFile) => {
const image = new Image();
image.onload = () => {
imageEl.style.aspectRatio = `${image.width} / ${image.height}`;
imageEl.style.backgroundImage = `url("${image.src}")`;
imageEl.style.minHeight = `${Math.min(image.height, 80)}px`;
};
image.src = URL.createObjectURL(compressedFile);
});
}
}

Expand Down

0 comments on commit 2474e4d

Please sign in to comment.