asset_oembed_renderer.class.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. /**
  3. * Process html pages that support the oembed protocol.
  4. *
  5. * Note that here we rely on the discovery service. That is each page that contains in
  6. * its metadata the oembed request.
  7. *
  8. * @see http://oembed.com/
  9. *
  10. * @copyright (c) 2011 University of Geneva
  11. * @license GNU General Public License - http://www.gnu.org/copyleft/gpl.html
  12. * @author Laurent Opprecht
  13. *
  14. */
  15. class AssetOembedRenderer extends AssetRenderer
  16. {
  17. /**
  18. *
  19. * @param HttpResource $asset
  20. */
  21. public function render($asset)
  22. {
  23. $link = $asset->get_link('type', 'application/json+oembed');
  24. if (empty($link))
  25. {
  26. return false;
  27. }
  28. $width = (int) $asset->config('size');
  29. $width = (24 <= $width && $width <= 800) ? $width : 300;
  30. $href = $link['href'];
  31. $data = HttpResource::fetch_json("$href&maxwidth=$width"); //&maxheight=$height
  32. if (empty($data))
  33. {
  34. return false;
  35. }
  36. $data['title'] = isset($data['title']) ? $data['title'] : '';
  37. $data['width'] = isset($data['width']) ? intval($data['width']) : '';
  38. $data['height'] = isset($data['height']) ? intval($data['height']) : '';
  39. $type = $data['type'];
  40. $f = array($this, "render_$type");
  41. if (is_callable($f))
  42. {
  43. $result = call_user_func($f, $asset, $data);
  44. }
  45. else
  46. {
  47. $result = array();
  48. }
  49. $result[self::THUMBNAIL] = isset($data['thumbnail_url']) ? $data['thumbnail_url'] : '';
  50. $result[self::TITLE] = isset($data['title']) ? $data['title'] : '';
  51. return $result;
  52. }
  53. protected function render_photo($asset, $data)
  54. {
  55. if ($data['type'] != 'photo')
  56. {
  57. return array();
  58. }
  59. $result = array();
  60. $html = isset($data['html']) ? $data['html'] : '';
  61. if ($html)
  62. {
  63. $result[self::EMBED_SNIPPET] = '<div style="display:inline-block">' . $html . '</div>';
  64. return $result;
  65. }
  66. $title = $data['title'];
  67. $width = (int)$data['width'];
  68. $height = (int)$data['height'];
  69. // $ratio = $height / $width;
  70. // $height = $ratio * $width;
  71. $url = $data['url'];
  72. $embed = <<<EOT
  73. <div><a href="$url"><img src="{$url}" width="{$width}" height="{$height}" "alt="{$title}" title="{$title}"></a></div>
  74. EOT;
  75. $result[self::EMBED_SNIPPET] = $embed;
  76. return $result;
  77. }
  78. protected function render_video($asset, $data)
  79. {
  80. if ($data['type'] != 'video')
  81. {
  82. return array();
  83. }
  84. $result = array();
  85. $result[self::EMBED_SNIPPET] = '<div style="display:inline-block">' . $data['html'] . '</div>';
  86. return $result;
  87. }
  88. protected function render_rich($asset, $data)
  89. {
  90. if ($data['type'] != 'rich')
  91. {
  92. return array();
  93. }
  94. $result = array();
  95. $result[self::EMBED_SNIPPET] = '<div style="display:inline-block">' . $data['html'] . '</div>';
  96. return $result;
  97. }
  98. protected function render_link($asset, $data)
  99. {
  100. if ($data['type'] != 'link')
  101. {
  102. return array();
  103. }
  104. return array();
  105. }
  106. }