1
0

UnsplashBridge.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <?php
  2. /**
  3. * UnsplashBridge
  4. * Returns the latests photos from http://unsplash.com
  5. *
  6. * @name Unsplash Bridge
  7. * @homepage http://unsplash.com/
  8. * @description Returns the latests photos from Unsplash
  9. * @maintainer nel50n
  10. * @update 2015-03-02
  11. * @use1(m="max number of photos",w="width (1920, 1680, ...)",q="jpeg quality (0..100)")
  12. */
  13. class UnsplashBridge extends BridgeAbstract {
  14. public function collectData(array $param){
  15. $html = '';
  16. $baseUri = 'http://unsplash.com';
  17. $width = $param['w'] ?: '1920'; // Default width
  18. $num = 0;
  19. $max = $param['m'] ?: 20;
  20. $quality = $param['q'] ?: 75;
  21. $lastpage = 1;
  22. for ($page = 1; $page <= $lastpage; $page++) {
  23. $link = $baseUri.'/grid?page='.$page;
  24. $html = file_get_html($link) or $this->returnError('No results for this query.', 404);
  25. if ($page === 1) {
  26. preg_match('/=(\d+)$/', $html->find('.pagination > a[!class]', -1)->href, $matches);
  27. $lastpage = min($matches[1], ceil($max/40));
  28. }
  29. foreach($html->find('.photo') as $element) {
  30. $thumbnail = $element->find('img', 0);
  31. $thumbnail->src = str_replace('https://', 'http://', $thumbnail->src);
  32. $item = new \Item();
  33. $item->uri = str_replace(array('q=75', 'w=400'),
  34. array("q=$quality", "w=$width"),
  35. $thumbnail->src).'.jpg'; // '.jpg' only for format hint
  36. $item->timestamp = time();
  37. $item->title = $thumbnail->alt;
  38. $item->thumbnailUri = $thumbnail->src;
  39. $item->content = $item->title.'<br><a href="'.$item->uri.'"><img src="'.$item->thumbnailUri.'" /></a>';
  40. $this->items[] = $item;
  41. $num++;
  42. if ($num >= $max)
  43. break 2;
  44. }
  45. }
  46. }
  47. public function getName(){
  48. return 'Unsplash';
  49. }
  50. public function getURI(){
  51. return 'http://unsplash.com';
  52. }
  53. public function getCacheDuration(){
  54. return 43200; // 12 hours
  55. }
  56. }