fix: Banner uploader fix error handling and reduce image size jump (#21903)

* improve uploader

* better error handling

* unnecessary check

* more adjustments

* remove logs
This commit is contained in:
Syed Ali Shahbaz authored and GitHub committed 2025-06-19 03:35:41 +00:00
1 parent 7075384313
commit a5fe10189f
5 files changed
+35 -5

No files matched your search

@@ -209,6 +209,12 @@ async function getCroppedImg(
const ctx = canvas.getContext("2d");
if (!ctx) throw new Error("Context is null, this should never happen.");
// Detect original image format from data URL
const originalFormat =
imageSrc.startsWith("data:image/jpeg") || imageSrc.startsWith("data:image/jpg")
? "image/jpeg"
: "image/png";
canvas.width = width;
canvas.height = height;
@@ -224,5 +230,6 @@ async function getCroppedImg(
canvas.height
);
return canvas.toDataURL("image/png");
// Use original format with quality setting for JPEG
return canvas.toDataURL(originalFormat, originalFormat === "image/jpeg" ? 0.6 : undefined);
}
@@ -192,6 +192,12 @@ async function getCroppedImg(imageSrc: string, pixelCrop: Area): Promise<string>
const ctx = canvas.getContext("2d");
if (!ctx) throw new Error("Context is null, this should never happen.");
// Detect original image format from data URL
const originalFormat =
imageSrc.startsWith("data:image/jpeg") || imageSrc.startsWith("data:image/jpg")
? "image/jpeg"
: "image/png";
const maxSize = Math.max(image.naturalWidth, image.naturalHeight);
const resizeRatio = MAX_IMAGE_SIZE / maxSize < 1 ? Math.max(MAX_IMAGE_SIZE / maxSize, 0.75) : 1;
@@ -216,7 +222,7 @@ async function getCroppedImg(imageSrc: string, pixelCrop: Area): Promise<string>
// on very low ratios, the quality of the resize becomes awful. For this reason the resizeRatio is limited to 0.75
if (resizeRatio <= 0.75) {
// With a smaller image, thus improved ratio. Keep doing this until the resizeRatio > 0.75.
return getCroppedImg(canvas.toDataURL("image/png"), {
return getCroppedImg(canvas.toDataURL(originalFormat), {
width: canvas.width,
height: canvas.height,
x: 0,
@@ -224,5 +230,6 @@ async function getCroppedImg(imageSrc: string, pixelCrop: Area): Promise<string>
});
}
return canvas.toDataURL("image/png");
// Use original format with quality setting for JPEG
return canvas.toDataURL(originalFormat, originalFormat === "image/jpeg" ? 0.9 : undefined);
}