# Image Upload API with QR Code Generation

A PHP API that allows users to upload images, automatically generates QR codes linking to the uploaded images, and provides a clean viewing interface.

## Features

- ✅ Secure image upload with validation
- ✅ Automatic QR code generation
- ✅ Database storage with prepared statements
- ✅ Beautiful web interface
- ✅ RESTful API endpoints
- ✅ Responsive design
- ✅ Error handling and logging

## Requirements

- PHP 7.4 or higher
- MySQL/MariaDB
- XAMPP or similar local server
- Internet connection (for QR code generation)

## Installation

1. Place all files in your web server directory (e.g., `c:\xampp\htdocs\stsimageqr\`)
2. Create a MySQL database named `stsimageqr`
3. Update database credentials in `connect.php` if needed
4. Ensure the web server has write permissions for the uploads directory
5. Access `index.php` in your browser

## Database Schema

The system automatically creates two tables:

### `images` table
- `id` - Primary key
- `filename` - Generated unique filename
- `original_name` - Original uploaded filename
- `file_path` - Path to stored image
- `file_size` - File size in bytes
- `mime_type` - Image MIME type
- `upload_date` - Upload timestamp

### `qr_codes` table
- `id` - Primary key
- `image_id` - Foreign key to images table
- `qr_filename` - QR code filename
- `qr_path` - Path to QR code image
- `view_link` - Link encoded in QR code
- `created_date` - Creation timestamp

## API Endpoints

### Upload Image
**POST** `/upload.php`

**Content-Type:** `multipart/form-data`

**Parameters:**
- `image` (file) - Image file to upload

**Response:**
```json
{
  "success": true,
  "message": "Image uploaded and QR code generated successfully",
  "data": {
    "image_id": 1,
    "image_filename": "img_123456.jpg",
    "image_path": "uploads/images/img_123456.jpg",
    "view_link": "http://localhost/stsimageqr/view.php?id=1",
    "qr_filename": "qr_1_789012.png",
    "qr_path": "uploads/qrcodes/qr_1_789012.png",
    "qr_link": "http://localhost/stsimageqr/uploads/qrcodes/qr_1_789012.png"
  }
}
```

### View Image
**GET** `/view.php?id={image_id}`

Displays the uploaded image with metadata and QR code.

## File Structure

```
stsimageqr/
├── connect.php              # Database connection with security
├── upload.php               # Main upload API endpoint
├── view.php                 # Image viewing interface
├── qrcode_generator.php     # QR code generation utility
├── index.php                # Web interface for testing
├── README.md                # This documentation
└── uploads/
    ├── images/              # Uploaded images storage
    └── qrcodes/             # Generated QR codes storage
```

## Security Features

- ✅ Prepared statements for all database queries
- ✅ File type validation (JPEG, PNG, GIF, WebP only)
- ✅ File size limits (10MB max)
- ✅ Unique filename generation to prevent conflicts
- ✅ Error logging without exposing sensitive information
- ✅ Input sanitization and validation
- ✅ UTF-8 support with proper charset handling

## Usage Examples

### Using cURL
```bash
curl -X POST -F "image=@/path/to/your/image.jpg" http://localhost/stsimageqr/upload.php
```

### Using JavaScript Fetch
```javascript
const formData = new FormData();
formData.append('image', fileInput.files[0]);

fetch('/stsimageqr/upload.php', {
    method: 'POST',
    body: formData
})
.then(response => response.json())
.then(data => console.log(data));
```

## Error Handling

The API returns appropriate HTTP status codes:

- `200` - Success
- `400` - Bad request (validation errors)
- `405` - Method not allowed
- `500` - Server error

Error responses include descriptive messages:
```json
{
  "error": "No image file provided"
}
```

## QR Code Generation

The system uses online QR code APIs with fallback options:
1. Google Charts API (primary)
2. QR Server API (fallback)

QR codes link directly to the image view page: `/view.php?id={image_id}`

## Troubleshooting

### Common Issues

1. **Database connection failed**
   - Check MySQL service is running
   - Verify database credentials in `connect.php`
   - Ensure database `stsimageqr` exists

2. **Upload directory errors**
   - Check write permissions on `uploads/` folder
   - Ensure `uploads/images/` and `uploads/qrcodes/` directories exist

3. **QR code generation failed**
   - Check internet connection
   - Verify server can make outbound HTTP requests

4. **File upload errors**
   - Check PHP `upload_max_filesize` and `post_max_size` settings
   - Verify file meets validation requirements

## License

This project is open source and available under the MIT License.
