From ef2fc7c42dac1d06df6f2ee488cdb7e237d42a67 Mon Sep 17 00:00:00 2001 From: apple502j <33279053+apple502j@users.noreply.github.com> Date: Wed, 8 Jul 2020 23:44:16 +0900 Subject: [PATCH] Fix backpacking zero-height/width costumes --- src/lib/backpack/jpeg-thumbnail.js | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/src/lib/backpack/jpeg-thumbnail.js b/src/lib/backpack/jpeg-thumbnail.js index e16c33544..bf6a4797c 100644 --- a/src/lib/backpack/jpeg-thumbnail.js +++ b/src/lib/backpack/jpeg-thumbnail.js @@ -6,17 +6,24 @@ const jpegThumbnail = dataUrl => new Promise((resolve, reject) => { const maxDimension = 96; // 3x the maximum displayed size of 32px - if (image.height > image.width) { - canvas.height = maxDimension; - canvas.width = (maxDimension / image.height) * image.width; + if (image.height < 1 || image.width < 1) { + canvas.width = canvas.height = maxDimension; + // drawImage can fail if image height/width is less than 1 + // Use blank image; the costume is too small to render anyway + ctx.fillStyle = 'white'; // Create white background, since jpeg doesn't have transparency + ctx.fillRect(0, 0, maxDimension, maxDimension); } else { - canvas.width = maxDimension; - canvas.height = (maxDimension / image.width) * image.height; + if (image.height > image.width) { + canvas.height = maxDimension; + canvas.width = (maxDimension / image.height) * image.width; + } else { + canvas.width = maxDimension; + canvas.height = (maxDimension / image.width) * image.height; + } + ctx.fillStyle = 'white'; + ctx.fillRect(0, 0, canvas.width, canvas.height); + ctx.drawImage(image, 0, 0, canvas.width, canvas.height); } - - ctx.fillStyle = 'white'; // Create white background, since jpeg doesn't have transparency - ctx.fillRect(0, 0, canvas.width, canvas.height); - ctx.drawImage(image, 0, 0, canvas.width, canvas.height); resolve(canvas.toDataURL('image/jpeg', 0.92 /* quality */)); // Default quality is 0.92 }; image.onerror = err => {