├── image.php └── README.md /image.php: -------------------------------------------------------------------------------- 1 | 'ImageAI API is operational and Powered by @TheSmartDev', 16 | 'endpoints' => [ 17 | 'post' => '/api.php (with {imageBase64, prompt} in body)' 18 | ], 19 | 'limits' => [ 20 | 'max_image_size' => '5MB', 21 | 'supported_formats' => 'JPEG, PNG, WEBP' 22 | ] 23 | ]); 24 | exit; 25 | } 26 | 27 | // Handle POST requests 28 | if ($_SERVER['REQUEST_METHOD'] === 'POST') { 29 | try { 30 | // Verify content type 31 | $contentType = isset($_SERVER['CONTENT_TYPE']) ? $_SERVER['CONTENT_TYPE'] : ''; 32 | if (strpos($contentType, 'application/json') === false) { 33 | http_response_code(400); 34 | echo json_encode(['error' => 'Content-Type must be application/json']); 35 | exit; 36 | } 37 | 38 | // Get JSON input 39 | $json = file_get_contents('php://input'); 40 | $data = json_decode($json, true); 41 | 42 | if (json_last_error() !== JSON_ERROR_NONE) { 43 | throw new Exception('Invalid JSON input'); 44 | } 45 | 46 | $imageBase64 = $data['imageBase64'] ?? null; 47 | $mimeType = $data['mimeType'] ?? 'image/jpeg'; 48 | $prompt = $data['prompt'] ?? "Describe The Image Properly"; 49 | 50 | if (!$imageBase64) { 51 | http_response_code(400); 52 | echo json_encode(['error' => 'imageBase64 field is required']); 53 | exit; 54 | } 55 | 56 | // Replace with your Google API key 57 | $apiKey = 'AIzaSyCWJB43hGDiFKPBi41cBD3nuvp18B4ivf4'; 58 | 59 | // Call Google Gemini Vision API 60 | $url = "https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash:generateContent?key=$apiKey"; 61 | 62 | $payload = [ 63 | 'contents' => [ 64 | [ 65 | 'parts' => [ 66 | ['text' => $prompt], 67 | [ 68 | 'inlineData' => [ 69 | 'mimeType' => $mimeType, 70 | 'data' => $imageBase64 71 | ] 72 | ] 73 | ] 74 | ] 75 | ] 76 | ]; 77 | 78 | $ch = curl_init($url); 79 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 80 | curl_setopt($ch, CURLOPT_POST, true); 81 | curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($payload)); 82 | curl_setopt($ch, CURLOPT_HTTPHEADER, [ 83 | 'Content-Type: application/json' 84 | ]); 85 | 86 | $response = curl_exec($ch); 87 | $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); 88 | curl_close($ch); 89 | 90 | if ($httpCode !== 200) { 91 | $error = json_decode($response, true); 92 | throw new Exception($error['error']['message'] ?? 'Gemini API Request Failed'); 93 | } 94 | 95 | $result = json_decode($response, true); 96 | $analysis = $result['candidates'][0]['content']['parts'][0]['text'] ?? "No analysis available for this image"; 97 | 98 | echo json_encode([ 99 | 'success' => true, 100 | 'analysis' => $analysis 101 | ]); 102 | 103 | } catch (Exception $e) { 104 | http_response_code(500); 105 | echo json_encode([ 106 | 'success' => false, 107 | 'error' => 'Image Utils API Dead Bro ', 108 | 'details' => $e->getMessage() 109 | ]); 110 | } 111 | exit; 112 | } 113 | 114 | // Method not allowed 115 | http_response_code(405); 116 | echo json_encode(['error' => '405 Method not allowed']); 117 | ?> -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Image Info API 2 | 3 | A PHP-based service that analyzes images using the Google Gemini Vision API. Simply send a Base64-encoded image with a prompt to get AI-powered analysis and detailed descriptions. 4 | 5 | [![Stars](https://img.shields.io/github/stars/TheSmartDevs/Image-Info-API?color=black&logo=github&logoColor=black&style=for-the-badge)](https://github.com/TheSmartDevs/Image-Info-API/stargazers) 6 | [![Forks](https://img.shields.io/github/forks/TheSmartDevs/Image-Info-API?color=black&logo=github&logoColor=black&style=for-the-badge)](https://github.com/TheSmartDevs/Image-Info-API/network/members) 7 | [![License](https://img.shields.io/github/license/TheSmartDevs/Image-Info-API?color=blueviolet&style=for-the-badge)](https://github.com/TheSmartDevs/Image-Info-API/blob/master/LICENSE) 8 | [![Language](https://img.shields.io/badge/Written%20in-PHP-orange?style=for-the-badge&logo=php)](https://www.php.net/) 9 | 10 | **Repository:** [github.com/TheSmartDevs/Image-Info-API](https://github.com/TheSmartDevs/Image-Info-API) 11 | 12 | ## Features 13 | 14 | - **Image Analysis:** Extract detailed information from images using AI 15 | - **Format Support:** JPEG, PNG, WEBP 16 | - **Size Limit:** 5MB maximum image size 17 | - **CORS Support:** Configured for cross-origin requests 18 | - **Health Endpoint:** Easy verification of API status 19 | - **Powered by Google:** Uses advanced Gemini Vision API for analysis 20 | 21 | ## Prerequisites 22 | 23 | To host this API, you need: 24 | 25 | - Web server with PHP 7.4 or newer 26 | - PHP cURL extension enabled 27 | - Google API key from Google AI Studio 28 | - Domain with cPanel or similar hosting control panel 29 | 30 | ## Setup Instructions 31 | 32 | Follow these steps to host the Image Info API on your domain: 33 | 34 | 1. **Obtain a Google API Key** 35 | - Visit [Google AI Studio](https://aistudio.google.com/) 36 | - Sign in and generate a new API key 37 | - Replace the placeholder in `image.php` (line: `$apiKey = 'YOUR_API_KEY';`) 38 | 39 | 2. **Upload the API File** 40 | - Login to your hosting provider's control panel 41 | - Navigate to the public directory (e.g., `public_html`) 42 | - Upload `image.php` to your desired location 43 | - Set appropriate file permissions (644 recommended) 44 | 45 | 3. **Verify the Installation** 46 | - Access the API at `https://yourdomain.com/image.php` 47 | - Use the GET endpoint to confirm it's working 48 | 49 | 4. **Implement Security Measures** 50 | - Use HTTPS encryption 51 | - Consider adding authentication or rate-limiting 52 | - Rotate your Google API key regularly 53 | - Restrict API key usage in Google AI Studio 54 | 55 | ## API Usage 56 | 57 | The API provides two endpoints: 58 | 59 | ### 1. Health Check (GET) 60 | 61 | **Endpoint:** `https://yourdomain.com/image.php` 62 | **Method:** GET 63 | **Response:** API status, endpoints, and limits 64 | 65 | **Example Request:** 66 | ```bash 67 | curl https://yourdomain.com/image.php 68 | ``` 69 | 70 | **Example Response:** 71 | ```json 72 | { 73 | "status": "ImageAI API is operational and Powered by @TheSmartDev and @nkka404", 74 | "endpoints": { 75 | "post": "/image.php (with {imageBase64, prompt} in body)" 76 | }, 77 | "limits": { 78 | "max_image_size": "5MB", 79 | "supported_formats": "JPEG, PNG, WEBP" 80 | } 81 | } 82 | ``` 83 | 84 | ### 2. Image Analysis (POST) 85 | 86 | **Endpoint:** `https://yourdomain.com/image.php` 87 | **Method:** POST 88 | **Content-Type:** `application/json` 89 | 90 | **Request Body Parameters:** 91 | - `imageBase64`: Base64-encoded image (required) 92 | - `mimeType`: Image MIME type (optional, default: `image/jpeg`) 93 | - `prompt`: Analysis prompt (optional, default: "Describe The Image Properly") 94 | 95 | **Example Request:** 96 | ```bash 97 | curl -X POST https://yourdomain.com/image.php \ 98 | -H "Content-Type: application/json" \ 99 | -d '{ 100 | "imageBase64": "data:image/jpeg;base64,/9j/4AAQSkZJRgABAQE...", 101 | "mimeType": "image/jpeg", 102 | "prompt": "Describe the objects in this image" 103 | }' 104 | ``` 105 | 106 | **Success Response:** 107 | ```json 108 | { 109 | "success": true, 110 | "analysis": "The image shows a sunny beach with palm trees, clear blue water, and a small boat." 111 | } 112 | ``` 113 | 114 | **Error Response:** 115 | ```json 116 | { 117 | "success": false, 118 | "error": "Image Utils API Dead Bro", 119 | "details": "Invalid JSON input" 120 | } 121 | ``` 122 | 123 | ## Converting Images to Base64 124 | 125 | You can generate Base64 strings using: 126 | 127 | **Online Tools:** 128 | - [base64-image.de](https://www.base64-image.de/) 129 | 130 | **Command Line:** 131 | ```bash 132 | base64 -i image.jpg 133 | ``` 134 | 135 | **JavaScript:** 136 | ```javascript 137 | const file = document.querySelector('input[type="file"]').files[0]; 138 | const reader = new FileReader(); 139 | reader.onloadend = () => { 140 | const base64String = reader.result.split(',')[1]; 141 | console.log(base64String); 142 | }; 143 | reader.readAsDataURL(file); 144 | ``` 145 | 146 | ## Error Handling 147 | 148 | The API provides specific error codes and messages: 149 | 150 | - **400 Bad Request:** Invalid JSON, missing image data, or incorrect Content-Type 151 | - **405 Method Not Allowed:** Unsupported HTTP method 152 | - **500 Internal Server Error:** Issues with Gemini API or server configuration 153 | 154 | ## Limitations 155 | 156 | - **Maximum Image Size:** 5MB 157 | - **Supported Formats:** JPEG, PNG, WEBP only 158 | - **Rate Limits:** Subject to Google Gemini API limits and hosting provider restrictions 159 | - **Security:** API key should be properly secured in production environments 160 | 161 | ## Contributing 162 | 163 | We welcome contributions to improve the Image Info API: 164 | 165 | 1. Fork the repository: [github.com/TheSmartDevs/Image-Info-API](https://github.com/TheSmartDevs/Image-Info-API) 166 | 2. Create a branch for your feature or fix 167 | 3. Submit a pull request with clear description 168 | 169 | Please follow the existing code style and include tests where possible. 170 | 171 | ## Contact 172 | 173 | For questions or support: 174 | - Open an issue on [GitHub](https://github.com/TheSmartDevs/Image-Info-API) 175 | - Contact [@TheSmartDev](https://github.com/TheSmartDev) or [@nkka404](https://github.com/nkka404) 176 | 177 | --- 178 | 179 | *Powered by @TheSmartDev and @nkka404* --------------------------------------------------------------------------------