Skip to content

Commit

Permalink
feat: resize image before submit
Browse files Browse the repository at this point in the history
  • Loading branch information
drochetti authored and aykutkardas committed Mar 6, 2024
1 parent 6904d6b commit 0791990
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 8 deletions.
4 changes: 2 additions & 2 deletions app/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import { Analytics } from "@vercel/analytics/react";
const inter = Inter({ subsets: ["latin"] });

export const metadata: Metadata = {
title: "Upscaler Comparator - by fal.ai",
description: "Comparator for Upscalers",
title: "Upscaler Battleground - by fal.ai",
description: "A demo comparing different upscalers served by fal.ai",
authors: [{ name: "fal.ai", url: "https://fal.ai" }],
// [TODO]: Update this to the correct URL
metadataBase: new URL("https://fastsdxl.ai"),
Expand Down
12 changes: 8 additions & 4 deletions app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
} from "@/components/model-dropdown";
import { Button } from "@/components/ui/button";
import { LoaderIcon } from "lucide-react";
import { resizeImage } from "@/lib/image";

fal.config({ proxyUrl: "/api/proxy" });

Expand Down Expand Up @@ -53,9 +54,10 @@ export default function UpscalerBattleground() {

let inferenceTime;

const resizedImage = await resizeImage(file, 1024);
const result: Record<string, any> = await fal.subscribe(firstModel.model, {
input: {
image_url: file,
image_url: resizedImage,
},
logs: true,
onQueueUpdate: (update) => {
Expand Down Expand Up @@ -83,9 +85,10 @@ export default function UpscalerBattleground() {

let inferenceTime;

const resizedImage = await resizeImage(file, 1024);
const result: Record<string, any> = await fal.subscribe(secondModel.model, {
input: {
image_url: file,
image_url: resizedImage,
},
logs: true,
onQueueUpdate: (update) => {
Expand Down Expand Up @@ -176,9 +179,10 @@ export default function UpscalerBattleground() {
onChange={handleImageSelect}
className={cn(
"font-light mx-auto rounded-full h-10 pr-10 truncate",
!imageFile && "border-orange-400/40"
!imageFile && "border-indigo-700/70"
)}
placeholder="Type something..."
accept="image/*"
placeholder="Choose file..."
/>
</div>
</div>
Expand Down
54 changes: 54 additions & 0 deletions lib/image.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
export async function resizeImage(
file: File,
max: number = 1024,
): Promise<Blob> {
const canvas = document.createElement("canvas");
const ctx = canvas.getContext("2d");

if (!ctx) {
throw new Error("Failed to get 2d context");
}

const image = await loadImage(file);

const targetSize = Math.min(max, Math.max(image.width, image.height));
console.log("targetSize", targetSize);
const targetWidth =
image.width > image.height
? targetSize
: Math.round((image.width / image.height) * targetSize);
const targetHeight =
image.height > image.width
? targetSize
: Math.round((image.height / image.width) * targetSize);

ctx.drawImage(image, 0, 0, targetWidth, targetHeight);

return new Promise((resolve, reject) => {
canvas.toBlob(
(blob) => {
if (!blob) {
reject(new Error("Failed to convert canvas to blob"));
return;
}
resolve(blob);
},
"image/jpeg",
0.7,
);
});
}

async function loadImage(file: File): Promise<HTMLImageElement> {
return new Promise((resolve, reject) => {
const reader = new FileReader();
reader.onload = () => {
const image = new Image();
image.onload = () => resolve(image);
image.onerror = reject;
image.src = reader.result as string;
};
reader.onerror = reject;
reader.readAsDataURL(file);
});
}
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 0791990

Please sign in to comment.