Skip to content

Praharsh7270/ImageGenrator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

7 Commits
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

๐ŸŽจ AI Image Generator

A full-stack web application that generates AI images from text prompts using React, Node.js, and ClipDrop API.

AI Image Generator React Node.js MongoDB

๐Ÿ“‹ Table of Contents

โœจ Features

๐Ÿ”ฅ Core Features

  • AI Image Generation: Convert text prompts to stunning images using ClipDrop API
  • User Authentication: Secure registration and login with JWT tokens
  • Credits System: Each user gets 5 free credits, with ability to purchase more
  • Real-time Feedback: Toast notifications for all user actions
  • Responsive Design: Works seamlessly on desktop and mobile devices
  • Smooth Animations: Beautiful Framer Motion animations throughout the app

๐Ÿš€ Advanced Features

  • Automatic Token Management: JWT tokens stored in localStorage with auto-refresh
  • Credit Tracking: Real-time credit updates after image generation
  • Download Functionality: Download generated images directly
  • Protected Routes: Authentication required for image generation
  • Error Handling: Comprehensive error handling with user-friendly messages
  • Loading States: Beautiful loading animations during image generation

๐Ÿ› ๏ธ Tech Stack

Frontend

  • React 19.1.0 - UI Framework
  • Vite - Build tool and development server
  • Tailwind CSS 4.1.11 - Styling and responsive design
  • Framer Motion 12.23.12 - Animations and transitions
  • React Router DOM 7.7.0 - Client-side routing
  • Axios 1.11.0 - HTTP client for API calls
  • React Toastify 11.0.5 - Toast notifications

Backend

  • Node.js with Express 5.1.0 - Server framework
  • MongoDB with Mongoose 8.17.0 - Database and ODM
  • JWT (jsonwebtoken 9.0.2) - Authentication tokens
  • bcrypt 6.0.0 - Password hashing
  • CORS 2.8.5 - Cross-origin resource sharing
  • dotenv 17.2.1 - Environment variable management

External APIs

  • ClipDrop API - AI image generation
  • MongoDB Atlas - Cloud database

๐Ÿ“ Project Structure

ImageGenrator/
โ”œโ”€โ”€ Client/                          # Frontend React application
โ”‚   โ”œโ”€โ”€ public/                      # Static assets
โ”‚   โ”‚   โ”œโ”€โ”€ favicon.svg
โ”‚   โ”‚   โ””โ”€โ”€ vite.svg
โ”‚   โ”œโ”€โ”€ src/
โ”‚   โ”‚   โ”œโ”€โ”€ assets/                  # Images, icons, and static files
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ logo.svg
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ profile_icon.png
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ credit_star.svg
โ”‚   โ”‚   โ”‚   โ””โ”€โ”€ [40+ other assets]
โ”‚   โ”‚   โ”œโ”€โ”€ Components/              # Reusable React components
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ Navbar.jsx          # Navigation with user info
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ Login.jsx           # Authentication modal
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ Header.jsx          # Hero section
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ Descrip.jsx         # Description section
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ Steps.jsx           # How it works section
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ Testimonials.jsx    # User testimonials
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ GenrateBtn.jsx      # Generate image button
โ”‚   โ”‚   โ”‚   โ””โ”€โ”€ Fotter.jsx          # Footer component
โ”‚   โ”‚   โ”œโ”€โ”€ Context/
โ”‚   โ”‚   โ”‚   โ””โ”€โ”€ AppContext.jsx      # Global state management
โ”‚   โ”‚   โ”œโ”€โ”€ Pages/                  # Main application pages
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ Home.jsx            # Landing page
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ Result.jsx          # Image generation page
โ”‚   โ”‚   โ”‚   โ””โ”€โ”€ BuyCredit.jsx       # Credit purchase page
โ”‚   โ”‚   โ”œโ”€โ”€ App.jsx                 # Main app component
โ”‚   โ”‚   โ”œโ”€โ”€ main.jsx                # React entry point
โ”‚   โ”‚   โ””โ”€โ”€ index.css               # Global styles
โ”‚   โ”œโ”€โ”€ .env                        # Frontend environment variables
โ”‚   โ”œโ”€โ”€ package.json                # Frontend dependencies
โ”‚   โ”œโ”€โ”€ vite.config.js              # Vite configuration
โ”‚   โ””โ”€โ”€ tailwind.config.js          # Tailwind CSS configuration
โ”œโ”€โ”€ server/                         # Backend Node.js application
โ”‚   โ”œโ”€โ”€ Config/
โ”‚   โ”‚   โ””โ”€โ”€ Mongodb.js              # Database connection
โ”‚   โ”œโ”€โ”€ Controllers/                # Business logic
โ”‚   โ”‚   โ”œโ”€โ”€ UserController.js       # User authentication & credits
โ”‚   โ”‚   โ””โ”€โ”€ ImageController.js      # Image generation logic
โ”‚   โ”œโ”€โ”€ Middlewares/
โ”‚   โ”‚   โ””โ”€โ”€ Auth.js                 # JWT authentication middleware
โ”‚   โ”œโ”€โ”€ Models/
โ”‚   โ”‚   โ””โ”€โ”€ UserModel.js            # User data schema
โ”‚   โ”œโ”€โ”€ Routes/                     # API route definitions
โ”‚   โ”‚   โ”œโ”€โ”€ UserRoutes.js           # User-related endpoints
โ”‚   โ”‚   โ””โ”€โ”€ ImageRoutes.js          # Image generation endpoints
โ”‚   โ”œโ”€โ”€ .env                        # Backend environment variables
โ”‚   โ”œโ”€โ”€ package.json                # Backend dependencies
โ”‚   โ””โ”€โ”€ server.js                   # Express server entry point
โ””โ”€โ”€ readme.md                       # This file

๐Ÿš€ Installation

Prerequisites

  • Node.js (v16 or higher)
  • MongoDB Atlas account
  • ClipDrop API key

1. Clone the Repository

git clone https://github.com/Praharsh7270/ImageGenrator.git
cd ImageGenrator

2. Backend Setup

cd server
npm install

3. Frontend Setup

cd ../Client
npm install

๐Ÿ”ง Environment Variables

Backend (.env in server folder)

PORT=5000
MONGO_URI="your_mongodb_connection_string"
JWT_SECRET="your_jwt_secret_key"
CLIPDROP_api="your_clipdrop_api_key"

Frontend (.env in Client folder)

VITE_BACKENDURL="http://localhost:5000"

Get Your API Keys

  1. MongoDB Atlas:

    • Create account at MongoDB Atlas
    • Create cluster and get connection string
  2. ClipDrop API:

    • Sign up at ClipDrop
    • Get your API key from dashboard

๐ŸŒ API Endpoints

User Authentication

POST /api/user/register    # Register new user
POST /api/user/login       # User login
POST /api/user/credits     # Get user credits (Protected)

Image Generation

POST /api/image/generateImage    # Generate AI image (Protected)

Request/Response Examples

Register User

// Request
POST /api/user/register
{
  "name": "John Doe",
  "email": "john@example.com",
  "password": "password123"
}

// Response
{
  "success": true,
  "user": { ... },
  "token": "jwt_token_here"
}

Generate Image

// Request (with JWT token in headers)
POST /api/image/generateImage
Headers: { "token": "your_jwt_token" }
{
  "prompt": "a beautiful sunset over mountains"
}

// Response
{
  "success": true,
  "image": "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAA...",
  "message": "Image generated successfully",
  "resultImage": "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAA..."
}

๐Ÿงฉ Components Overview

Core Components

๐Ÿ” Authentication System

  • Login.jsx: Modal with animated form for login/register
  • Auth.js: JWT middleware for protected routes
  • AppContext.jsx: Global state management for user data

๐ŸŽจ Image Generation

  • Result.jsx: Main image generation interface with loading states
  • ImageController.js: Backend logic for ClipDrop API integration
  • GenrateBtn.jsx: Call-to-action button with animations

๐Ÿ  Landing Page

  • Home.jsx: Combines all sections for landing page
  • Header.jsx: Hero section with animated elements
  • Descrip.jsx: Feature descriptions
  • Steps.jsx: How-to-use section
  • Testimonials.jsx: User feedback section

๐Ÿงญ Navigation & Layout

  • Navbar.jsx: Responsive navigation with user info
  • Fotter.jsx: Footer with social links

๐Ÿ” Authentication

JWT Token Flow

  1. User registers/logs in โ†’ Server generates JWT token
  2. Token stored in localStorage and React state
  3. All protected API calls include token in headers
  4. Server validates token using middleware
  5. Auto-redirect to login if token invalid/expired

Security Features

  • Password Hashing: bcrypt with salt rounds
  • JWT Expiration: 30-day token validity
  • Protected Routes: Authentication required for image generation
  • Input Validation: Server-side validation for all inputs

๐Ÿ’ฐ Credits System

How It Works

  • Free Credits: 5 credits given on registration
  • Credit Deduction: 1 credit per image generation
  • Credit Tracking: Real-time updates in UI
  • Credit Protection: Frontend and backend validation
  • Purchase Flow: Redirect to buy credits page when insufficient

Credit Management

// Frontend credit check
if (credits <= 0) {
    toast.error("Insufficient credits!");
    navigate("/buycredit");
    return;
}

// Backend credit validation
if (user.credits <= 0) {
    return res.status(400).json({ 
        success: false, 
        message: "Insufficient credits" 
    });
}

๐ŸŽฏ Usage

Running the Application

  1. Start Backend Server
cd server
npm run server    # Uses nodemon for development
# or
npm start         # For production
  1. Start Frontend Development Server
cd Client
npm run dev       # Starts Vite dev server
  1. Access Application

User Journey

  1. Landing Page: User sees hero section, features, and testimonials
  2. Sign Up: Click "Get Started" โ†’ Register with email/password
  3. Dashboard: Access to image generation with 5 free credits
  4. Generate Image: Enter prompt โ†’ Beautiful loading animation โ†’ Generated image
  5. Download: Save generated image to device
  6. Credits: Monitor remaining credits in navbar
  7. Buy Credits: Redirect when credits run out

๐ŸŽฌ Key Features in Action

Animation System

  • Framer Motion: Smooth page transitions and micro-interactions
  • Loading States: Beautiful progress indicators during image generation
  • Hover Effects: Interactive buttons and cards
  • Page Transitions: Seamless navigation between pages

Responsive Design

  • Mobile-First: Optimized for all device sizes
  • Tailwind CSS: Utility-first CSS framework
  • Flexible Layouts: Adapts to different screen sizes
  • Touch-Friendly: Mobile gesture support

Error Handling

  • Toast Notifications: User-friendly error messages
  • Network Errors: Graceful handling of API failures
  • Validation: Client and server-side input validation
  • Loading States: Clear feedback during async operations

๐Ÿ”„ State Management

Global State (AppContext)

const AppContext = {
  user: null,              // Current user data
  token: "jwt_token",      // Authentication token
  credits: 5,              // Available credits
  showLogin: false,        // Login modal visibility
  generateImage: fn,       // Image generation function
  getUserCredits: fn,      // Fetch user credits
  logout: fn              // Logout function
}

๐Ÿ› Debugging & Development

Common Issues & Solutions

  1. CORS Errors

    • Ensure CORS is enabled in server.js
    • Check frontend/backend URLs match
  2. Authentication Failures

    • Verify JWT_SECRET in environment variables
    • Check token format in API calls
  3. Image Generation Issues

    • Validate ClipDrop API key
    • Check credit availability
    • Monitor API rate limits

Development Tips

  • Use React DevTools for component debugging
  • Monitor Network tab for API call issues
  • Check browser console for JavaScript errors
  • Use MongoDB Compass for database inspection

๐Ÿ“ฑ Screenshots

Note: Add screenshots of your application here

  • Landing page with hero section
  • Image generation interface
  • Generated image with download options
  • User authentication modal
  • Credits tracking in navigation

๐Ÿค Contributing

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/AmazingFeature)
  3. Commit your changes (git commit -m 'Add some AmazingFeature')
  4. Push to the branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

๐Ÿ“„ License

This project is licensed under the ISC License.

๐Ÿ‘จโ€๐Ÿ’ป Author

Praharsh Singh

๐Ÿ™ Acknowledgments

  • ClipDrop API for AI image generation
  • MongoDB Atlas for cloud database hosting
  • Vercel/Netlify for deployment options
  • React Community for excellent documentation
  • Tailwind CSS for utility-first styling

๐Ÿ”ฎ Future Enhancements

  • Payment integration for credit purchases
  • Image history and gallery
  • Advanced prompt suggestions
  • Social sharing features
  • Image editing tools
  • Batch image generation
  • User profile customization
  • Admin dashboard
  • API rate limiting
  • Image optimization and compression

Made with โค๏ธ by Praharsh Singh

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages