Skip to content

armgit / dub

Public Imported from GitHub
0

The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.

fix: add logo support to QR code API using <img> tag as a workaround

Bharath Lakshman Kumar9afc7a45f84a
diff --git a/apps/web/app/api/qr/route.tsx b/apps/web/app/api/qr/route.tsx
index 9c65c09220..a4fc751b43 100644
--- a/apps/web/app/api/qr/route.tsx
+++ b/apps/web/app/api/qr/route.tsx
@@ -1,9 +1,9 @@
import { handleAndReturnErrorResponse } from "@/lib/api/errors";
import { ratelimitOrThrow } from "@/lib/api/utils";
-import { getQRAsSVG } from "@/lib/qr/api";
+import { QRCodeSVG } from "@/lib/qr/utils";
import { getQRCodeQuerySchema } from "@/lib/zod/schemas/qr";
import { getSearchParams } from "@dub/utils";
-import { ImageResponse } from "@vercel/og";
+import { ImageResponse } from "next/og";
import { NextRequest } from "next/server";
export const runtime = "edge";
@@ -19,25 +19,26 @@ export async function GET(req: NextRequest) {
const logo =
req.nextUrl.searchParams.get("logo") || "https://assets.dub.co/logo.png";
- const svg = await getQRAsSVG({
- value: url,
- size,
- level,
- includeMargin,
- fgColor,
- bgColor,
- imageSettings: {
- src: logo,
- height: size / 4,
- width: size / 4,
- excavate: true,
+ return new ImageResponse(
+ QRCodeSVG({
+ value: url,
+ size,
+ level,
+ includeMargin,
+ fgColor,
+ bgColor,
+ imageSettings: {
+ src: logo,
+ height: size / 4,
+ width: size / 4,
+ excavate: true,
+ },
+ }),
+ {
+ width: size,
+ height: size,
},
- });
-
- return new ImageResponse(svg, {
- width: size,
- height: size,
- });
+ );
} catch (error) {
return handleAndReturnErrorResponse(error);
}
diff --git a/apps/web/lib/qr/utils.tsx b/apps/web/lib/qr/utils.tsx
index 54a43f75df..8ec8756393 100644
--- a/apps/web/lib/qr/utils.tsx
+++ b/apps/web/lib/qr/utils.tsx
@@ -116,6 +116,31 @@ export function getImageSettings(
return { x, y, h, w, excavation };
}
+export function convertImageSettingsToPixels(
+ calculatedImageSettings: {
+ x: number;
+ y: number;
+ w: number;
+ h: number;
+ excavation: Excavation | null;
+ },
+ size: number,
+ numCells: number,
+): {
+ imgWidth: number;
+ imgHeight: number;
+ imgLeft: number;
+ imgTop: number;
+} {
+ const pixelRatio = size / numCells;
+ const imgWidth = calculatedImageSettings.w * pixelRatio;
+ const imgHeight = calculatedImageSettings.h * pixelRatio;
+ const imgLeft = calculatedImageSettings.x * pixelRatio;
+ const imgTop = calculatedImageSettings.y * pixelRatio;
+
+ return { imgWidth, imgHeight, imgLeft, imgTop };
+}
+
export function QRCodeSVG(props: QRPropsSVG) {
const {
value,
@@ -128,9 +153,16 @@ export function QRCodeSVG(props: QRPropsSVG) {
...otherProps
} = props;
+ const shouldUseHigherErrorLevel =
+ imageSettings?.excavate && (level === "L" || level === "M");
+
+ // Use a higher error correction level 'Q' when excavation is enabled
+ // to ensure the QR code remains scannable despite the removed modules.
+ const effectiveLevel = shouldUseHigherErrorLevel ? "Q" : level;
+
let cells = qrcodegen.QrCode.encodeText(
value,
- ERROR_LEVEL_MAP[level],
+ ERROR_LEVEL_MAP[effectiveLevel],
).getModules();
const margin = includeMargin ? MARGIN_SIZE : 0;
@@ -148,14 +180,20 @@ export function QRCodeSVG(props: QRPropsSVG) {
cells = excavateModules(cells, calculatedImageSettings.excavation);
}
+ const { imgWidth, imgHeight, imgLeft, imgTop } =
+ convertImageSettingsToPixels(calculatedImageSettings, size, numCells);
+
image = (
- <image
- href={imageSettings.src}
- height={calculatedImageSettings.h}
- width={calculatedImageSettings.w}
- x={calculatedImageSettings.x + margin}
- y={calculatedImageSettings.y + margin}
- preserveAspectRatio="none"
+ <img
+ src={imageSettings.src}
+ alt="Logo"
+ style={{
+ position: "absolute",
+ left: `${imgLeft}px`,
+ top: `${imgTop}px`,
+ width: `${imgWidth}px`,
+ height: `${imgHeight}px`,
+ }}
/>
);
}