Skip to content

Commit f1729a0

Browse files
committed
Fixed: svg image support and updated README.md
1 parent 29cf437 commit f1729a0

File tree

3 files changed

+27
-13
lines changed

3 files changed

+27
-13
lines changed

assets/upload-download.png

-1.46 KB
Loading

src/app/api/modify/route.ts

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,16 @@ async function modifyImage(
5454
imageFile: File,
5555
imageInfo: ImageInfo,
5656
): Promise<ModifiedImage> {
57-
const fileBuffer = await imageFile.arrayBuffer();
58-
59-
const raw = imageInfo.raw;
57+
let fileBuffer: ArrayBuffer | string = await imageFile.arrayBuffer();
6058
const outputTitle = imageInfo.title;
59+
const image = sharp(Buffer.from(fileBuffer), { animated: false });
60+
const metadata = await image.metadata();
61+
const raw = imageInfo.raw;
62+
63+
if (raw && metadata.format === "svg") {
64+
fileBuffer = await imageFile.text();
65+
}
66+
6167
let outputWidth;
6268
let outputHeight;
6369
let outputFormat;
@@ -67,13 +73,10 @@ async function modifyImage(
6773

6874
if (raw) {
6975
imageBuffer = fileBuffer;
70-
outputHeight = imageInfo.height!;
71-
outputWidth = imageInfo.width!;
72-
outputFormat = imageInfo.format;
76+
outputHeight = metadata.height!;
77+
outputWidth = metadata.width!;
78+
outputFormat = metadata.format!;
7379
} else {
74-
const image = sharp(Buffer.from(fileBuffer), { animated: false });
75-
const metadata = await image.metadata();
76-
7780
if (!imageInfo.height && !imageInfo.width) {
7881
outputHeight = metadata.height;
7982
outputWidth = metadata.width;
@@ -111,12 +114,21 @@ async function modifyImage(
111114
outputFormat = modifiedInfo.format;
112115
}
113116

114-
const buffer = Buffer.from(imageBuffer).toString("base64");
117+
let buffer, sizeKB, base64Format;
118+
if (typeof imageBuffer === "string") {
119+
sizeKB = imageBuffer.length / 1024;
120+
buffer = btoa(imageBuffer);
121+
base64Format = "data:image/svg+xml;base64,";
122+
} else {
123+
sizeKB = imageBuffer.byteLength / 1024;
124+
buffer = Buffer.from(imageBuffer).toString("base64");
125+
base64Format = `data:image/${outputFormat};base64,`;
126+
}
115127

116128
return {
117129
title: outputTitle,
118-
previewUrl: `data:image/${outputFormat};base64,${buffer}`,
119-
sizeKb: imageBuffer.byteLength / 1024,
130+
previewUrl: `${base64Format}${buffer}`,
131+
sizeKb: sizeKB,
120132
format: outputFormat,
121133
height: outputHeight,
122134
width: outputWidth,

src/components/image-upload-form.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@ interface ImageUploadFormProps {
2828
const ImageUploadForm = ({ image, onFormChange }: ImageUploadFormProps) => {
2929
const { control, watch } = useForm<UploadFormData>({
3030
defaultValues: {
31-
title: (image.title || "").replace(/\.[^/.]+$/, `.${image.format}`),
31+
title: image.raw
32+
? image.title || ""
33+
: (image.title || "").replace(/\.[^/.]+$/, `.${image.format}`),
3234
bucket: image.raw ? "storage" : "server",
3335
folder: image.folder || "",
3436
},

0 commit comments

Comments
 (0)