25 lines
1.1 KiB
PHP
25 lines
1.1 KiB
PHP
<?php
|
|
header('Content-Type: application/json');
|
|
require_once __DIR__.'/../includes/pexels.php';
|
|
|
|
if (php_sapi_name() === 'cli') {
|
|
parse_str(implode('&', array_slice($argv, 1)), $_GET);
|
|
}
|
|
|
|
$q = isset($_GET['query']) ? $_GET['query'] : 'nature';
|
|
$orientation = isset($_GET['orientation']) ? $_GET['orientation'] : 'portrait';
|
|
$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,
|
|
]);
|