Merge pull request #1195 from berniexie/1093/large-png-exports

exports: Prevent exports over canvas max dimensions
This commit is contained in:
Bernard Xie 2023-04-13 15:11:51 -07:00 committed by GitHub
commit ab09193128
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1,4 +1,4 @@
async ({imgString, scale}) => {
async ({ imgString, scale }) => {
const tempImg = new Image();
const loadImage = () => {
return new Promise((resolve, reject) => {
@ -13,6 +13,31 @@ async ({imgString, scale}) => {
const canvas = document.createElement("canvas");
canvas.width = img.width * scale;
canvas.height = img.height * scale;
// https://developer.mozilla.org/en-US/docs/Web/HTML/Element/canvas
const MAX_DIMENSION = 32767;
const MAX_AREA = 268435456;
const ratio = img.width / img.height;
if (ratio > 1) {
if (canvas.width > MAX_DIMENSION) {
canvas.width = MAX_DIMENSION;
canvas.height = MAX_DIMENSION / ratio;
}
} else {
if (canvas.height > MAX_DIMENSION) {
canvas.height = MAX_DIMENSION;
canvas.width = MAX_DIMENSION * ratio;
}
}
const currentArea = canvas.width * canvas.height;
if (currentArea > MAX_AREA) {
const areaRatio = MAX_AREA / currentArea;
canvas.height = Math.floor(canvas.height * areaRatio);
canvas.width = Math.floor(canvas.width * areaRatio);
}
const ctx = canvas.getContext("2d");
if (!ctx) {
return new Error("could not get canvas context");