Skip to content

Instantly share code, notes, and snippets.

@JimmyRittenborg
Created November 10, 2014 04:15
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JimmyRittenborg/3a56f11371c486e64740 to your computer and use it in GitHub Desktop.
Save JimmyRittenborg/3a56f11371c486e64740 to your computer and use it in GitHub Desktop.
Force a thumbnail for the kirbytext image tag. Place this in /site/tags/image.php
<?php
// This file should reflect the image tag functionality found in /kirby/extensions/tags.php
// image tag
kirbytext::$tags['image'] = array(
'attr' => array(
'width',
'height',
'alt',
'text',
'title',
'class',
'imgclass',
'linkclass',
'caption',
'link',
'target',
'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);
// 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', '1024');
if( $tag->attr('width') and $tag->attr('height') ) {
$url = thumb($file, array('width' => $tag->attr('width'), 'height' => $tag->attr('height'), 'crop' => true))->url();
} else if(!empty($tag->attr('width')) or !empty($tag->attr('height'))){
$url = thumb($file, array('width' => $tag->attr('width'), 'height' => $tag->attr('height')))->url();
} else if($file->width() > $maxImageSize or $file->height() > $maxImageSize){
$fit_to = $file->isLandscape() ? 'width' : 'height';
$url = thumb($file, array($fit_to => $maxImageSize))->url();
}
}
if(empty($alt)) $alt = pathinfo($url, PATHINFO_FILENAME);
$image = html::img($url, array(
'width' => $tag->attr('width'),
'height' => $tag->attr('height'),
'class' => $tag->attr('imgclass'),
'title' => html($title),
'alt' => html($alt)
));
if($tag->attr('link')) {
// build the href for the link
if($link == 'self') {
$href = $url;
} else if($file and $link == $file->filename()) {
$href = $file->url();
} else {
$href = $link;
}
$image = html::a(url($href), $image, array(
'rel' => $tag->attr('rel'),
'class' => $tag->attr('linkclass'),
'title' => html($tag->attr('title')),
'target' => $tag->target()
));
}
$figure = new Brick('figure');
$figure->addClass($tag->attr('class'));
$figure->append($image);
if(!empty($caption)) {
$figure->append('<figcaption>' . html($caption) . '</figcaption>');
}
return $figure;
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment