mirror of
https://github.com/daledavies/jump.git
synced 2026-05-07 11:56:04 +02:00
Add Unsplash background image support
This commit is contained in:
79
jumpapp/api/unsplashdata.php
Normal file
79
jumpapp/api/unsplashdata.php
Normal file
@@ -0,0 +1,79 @@
|
||||
<?php
|
||||
/**
|
||||
* Proxy requests to Unsplash API and cache response.
|
||||
*
|
||||
* @author Dale Davies <dale@daledavies.co.uk>
|
||||
* @license MIT
|
||||
*/
|
||||
|
||||
// Provided by composer for psr-4 style autoloading.
|
||||
require __DIR__ .'/../vendor/autoload.php';
|
||||
|
||||
$config = new Jump\Config();
|
||||
$cache = new Jump\Cache($config);
|
||||
|
||||
// If this script is run via CLI then clear the cache and repopulate it,
|
||||
// otherwise if run via web then get image data from cache and run this
|
||||
// script asynchronously to refresh the cahce for next time.
|
||||
if (http_response_code() === false) {
|
||||
$cache->clear('unsplash');
|
||||
load_cache_unsplash_data();
|
||||
die('Cached data from Unsplash');
|
||||
}
|
||||
|
||||
// Output header here so we can return early with a json response if there is a curl error.
|
||||
header('Content-Type: application/json; charset=utf-8');
|
||||
|
||||
// Initialise a new session using the request object.
|
||||
$session = new \Nette\Http\Session((new \Nette\Http\RequestFactory)->fromGlobals(), new \Nette\Http\Response);
|
||||
$session->setName($config->get('sessionname'));
|
||||
$session->setExpiration($config->get('sessiontimeout'));
|
||||
|
||||
// Get a Nette session section for CSRF data.
|
||||
$csrfsection = $session->getSection('csrf');
|
||||
|
||||
// Has a CSRF token been set up for the session yet?
|
||||
if (!$csrfsection->offsetExists('token')){
|
||||
http_response_code(401);
|
||||
die(json_encode(['error' => 'Session not fully set up']));
|
||||
}
|
||||
|
||||
// Check CSRF token saved in session against token provided via request.
|
||||
$token = isset($_GET['token']) ? $_GET['token'] : false;
|
||||
if (!$token || !hash_equals($csrfsection->get('token'), $token)) {
|
||||
http_response_code(401);
|
||||
die(json_encode(['error' => 'API token is incorrect or missing']));
|
||||
}
|
||||
|
||||
$unsplashdata = load_cache_unsplash_data();
|
||||
shell_exec('/usr/bin/nohup /usr/bin/php -f unsplashdata.php >/dev/null 2>&1 &');
|
||||
echo json_encode($unsplashdata);
|
||||
|
||||
function load_cache_unsplash_data() {
|
||||
global $cache, $config;
|
||||
return $cache->load(cachename: 'unsplash', callback: function() use ($config) {
|
||||
Crew\Unsplash\HttpClient::init([
|
||||
'utmSource' => 'jump_startpage',
|
||||
'applicationId' => $config->get('unsplashapikey'),
|
||||
]);
|
||||
// Or apply some optional filters by passing a key value array of filters
|
||||
$filters = [
|
||||
'collections' => $config->get('unsplashcollections', false),
|
||||
];
|
||||
$photo = Crew\Unsplash\Photo::random($filters);
|
||||
// Download the image data from Unsplash.
|
||||
$ch = curl_init();
|
||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
|
||||
curl_setopt($ch, CURLOPT_URL, $photo->urls['raw'] . '&auto=compress&w=1920');
|
||||
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
|
||||
curl_setopt($ch, CURLOPT_FAILONERROR, true);
|
||||
$response = curl_exec($ch);
|
||||
$description = $photo->description?$photo->description:'Photo';
|
||||
$unsplashdata = new stdClass();
|
||||
$unsplashdata->cssimg = $photo->urls['raw'] . '&auto=compress&w=1920';
|
||||
$unsplashdata->attribution = '<a target="_blank" rel="noopener" href="'.$photo->links['html'].'">'.$description.'</a> by <a target="_blank" rel="noopener" href="'.$photo->user['links']['html'].'">'.$photo->user['name'].'</a>';
|
||||
$unsplashdata->blurhash = $photo->blur_hash;
|
||||
$unsplashdata->imagedatauri = 'data: '.(new \finfo(FILEINFO_MIME_TYPE))->buffer($response).';base64,'.base64_encode($response);
|
||||
return $unsplashdata;
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user