Skip to content

Instantly share code, notes, and snippets.

@texnixe
Created June 12, 2019 12:03

Revisions

  1. texnixe created this gist Jun 12, 2019.
    66 changes: 66 additions & 0 deletions index.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,66 @@
    <?php

    Kirby::plugin('texnixe/imagetagtest', [
    'tags' => [
    'image' => [
    'attr' => [
    'alt',
    'caption',
    'class',
    'height',
    'imgclass',
    'link',
    'linkclass',
    'rel',
    'size',
    'target',
    'text',
    'title',
    'width'
    ],
    'html' => function ($tag) {
    if ($tag->file = $tag->file($tag->value)) {
    $tag->src = $tag->file->url();
    $tag->alt = $tag->alt ?? $tag->file->alt()->or(' ')->value();
    $tag->title = $tag->title ?? $tag->file->title()->value();
    $tag->caption = $tag->caption ?? $tag->file->caption()->value();
    $tag->ratio = rtrim(rtrim(number_format($tag->file->height() / $tag->file->width() * 100, 10, '.', ''), '0'), '.');
    } else {
    $tag->src = Url::to($tag->value);
    }
    $link = function ($img) {
    if (empty($tag->link) === true) {
    return $img;
    }
    return Html::a($tag->link === 'self' ? $tag->src : $tag->link, [$img], [
    'rel' => $tag->rel,
    'class' => trim('link-reset ' . $tag->linkclass),
    'target' => $tag->target
    ]);
    };
    $image = Html::img($tag->src, [
    'width' => $tag->width,
    'height' => $tag->height,
    'class' => $tag->imgclass,
    'title' => $tag->title,
    'alt' => $tag->alt ?? ' '
    ]);
    if ($tag->file) {
    $placeholder = Html::span('', [
    'class' => 'img-ratio-placeholder',
    'style' => "padding-top: {$tag->ratio}%;",
    ]);
    $wrapperWidth = $tag->width ?? $tag->file->width();
    $wrapperAttr = ['class' => 'img-wrap'];
    if (in_array($tag->size, ['banner', 'large']) === false) {
    $wrapperAttr['style'] = "max-width: {$wrapperWidth}px;";
    }
    $image = Html::span([$placeholder, $image], $wrapperAttr);
    }
    return Html::figure([ $link($image) ], $tag->caption, [
    'class' => trim($tag->class . ' figure -size:' . $tag->size)
    ]);
    }
    ]
    ]
    ]);