2015-03-03 00:42:54 +03:00
|
|
|
<?php
|
|
|
|
|
2022-04-05 16:00:10 +03:00
|
|
|
class UnsplashBridge extends BridgeAbstract
|
|
|
|
{
|
|
|
|
const MAINTAINER = 'nel50n, langfingaz';
|
2017-02-11 18:16:56 +03:00
|
|
|
const NAME = 'Unsplash Bridge';
|
2019-07-16 17:50:14 +03:00
|
|
|
const URI = 'https://unsplash.com/';
|
2016-09-25 18:04:28 +03:00
|
|
|
const CACHE_TIMEOUT = 43200; // 12h
|
2022-04-05 16:00:10 +03:00
|
|
|
const DESCRIPTION = 'Returns the latest photos from Unsplash';
|
2015-11-05 18:50:18 +03:00
|
|
|
|
2022-04-05 16:00:10 +03:00
|
|
|
const PARAMETERS = array(array(
|
|
|
|
'u' => array(
|
|
|
|
'name' => 'Filter by username (optional)',
|
|
|
|
'type' => 'text',
|
|
|
|
'defaultValue' => 'unsplash'
|
|
|
|
),
|
2017-02-11 18:16:56 +03:00
|
|
|
'm' => array(
|
|
|
|
'name' => 'Max number of photos',
|
|
|
|
'type' => 'number',
|
2022-04-05 16:00:10 +03:00
|
|
|
'defaultValue' => 20,
|
|
|
|
'required' => true
|
|
|
|
),
|
|
|
|
'prev_q' => array(
|
|
|
|
'name' => 'Preview quality',
|
|
|
|
'type' => 'list',
|
|
|
|
'values' => array(
|
|
|
|
'full' => 'full',
|
|
|
|
'regular' => 'regular',
|
|
|
|
'small' => 'small',
|
|
|
|
'thumb' => 'thumb',
|
|
|
|
),
|
|
|
|
'defaultValue' => 'regular'
|
2017-02-11 18:16:56 +03:00
|
|
|
),
|
|
|
|
'w' => array(
|
2022-04-05 16:00:10 +03:00
|
|
|
'name' => 'Max download width (optional)',
|
|
|
|
'exampleValue' => 1920,
|
|
|
|
'type' => 'number',
|
|
|
|
'defaultValue' => 1920,
|
2017-02-11 18:16:56 +03:00
|
|
|
),
|
2022-04-05 16:00:10 +03:00
|
|
|
'jpg_q' => array(
|
|
|
|
'name' => 'Max JPEG quality (optional)',
|
|
|
|
'exampleValue' => 75,
|
2017-02-11 18:16:56 +03:00
|
|
|
'type' => 'number',
|
2022-04-05 16:00:10 +03:00
|
|
|
'defaultValue' => 75,
|
2017-02-11 18:16:56 +03:00
|
|
|
)
|
|
|
|
));
|
2015-11-05 18:50:18 +03:00
|
|
|
|
2022-04-05 16:00:10 +03:00
|
|
|
public function collectData()
|
|
|
|
{
|
|
|
|
$filteredUser = $this->getInput('u');
|
2017-02-11 18:16:56 +03:00
|
|
|
$width = $this->getInput('w');
|
|
|
|
$max = $this->getInput('m');
|
2022-04-05 16:00:10 +03:00
|
|
|
$previewQuality = $this->getInput('prev_q');
|
|
|
|
$jpgQuality = $this->getInput('jpg_q');
|
|
|
|
|
|
|
|
$url = 'https://unsplash.com/napi';
|
|
|
|
if (strlen($filteredUser) > 0) $url .= '/users/' . $filteredUser;
|
|
|
|
$url .= '/photos?page=1&per_page=' . $max;
|
|
|
|
$api_response = getContents($url);
|
2015-03-03 00:42:54 +03:00
|
|
|
|
2019-07-16 17:50:14 +03:00
|
|
|
$json = json_decode($api_response, true);
|
2015-03-03 00:42:54 +03:00
|
|
|
|
2019-07-16 17:50:14 +03:00
|
|
|
foreach ($json as $json_item) {
|
|
|
|
$item = array();
|
2015-03-03 00:42:54 +03:00
|
|
|
|
2019-07-16 17:50:14 +03:00
|
|
|
// Get image URI
|
2022-04-05 16:00:10 +03:00
|
|
|
$uri = $json_item['urls']['raw'] . '&fm=jpg';
|
|
|
|
if ($jpgQuality > 0) $uri .= '&q=' . $jpgQuality;
|
|
|
|
if ($width > 0) $uri .= '&w=' . $width . '&fit=max';
|
|
|
|
$uri .= '.jpg'; // only for format hint
|
2019-07-16 17:50:14 +03:00
|
|
|
$item['uri'] = $uri;
|
2015-03-03 00:42:54 +03:00
|
|
|
|
2019-07-16 17:50:14 +03:00
|
|
|
// Get title from description
|
2022-04-05 16:00:10 +03:00
|
|
|
if (is_null($json_item['description'])) {
|
|
|
|
$item['title'] = 'Unsplash picture from ' . $json_item['user']['name'];
|
2019-07-16 17:50:14 +03:00
|
|
|
} else {
|
2022-04-05 16:00:10 +03:00
|
|
|
$item['title'] = $json_item['description'];
|
2019-07-16 17:50:14 +03:00
|
|
|
}
|
2017-02-11 18:16:56 +03:00
|
|
|
|
2022-04-05 16:00:10 +03:00
|
|
|
$item['timestamp'] = $json_item['created_at'];
|
|
|
|
$content = 'User: <a href="'
|
|
|
|
. $json_item['user']['links']['html']
|
|
|
|
. '">@'
|
|
|
|
. $json_item['user']['username']
|
|
|
|
. '</a>';
|
|
|
|
if (isset($json_item['location']['name'])) {
|
|
|
|
$content .= ' | Location: ' . $json_item['location']['name'];
|
|
|
|
}
|
|
|
|
$content .= ' | Image on <a href="'
|
|
|
|
. $json_item['links']['html']
|
|
|
|
. '">Unsplash</a><br><a href="'
|
|
|
|
. $uri
|
2017-02-11 18:16:56 +03:00
|
|
|
. '"><img src="'
|
2022-04-05 16:00:10 +03:00
|
|
|
. $json_item['urls'][$previewQuality]
|
|
|
|
. '" alt="Image from '
|
|
|
|
. $filteredUser
|
2017-02-11 18:16:56 +03:00
|
|
|
. '" /></a>';
|
2022-04-05 16:00:10 +03:00
|
|
|
$item['content'] = $content;
|
2017-02-11 18:16:56 +03:00
|
|
|
|
2019-07-16 17:50:14 +03:00
|
|
|
$this->items[] = $item;
|
2017-02-11 18:16:56 +03:00
|
|
|
}
|
|
|
|
}
|
2022-04-05 16:00:10 +03:00
|
|
|
|
|
|
|
public function getName()
|
|
|
|
{
|
|
|
|
$filteredUser = $this->getInput('u');
|
|
|
|
if (strlen($filteredUser) > 0) {
|
|
|
|
return $filteredUser . ' - ' . self::NAME;
|
|
|
|
} else {
|
|
|
|
return self::NAME;
|
|
|
|
}
|
|
|
|
}
|
2015-03-03 00:42:54 +03:00
|
|
|
}
|