UnsplashBridge.php 2.2 KB

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