<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Usama Nazir</title>
    <description>The latest articles on Forem by Usama Nazir (@usama_d14e7149bf47b1).</description>
    <link>https://forem.com/usama_d14e7149bf47b1</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3777295%2F791f72bd-cd5c-488d-81f9-382262086e12.JPG</url>
      <title>Forem: Usama Nazir</title>
      <link>https://forem.com/usama_d14e7149bf47b1</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/usama_d14e7149bf47b1"/>
    <language>en</language>
    <item>
      <title>How I Build an AI-Powered Virtual Try-On For Men’s Clothing Brand</title>
      <dc:creator>Usama Nazir</dc:creator>
      <pubDate>Tue, 17 Feb 2026 10:15:46 +0000</pubDate>
      <link>https://forem.com/usama_d14e7149bf47b1/how-i-build-an-ai-powered-virtual-try-on-for-mens-clothing-brand-264f</link>
      <guid>https://forem.com/usama_d14e7149bf47b1/how-i-build-an-ai-powered-virtual-try-on-for-mens-clothing-brand-264f</guid>
      <description>&lt;p&gt;The fashion industry has long struggled with one fundamental challenge, how do customers know if something will look good on them before buying? Physical try-ons are time-consuming, e-commerce returns are costly, and customers are left guessing.&lt;/p&gt;

&lt;p&gt;Today, I'm excited to share how I built “Shabab Al Yola Virtual Try-On” &lt;a href="https://www.shababalyola.ae/virtual-try-on" rel="noopener noreferrer"&gt;https://www.shababalyola.ae/virtual-try-on&lt;/a&gt;, an AI-powered virtual fitting solution that's changing how customers shop for traditional attire like Kandoras, Thobes, and Bishts.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Problems
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Traditional online shopping for custom tailoring has massive friction:&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Customers can't "see" how fabric looks on them&lt;/li&gt;
&lt;li&gt;Fitting issues lead to expensive returns&lt;/li&gt;
&lt;li&gt;Language barriers in regional fashion (Arabic attire)&lt;/li&gt;
&lt;li&gt;No way to visualize style + color combinations&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The solution? An intelligent virtual try-on that lets users upload a photo, select a style and color, and generate a realistic preview, in seconds.&lt;/p&gt;

&lt;h2&gt;
  
  
  Here's what powers our Virtual Try-On:
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Frontend:&lt;/strong&gt; Next.js 14 + TypeScript&lt;br&gt;
&lt;strong&gt;AI Image Generation:&lt;/strong&gt; Gemini 2.5 Image Pro (via Runware SDK)&lt;br&gt;
&lt;strong&gt;Image Storage:&lt;/strong&gt; Cloudflare R2 (S3-compatible)&lt;br&gt;
&lt;strong&gt;API Layer:&lt;/strong&gt; Next.js Route Handlers&lt;br&gt;
&lt;strong&gt;Validation:&lt;/strong&gt; Zod&lt;/p&gt;
&lt;h2&gt;
  
  
  Architecture Overview
&lt;/h2&gt;

&lt;p&gt;User uploads photo → Crop &amp;amp; Process → Upload to R2 → Select Style/Color → Generate AI Image → Display Result&lt;/p&gt;

&lt;p&gt;The flow is designed for speed. I use an optimistic approach where the image uploads while the user selects their preferences, minimizing perceived latency.&lt;/p&gt;
&lt;h2&gt;
  
  
  Core Implementation
&lt;/h2&gt;
&lt;h2&gt;
  
  
  1. Image Upload with Presigned URLs
&lt;/h2&gt;

&lt;p&gt;We use Cloudflare R2 for storage with a presigned URL pattern. This keeps our serverless function lightweight and scales infinitely:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// API Route: /api/r2/presign
const command = new PutObjectCommand({
  Bucket: bucket,
  Key: `faces/${Date.now()}-${crypto.randomUUID()}.${extension}`,
  ContentType: mime,
});

const url = await getSignedUrl(r2(), command, { expiresIn: 300 });
// Returns uploadUrl to client, publicUrl for AI processing
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Why this matters:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;No server bottleneck on file uploads&lt;/li&gt;
&lt;li&gt;Direct upload to R2 = faster + cheaper&lt;/li&gt;
&lt;li&gt;Rate limiting protects against abuse&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  2. AI-Powered Image Generation
&lt;/h2&gt;

&lt;p&gt;Once we have the user's photo and their style preferences, we send everything to the AI:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// API Route: /api/generate
const payload = {
  positivePrompt: generateTryOnPrompt(style, color),
  model: "google:4@1",
  numberResults: 1,
  outputType: "URL",
  referenceImages: [faceURL],
};

const images = await client.requestImages(payload);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  3. Smart Prompt Engineering
&lt;/h2&gt;

&lt;p&gt;The secret sauce is our prompt builder. It instructs the AI to preserve the user's identity while applying the selected style:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export const generateTryOnPrompt = (style: StyleOption, color: ColorOption): string =&amp;gt; {
  return `Generate a high-resolution, photorealistic fashion photoshoot 
  of the EXACT person from the reference image.

  CRITICAL IDENTITY INSTRUCTIONS:
  PRESERVE EXACT FACE: Keep user's face shape, jawline, beard style
  PRESERVE BODY TYPE: Don't standardize to a fitness model body
  ACCESSORIES: If wearing glasses, keep them exactly as seen

  Clothing: He wears a classic ${color.value} ${style.name};
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Key Features of AI Try-On
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;1- Identity Preservation:&lt;/strong&gt; The model strictly maintains the user's facial features, body type, and accessories, no unwanted "beautification" or body shape changes.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2- Cultural Authenticity:&lt;/strong&gt; We support regional styles: Emirati Kandora, Saudi Thobe, Qatari Thobe, each with precise collar and cuff details.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3- Fast &amp;amp; Responsive:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Presigned uploads remove server bottlenecks&lt;/li&gt;
&lt;li&gt;Concurrent processing of style selection + image upload&lt;/li&gt;
&lt;li&gt;Optimized prompt length for faster AI response&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;4- Secure by Design&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Only whitelisted image domains allowed&lt;/li&gt;
&lt;li&gt;Rate limiting on upload endpoints&lt;/li&gt;
&lt;li&gt;Cookie-based usage limits (3 free tries/day)&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Challenges I Solved
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;1. Image Security:&lt;/strong&gt; Validating that user-uploaded images come from trusted sources only&lt;br&gt;
&lt;strong&gt;2. Timeout Handling:&lt;/strong&gt; AI image generation can take time, I implemented smart timeouts with user-friendly error messages&lt;br&gt;
&lt;strong&gt;3. Cost Optimization:&lt;/strong&gt; Tracking generation costs per request to manage API spend&lt;br&gt;
&lt;strong&gt;4. Mobile-First:&lt;/strong&gt; Ensuring the upload flow works smoothly on mobile devices&lt;/p&gt;

</description>
      <category>ai</category>
      <category>automation</category>
    </item>
  </channel>
</rss>
