UnsplashBridge.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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('/=(\d+)$/', $html->find('.pagination > a[!class]', -1)->href, $matches);
  37. $lastpage = min($matches[1], ceil($max/40));
  38. }
  39. foreach($html->find('.photo') as $element) {
  40. $thumbnail = $element->find('img', 0);
  41. $thumbnail->src = str_replace('https://', 'http://', $thumbnail->src);
  42. $item = array();
  43. $item['uri'] = str_replace(array('q=75', 'w=400'),
  44. array("q=$quality", "w=$width"),
  45. $thumbnail->src).'.jpg'; // '.jpg' only for format hint
  46. $item['timestamp'] = time();
  47. $item['title'] = $thumbnail->alt;
  48. $item['content'] = $item['title'].'<br><a href="'.$item['uri'].'"><img src="'.$thumbnail->src.'" /></a>';
  49. $this->items[] = $item;
  50. $num++;
  51. if ($num >= $max)
  52. break 2;
  53. }
  54. }
  55. }
  56. }