Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
This ia a rough example of making the image kirbytext tag support PhotoSwipe and automatically generate thumbnails. (Works - but not tested entirely through)
<?php
// Custom image kirbytag highjack
// This file should reflect the image tag functionality found in /kirby/extensions/tags.php
kirbytext::$tags['image'] = array(
'attr' => array(
'width',
'height',
'alt',
'text',
'title',
'class',
'imgclass',
'linkclass',
'caption',
'link',
'target',
'popup',
'rel'
),
'html' => function($tag) {
$url = $tag->attr('image');
$alt = $tag->attr('alt');
$title = $tag->attr('title');
$link = $tag->attr('link');
$caption = $tag->attr('caption');
$file = $tag->file($url);
$autoLightbox = empty($alt) ? true : false;
$thumb = false;
$url_2x = false;
// use the file url if available and otherwise the given url
$url = $file ? $file->url() : url($url);
// alt is just an alternative for text
if($text = $tag->attr('text')) $alt = $text;
// try to get the title from the image object and use it as alt text
if($file) {
if(empty($alt) and $file->alt() != '') {
$alt = $file->alt();
}
if(empty($title) and $file->title() != '') {
$title = $file->title();
}
// force a thumbnail
$maxImageSize = c::get('maxImageSize', '800');
if( $tag->attr('width') and $tag->attr('height') ) {
// $thumb = thumb($file, array('width' => $tag->attr('width'), 'height' => $tag->attr('height'), 'crop' => true));
$thumb = $file->crop($tag->attr('width'), $tag->attr('height'));
$thumb_2x = $file->crop($tag->attr('width') *2, $tag->attr('height') *2);
$url = $thumb->url();
$url_2x = $thumb_2x->url();
} else if(!empty($tag->attr('width')) or !empty($tag->attr('height'))){
// $thumb = thumb($file, array('width' => $tag->attr('width'), 'height' => $tag->attr('height')));
$thumb = $file->resize($tag->attr('width'), $tag->attr('height'));
$thumb_2x = $file->resize($tag->attr('width') *2, $tag->attr('height') *2);
$url = $thumb->url();
$url_2x = $thumb_2x->url();
} else if($file->width() > $maxImageSize or $file->height() > $maxImageSize){
$fit_to = $file->isLandscape() ? 'width' : 'height';
$thumb = thumb($file, array($fit_to => $maxImageSize));
$thumb_2x = thumb($file, array($fit_to => $maxImageSize *2));
$url = $thumb->url();
$url_2x = $thumb_2x->url();
}
}
if(empty($alt)) $alt = pathinfo($url, PATHINFO_FILENAME);
// link builder
$_link = function($image) use($tag, $url, $link, $file, $thumb) {
// if(empty($link)) return $image;
if(empty($link) and $file){
$link = $url;
} else {
return $image;
};
// build the href for the link
if($link == 'self') {
$href = $url;
} else if($file and $link == $file->filename()) {
$href = $file->url();
} else if($tag->file($link)) {
$href = $tag->file($link)->url();
} else {
$href = $link;
}
if($link == $url and $file){
// return html::a(url($href), $image, array(
return html::a($file->url(), $image, array(
'data-size' => $file->width() . "x" . $file->height(),
'data-share-src' => url($file->url()),
'rel' => $tag->attr('rel'),
'class' => $tag->attr('linkclass'),
'title' => $tag->attr('title'),
'target' => $tag->target()
));
} else {
return html::a(url($href), $image, array(
'rel' => $tag->attr('rel'),
'class' => $tag->attr('linkclass'),
'title' => $tag->attr('title'),
'target' => $tag->target()
));
}
};
// image builder
$_image = function($class) use($tag, $url, $alt, $title, $url_2x) {
if($url_2x){
return html::img($url, array(
'width' => $tag->attr('width'),
'height' => $tag->attr('height'),
'class' => $class,
'title' => $title,
'alt' => $alt,
'srcset' => $url_2x . ' 2x'
));
} else {
return html::img($url, array(
'width' => $tag->attr('width'),
'height' => $tag->attr('height'),
'class' => $class,
'title' => $title,
'alt' => $alt
));
}
};
if(kirby()->option('kirbytext.image.figure') or !empty($caption)) {
$image = $_link($_image($tag->attr('imgclass')));
$figure = new Brick('figure');
$figure->addClass($tag->attr('class'));
if($file) {
$figure->addClass('zoom');
}
$figure->append($image);
if(!empty($caption)) {
$figure->append('<figcaption>' . html($caption) . '</figcaption>');
}
return $figure;
} else {
$class = trim($tag->attr('class') . ' ' . $tag->attr('imgclass'));
return $_link($_image($class));
}
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment