UnsplashBridge.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. <?php
  2. class UnsplashBridge extends BridgeAbstract {
  3. public function loadMetadatas() {
  4. $this->maintainer = "nel50n";
  5. $this->name = "Unsplash Bridge";
  6. $this->uri = "http://unsplash.com/";
  7. $this->description = "Returns the latests photos from Unsplash";
  8. $this->parameters[] = array(
  9. 'm'=>array(
  10. 'name'=>'Max number of photos',
  11. 'type'=>'number'
  12. ),
  13. 'w'=>array(
  14. 'name'=>'Width',
  15. 'exampleValue'=>'1920, 1680, …'
  16. ),
  17. 'q'=>array(
  18. 'name'=>'JPEG quality',
  19. 'type'=>'number'
  20. )
  21. );
  22. }
  23. public function collectData(array $param){
  24. $html = '';
  25. $baseUri = 'http://unsplash.com';
  26. $width = $param['w'] ?: '1920'; // Default width
  27. $num = 0;
  28. $max = $param['m'] ?: 20;
  29. $quality = $param['q'] ?: 75;
  30. $lastpage = 1;
  31. for ($page = 1; $page <= $lastpage; $page++) {
  32. $link = $baseUri.'/grid?page='.$page;
  33. $html = $this->getSimpleHTMLDOM($link) 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. }