|
- <?php
-
- namespace Modules;
-
- class CEimage
- {
- public $r;
- public $b;
- public $structs;
- public $content;
- public $domains;
-
- function __construct($request,$body=[],$structs=false,$content=false,$domains=false) {
- $this->r = $request;
- $this->b = $body;
- $this->structs = &$structs;
- $this->content = &$content;
- $this->domains = &$domains;
- }
-
- function is_width($string) {
- $pattern="/^[0-9]+%$/";
- if (preg_match($pattern,$string)) {
- return true;
- } else {
- return false;
- }
- }
- function index() {
- $md = new \freaParsedown();
- $request = $this->r;
- $body = $this->b;
- $key=$request[1];
- $image="";
- $new="";
-
- // see if we can find image
- if ( is_file($request[1])) {
- $image= $request[1];
- } else if ($this->structs) {
- foreach($this->structs as $domain=>$destination) {
- if(array_key_exists($key, $this->structs[$domain]['pic'])) {
- $image = $this->structs[$domain]['pic'][$key];
- unset($this->structs[$domain]['pic'][$key]);
- unset($this->content[$this->domains[$domain]][$key]);
- break;
- }
- }
- }
-
- // image positioning
- if( in_array($request[2],array('left','right','full','auto'))) {
- $class = $request[2];
- } else {
- $class = 'full';
- }
- if (isset($request[3])) {
- $width=self::is_width($request[3]) ? $request[3] : false;
- }
-
- if ($image) {
-
-
- $gallery = "gallery";
- // image orientation?
- $img = new \Image($image);
- // $ratio = "landscape";
- $ratio = ($img->width() >= $img->height())
- ? "landscape"
- : "portrait"
- ;
- unset($img);
- $cached = new CachedImage($image);
-
- // identify if caption contains a copyright note
- // if so, indicate with a class
- foreach ($body as $k=>$line) {
- if (strpos($line,"©") !== FALSE
- || strpos($line,"©") !== FALSE) {
- $body[$k] = sprintf("<span class=\"copyright\">%s</span>",$line);
- }
- }
-
- $new=sprintf("<div class='content_element_image %s'>"
- ."<div class=\"media %s\" %s><a href=\"%s\" class=\"$gallery\"><img src=\"%s\" alt=\"user supplied image\" /></a></div>"
- ."<img src=\"%s\" style=\"display:none;\" alt=\"user supplied image\" />"
- ."<div class=\"caption\">%s</div>"
- ."</div>",
- $class,
- $ratio,
- (isset($width) ? "style=\"flex-basis:$width;\"" : ''),
- $cached->get_src(1600),
- $cached->get_src(1600),
- $cached->get_src(1600),
- $md->text(implode("\n",$body))
- );
- unset($cached);
- } else {
- // image not found
- $new=sprintf("<div class='content_element_image %s'>\n"
- ."<div class=\"media %s\">\n"
- ."<div class=\"caption\">\n%s\n</div>\n"
- ."</div>\n",
- $class,
- "landscape",
- $md->text(implode("\n",$body))
- );
- }
-
- return $new;
- }
- }
|