UnsplashBridge.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. class UnsplashBridge extends BridgeAbstract {
  3. const MAINTAINER = 'nel50n';
  4. const NAME = 'Unsplash Bridge';
  5. const URI = 'http://unsplash.com/';
  6. const CACHE_TIMEOUT = 43200; // 12h
  7. const DESCRIPTION = 'Returns the latests photos from Unsplash';
  8. const PARAMETERS = array( array(
  9. 'm' => array(
  10. 'name' => 'Max number of photos',
  11. 'type' => 'number',
  12. 'defaultValue' => 20
  13. ),
  14. 'w' => array(
  15. 'name' => 'Width',
  16. 'exampleValue' => '1920, 1680, …',
  17. 'defaultValue' => '1920'
  18. ),
  19. 'q' => array(
  20. 'name' => 'JPEG quality',
  21. 'type' => 'number',
  22. 'defaultValue' => 75
  23. )
  24. ));
  25. public function collectData(){
  26. $width = $this->getInput('w');
  27. $num = 0;
  28. $max = $this->getInput('m');
  29. $quality = $this->getInput('q');
  30. $lastpage = 1;
  31. for($page = 1; $page <= $lastpage; $page++) {
  32. $link = self::URI . '/grid?page=' . $page;
  33. $html = getSimpleHTMLDOM($link)
  34. or returnServerError('No results for this query.');
  35. if($page === 1) {
  36. preg_match(
  37. '/=(\d+)$/',
  38. $html->find('.pagination > a[!class]', -1)->href,
  39. $matches
  40. );
  41. $lastpage = min($matches[1], ceil($max / 40));
  42. }
  43. foreach($html->find('.photo') as $element) {
  44. $thumbnail = $element->find('img', 0);
  45. $thumbnail->src = str_replace('https://', 'http://', $thumbnail->src);
  46. $item = array();
  47. $item['uri'] = str_replace(
  48. array('q=75', 'w=400'),
  49. array("q=$quality", "w=$width"),
  50. $thumbnail->src).'.jpg'; // '.jpg' only for format hint
  51. $item['timestamp'] = time();
  52. $item['title'] = $thumbnail->alt;
  53. $item['content'] = $item['title']
  54. . '<br><a href="'
  55. . $item['uri']
  56. . '"><img src="'
  57. . $thumbnail->src
  58. . '" /></a>';
  59. $this->items[] = $item;
  60. $num++;
  61. if ($num >= $max)
  62. break 2;
  63. }
  64. }
  65. }
  66. }