'API key not configured.']); exit; } if ($_SERVER['REQUEST_METHOD'] !== 'POST' || !isset($_FILES['vehicleImage'])) { http_response_code(400); echo json_encode(['error' => 'Invalid request.']); exit; } $file = $_FILES['vehicleImage']; if ($file['error'] !== UPLOAD_ERR_OK) { http_response_code(500); echo json_encode(['error' => 'Error uploading file.']); exit; } // Check file type $mime_type = mime_content_type($file['tmp_name']); if (strpos($mime_type, 'image') !== 0) { http_response_code(400); echo json_encode(['error' => 'Invalid file type. Please upload an image.']); exit; } $imageData = file_get_contents($file['tmp_name']); $base64Image = base64_encode($imageData); $payload = [ 'model' => 'gpt-4o', 'messages' => [ [ 'role' => 'system', 'content' => 'You are an expert in vehicle damage assessment. Analyze the provided image to identify the vehicle\'s make, model, and color, and find all damages. Respond with a single JSON object with two top-level keys: "carInfo" and "defects". The "carInfo" key should contain an object with "make", "model", and "color". The "defects" key should contain an array of objects, where each object has two keys: "defect" (a string describing the damage) and "cost" (a number representing the estimated repair cost in EUR).' ], [ 'role' => 'user', 'content' => [ [ 'type' => 'text', 'text' => 'Please analyze the attached image for vehicle defects and list them.' ], [ 'type' => 'image_url', 'image_url' => [ 'url' => "data:{$mime_type};base64,{$base64Image}" ] ] ] ] ], 'response_format' => ['type' => 'json_object'], 'max_tokens' => 1000 ]; $ch = curl_init('https://api.openai.com/v1/chat/completions'); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($payload)); curl_setopt($ch, CURLOPT_HTTPHEADER, [ 'Content-Type: application/json', 'Authorization: Bearer ' . OPENAI_API_KEY ]); $response = curl_exec($ch); $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE); curl_close($ch); if ($httpcode >= 400) { http_response_code($httpcode); // Forward the error from OpenAI if possible $error_response = json_decode($response, true); if (isset($error_response['error']['message'])) { echo json_encode(['error' => 'OpenAI API Error: ' . $error_response['error']['message']]); } else { echo json_encode(['error' => 'An error occurred with the OpenAI API.']); } exit; } $result = json_decode($response, true); if (isset($result['choices'][0]['message']['content'])) { // The response from OpenAI is a JSON string, so we need to decode it again $defects_json = $result['choices'][0]['message']['content']; $defects_data = json_decode($defects_json, true); if (json_last_error() === JSON_ERROR_NONE && (isset($defects_data['defects']) || isset($defects_data['carInfo']))) { echo json_encode($defects_data); } else { // Handle cases where the response is not the expected JSON http_response_code(500); echo json_encode(['error' => 'Failed to parse defects from API response.', 'raw_response' => $defects_json]); } } else { http_response_code(500); echo json_encode(['error' => 'No content received from API.', 'raw_response' => $response]); } ?>