Advertisement
Guest User

Untitled

a guest
Apr 1st, 2017
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.56 KB | None | 0 0
  1. <?php
  2.  
  3. // image tag
  4. kirbytext::$tags['image'] = array(
  5.   'attr' => array(
  6.     'width',
  7.     'height',
  8.     'alt',
  9.     'text',
  10.     'title',
  11.     'class',
  12.     'imgclass',
  13.     'linkclass',
  14.     'caption',
  15.     'link',
  16.     'target',
  17.     'popup',
  18.     'rel'
  19.   ),
  20.   'html' => function($tag) {
  21.  
  22.     $url     = $tag->attr('image');
  23.     $alt     = $tag->attr('alt');
  24.     $title   = $tag->attr('title');
  25.     $link    = $tag->attr('link');
  26.     $caption = $tag->attr('caption');
  27.     $file    = $tag->file($url);
  28.  
  29.     // use the file url if available and otherwise the given url
  30.     $url = $file ? $file->url() : url($url);
  31.  
  32.     // alt is just an alternative for text
  33.     if($text = $tag->attr('text')) $alt = $text;
  34.  
  35.     // try to get the title from the image object and use it as alt text
  36.     if($file) {
  37.  
  38.       if(empty($alt) and $file->alt() != '') {
  39.         $alt = $file->alt();
  40.       }
  41.  
  42.       if(empty($title) and $file->title() != '') {
  43.         $title = $file->title();
  44.       }
  45.  
  46.     }
  47.  
  48.     // at least some accessibility for the image
  49.     if(empty($alt)) $alt = ' ';
  50.  
  51.     // link builder
  52.     $_link = function($image) use($tag, $url, $link, $file) {
  53.  
  54.       if(empty($link)) return $image;
  55.  
  56.       // build the href for the link
  57.       if($link == 'self') {
  58.         $href = $url;
  59.       } else if($file and $link == $file->filename()) {
  60.         $href = $file->url();
  61.       } else if($tag->file($link)) {
  62.         $href = $tag->file($link)->url();
  63.       } else {
  64.         $href = $link;
  65.       }
  66.  
  67.       return html::a(url($href), $image, array(
  68.         'rel'    => $tag->attr('rel'),
  69.         'class'  => $tag->attr('linkclass'),
  70.         'title'  => $tag->attr('title'),
  71.         'target' => $tag->target()
  72.       ));
  73.  
  74.     };
  75.  
  76.     // image builder
  77.     $_image = function($class) use($tag, $url, $alt, $title) {
  78.       return html::img($url, array(
  79.         'width'  => $tag->attr('width'),
  80.         'height' => $tag->attr('height'),
  81.         'class'  => $class,
  82.         'title'  => $title,
  83.         'alt'    => $alt
  84.       ));
  85.     };
  86.  
  87.     if(kirby()->option('kirbytext.image.figure') or !empty($caption)) {
  88.       $image  = $_link($_image($tag->attr('imgclass')));
  89.       $figure = new Brick('figure');
  90.       $figure->addClass($tag->attr('class'));
  91.       $figure->append($image);
  92.       if(!empty($caption)) {
  93.         $figure->append('<figcaption>' . html($caption) . '</figcaption>');
  94.       }
  95.       return $figure;
  96.     } else {
  97.       $class = trim($tag->attr('class') . ' ' . $tag->attr('imgclass'));
  98.       return $_link($_image($class));
  99.     }
  100.  
  101.   }
  102. );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement