$photo['id'], 'local_path' => 'assets/images/pexels/' . $filename, 'photographer' => $photo['photographer'] ?? 'Unknown', 'photographer_url' => $photo['photographer_url'] ?? '#', 'avg_color' => $photo['avg_color'] ]; } else { $errors[] = "Image file does not exist at " . $target; } } else { $errors[] = "Failed to fetch data from Pexels for query: " . $q; } } // Ensure we have 3 models, even if Pexels fails, use placeholders while (count($results) < 3) { $placeholder_index = count($results) + 1; $results[] = [ 'id' => 'placeholder_' . $placeholder_index, // Using an external placeholder service as a fallback 'local_path' => 'https://via.placeholder.com/400x600.png?text=Model+' . $placeholder_index, 'photographer' => 'Placeholder', 'photographer_url' => '#', 'avg_color' => '#cccccc' ]; } $response = [ 'models' => array_slice($results, 0, 3) ]; if (!empty($errors)) { $response['errors'] = $errors; } echo json_encode($response);