mostly filebased Content Presentation System
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

118 lines
3.7KB

  1. <?php
  2. namespace Modules;
  3. class CEimage
  4. {
  5. public $r;
  6. public $b;
  7. public $structs;
  8. public $content;
  9. public $domains;
  10. function __construct($request,$body=[],$structs=false,$content=false,$domains=false) {
  11. $this->r = $request;
  12. $this->b = $body;
  13. $this->structs = &$structs;
  14. $this->content = &$content;
  15. $this->domains = &$domains;
  16. }
  17. function is_width($string) {
  18. $pattern="/^[0-9]+%$/";
  19. if (preg_match($pattern,$string)) {
  20. return true;
  21. } else {
  22. return false;
  23. }
  24. }
  25. function index() {
  26. $md = new \freaParsedown();
  27. $request = $this->r;
  28. $body = $this->b;
  29. $key=$request[1];
  30. $image="";
  31. $new="";
  32. // see if we can find image
  33. if ( is_file($request[1])) {
  34. $image= $request[1];
  35. } else if ($this->structs) {
  36. foreach($this->structs as $domain=>$destination) {
  37. if(array_key_exists($key, $this->structs[$domain]['pic'])) {
  38. $image = $this->structs[$domain]['pic'][$key];
  39. unset($this->structs[$domain]['pic'][$key]);
  40. unset($this->content[$this->domains[$domain]][$key]);
  41. break;
  42. }
  43. }
  44. }
  45. // image positioning
  46. if (count($request) >= 3) {
  47. if( in_array($request[2],array('left','right','full','auto'))) {
  48. $class = $request[2];
  49. } else {
  50. $class = 'left';
  51. }
  52. } else {
  53. $class = 'left';
  54. }
  55. if (isset($request[3])) {
  56. $width=self::is_width($request[3]) ? $request[3] : null;
  57. }
  58. if ($image) {
  59. $gallery = "gallery";
  60. // image orientation?
  61. $img = new \Image($image);
  62. // $ratio = "landscape";
  63. $ratio = ($img->width() >= $img->height())
  64. ? "landscape"
  65. : "portrait"
  66. ;
  67. unset($img);
  68. $cached = new CachedImage($image);
  69. // identify if caption contains a copyright note
  70. // if so, indicate with a class
  71. foreach ($body as $k=>$line) {
  72. if (strpos($line,"©") !== FALSE
  73. || strpos($line,"&copy;") !== FALSE) {
  74. $body[$k] = sprintf("<span class=\"copyright\">%s</span>",$line);
  75. }
  76. }
  77. $new=sprintf("<div class='content_element_image %s'>"
  78. ."<div class=\"media %s\" %s><a href=\"%s\" class=\"$gallery\"><img src=\"%s\" alt=\"user supplied image\" /></a></div>"
  79. ."<img src=\"%s\" style=\"display:none;\" alt=\"user supplied image\" />"
  80. ."<div class=\"caption\">%s</div>"
  81. ."</div>",
  82. $class,
  83. $ratio,
  84. (isset($width) ? "style=\"flex-basis:$width;\"" : ''),
  85. $cached->get_src(1600),
  86. $cached->get_src(1600),
  87. $cached->get_src(1600),
  88. $md->text(implode("\n",$body))
  89. );
  90. unset($cached);
  91. } else {
  92. // image not found
  93. $new=sprintf("<div class='content_element_image %s'>\n"
  94. ."<div class=\"media %s\">\n"
  95. ."<div class=\"caption\">\n%s\n</div>\n"
  96. ."</div>\n",
  97. $class,
  98. "landscape",
  99. $md->text(implode("\n",$body))
  100. );
  101. }
  102. return $new;
  103. }
  104. }