A modern web application that calculates and visualizes the environmental impact of AI-generated ad banners. Upload an ad banner, and get detailed CO2 emission estimates powered by OpenAI's API, complete with environmental comparisons and recovery metrics.
- π’ Drag & Drop Ad Upload - Intuitive interface with visual feedback
- π€ AI-Powered Analysis - Uses OpenAI API to estimate CO2 emissions based on ad banner characteristics
- π Detailed Metrics - View generation CO2, transmission CO2, and total environmental impact
- π± Recovery Metrics - See how many trees, plastic bottles, bike kilometers, and ocean hours needed to offset emissions
- π Dynamic Calculations - Adjust view counts (1K to 1B impressions) and see real-time recalculations
- π Modern UI - Glassmorphism effects, smooth animations, and emoji-enhanced visuals
- π± Fully Responsive - Optimized for mobile, tablet, and desktop devices
- β‘ Fast & Lightweight - Built with Vite for optimal performance
- Node.js 16+ and npm
- OpenAI API key (Get one here)
-
Clone the repository
git clone <repository-url> cd ai-image-co2-calculator
-
Install dependencies
npm install
-
Set up environment variables
Create a
.envfile in the root directory:cp .env.example .env
Open
.envand add your OpenAI API key:VITE_OPENAI_API_KEY=your-api-key-here
β οΈ Important: Never commit your.envfile to version control. It's already included in.gitignore. -
Start the development server
npm run dev
The application will open at
http://localhost:5173
npm run dev- Start development server with hot reloadnpm run build- Build for production (outputs todist/)npm run preview- Preview production build locallynpm run lint- Run ESLint to check code qualitynpm test- Run unit tests
-
Upload an Ad Banner
- Drag and drop an AI-generated ad banner onto the upload zone
- Or click to select a file (supports PNG, JPG, JPEG, WebP, GIF)
-
View Analysis
- Ad banner metadata (resolution, file size, format) is extracted automatically
- OpenAI API analyzes the ad and estimates CO2 emissions
-
Explore Impact
- See generation CO2 (emissions from creating the ad banner)
- Adjust view count to see transmission CO2 impact across impressions
- View environmental comparisons (everyday activities & digital equivalents)
- Check recovery metrics (trees, recycling, transportation)
-
Upload Another
- Click "Upload New Ad" to analyze a different banner
ai-image-co2-calculator/
βββ components/ # React components
β βββ CO2EmissionCard.tsx
β βββ EnvironmentalComparisons.tsx
β βββ ErrorDisplay.tsx
β βββ ImageMetadataDisplay.tsx
β βββ ImageUpload.tsx
β βββ ImpactSummary.tsx
β βββ LoadingSpinner.tsx
β βββ RecoveryMetrics.tsx
β βββ ViewCountSelector.tsx
βββ utils/ # Utility functions
β βββ co2Calculations.ts
β βββ imageProcessing.ts
β βββ openai.ts
βββ src/
β βββ main.tsx # Application entry point
βββ App.tsx # Main application component
βββ types.ts # TypeScript type definitions
βββ index.html # HTML template
βββ index.css # Global styles
βββ tailwind.config.js # Tailwind CSS configuration
βββ vite.config.ts # Vite configuration
βββ package.json # Project dependencies
- Frontend: React 18 with TypeScript
- Styling: Tailwind CSS with custom animations
- Icons: Lucide React
- Build Tool: Vite
- API: OpenAI GPT-4o-mini/GPT-4o
- Testing: Vitest, Testing Library
| Variable | Description | Required |
|---|---|---|
VITE_OPENAI_API_KEY |
Your OpenAI API key | Yes |
VITE_OPENAI_MODEL |
OpenAI model to use (default: gpt-4) | No |
-
Build the application
npm run build
-
Preview the build
npm run preview
-
Deploy
- The
dist/folder contains the production-ready files - Deploy to any static hosting service (Vercel, Netlify, GitHub Pages, etc.)
- Remember to set environment variables in your hosting platform
- The
The project includes comprehensive tests:
# Run all tests
npm test
# Run specific test files
npm run test:runTest coverage includes:
- Image processing utilities
- CO2 calculation formulas
- Component rendering and interactions
- API integration (with mocks)
- Responsive design
- End-to-end user flows
The application calculates two types of emissions:
-
Generation CO2: Emissions from creating the AI ad banner
- Based on ad resolution, file size, and format
- Considers the AI model (MugenAI Ads 2), system specs, and server location
- Estimated using OpenAI API analysis
-
Transmission CO2: Emissions from serving the ad banner
- Calculated per impression based on file size
- Multiplied by selected view count (1K to 1B impressions)
Results are contextualized with relatable comparisons:
- Everyday activities: Driving, charging phones, boiling water, etc.
- Digital activities: Emails, video streaming, web browsing, etc.
Shows what's needed to offset the emissions:
- π³ Trees to plant (1 tree absorbs ~21kg CO2/year)
- β»οΈ Plastic bottles to recycle (saves ~0.03kg CO2 each)
- π΄ Bike kilometers vs driving (saves ~0.12kg CO2/km)
- π Ocean absorption time (oceans absorb ~2.5kg CO2/mΒ²/year)
- Modern & Engaging: Glassmorphism effects, gradient borders, smooth animations
- User-Friendly: Clear visual feedback, emoji indicators, intuitive interactions
- Performance-First: Optimized bundle size, efficient rendering, 60fps animations
- Accessible: Keyboard navigation, proper contrast, responsive touch targets
- Mobile-Optimized: Single-column layouts, touch-friendly buttons, adaptive spacing
- API keys are stored in environment variables (never in code)
- File uploads are processed client-side only (no server storage)
- Input validation prevents malicious file uploads
- API responses are sanitized before display
- Ensure your
.envfile exists and containsVITE_OPENAI_API_KEY - Restart the dev server after changing environment variables
- Check that your OpenAI API key is valid and has credits
- Clear node_modules and reinstall:
rm -rf node_modules && npm install - Clear Vite cache:
rm -rf node_modules/.vite - Ensure you're using Node.js 16 or higher
- Check file size (limit: 10MB)
- Verify file format (PNG, JPG, JPEG, WebP, GIF)
- Try a different browser if drag-and-drop isn't working
Contributions are welcome! Please feel free to submit a Pull Request.
This project is licensed under the MIT License.
- OpenAI for the API that powers emission estimates
- Tailwind CSS for the styling framework
- Lucide for the beautiful icons
- The React and Vite teams for excellent developer tools
For questions or feedback, please open an issue on GitHub.
Made with π for a more sustainable digital future