36792-vm/api/pexels.php
2025-12-09 17:26:41 +00:00

20 lines
982 B
PHP

<?php
header('Content-Type: application/json');
require_once __DIR__.'/../includes/pexels.php';
$q = isset($_GET['query']) ? $_GET['query'] : 'nature';
$orientation = isset($_GET['orientation']) ? $_GET['orientation'] : 'landscape';
$url = 'https://api.pexels.com/v1/search?query=' . urlencode($q) . '&orientation=' . urlencode($orientation) . '&per_page=1&page=1';
$data = pexels_get($url);
if (!$data || empty($data['photos'])) { echo json_encode(['error'=>'Failed to fetch image']); exit; }
$photo = $data['photos'][0];
$src = $photo['src']['large2x'] ?? ($photo['src']['large'] ?? $photo['src']['original']);
$target = __DIR__ . '/../assets/images/pexels/' . $photo['id'] . '.jpg';
download_to($src, $target);
// Return minimal info and local relative path
echo json_encode([
'id' => $photo['id'],
'local' => 'assets/images/pexels/' . $photo['id'] . '.jpg',
'photographer' => $photo['photographer'] ?? null,
'photographer_url' => $photo['photographer_url'] ?? null,
]);