Created
June 12, 2019 12:03
-
-
Save texnixe/cb58831f12273ee2c586df37184cdf2a to your computer and use it in GitHub Desktop.
Imagetag override test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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) | |
]); | |
} | |
] | |
] | |
]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment