Skip to content

Instantly share code, notes, and snippets.

@bastianallgeier
Created July 3, 2012 12:48
Show Gist options
  • Save bastianallgeier/3039536 to your computer and use it in GitHub Desktop.
Save bastianallgeier/3039536 to your computer and use it in GitHub Desktop.

Thumb extension for Kirbytext

This extension for Kirbytext will make it possible to use

(thumb: myimage.png width: 200 height: 100)

in your content files.

Installation

Put kirbytext.extended.php in your site/plugins folder Make sure to install the thumb plugin as well: https://github.com/bastianallgeier/kirbycms-extensions/tree/master/plugins/thumb

To make sure the thumbs plugin will work, you must create a thumbs folder in your main folder of your site (next to Kirby's index.php) and make sure that it is writable by PHP.

Usage

In your content text files you can now use the new thumb tag:

(thumb: myimage.jpg)

myimage.jpg must be located in the same content folder as the text file in which you use the tag. But of course you are free to name your files like you want :)

Parameters

You can pass all parameters, which are also available for the thumb plugin:

(thumb: myimage.jpg width: 200 height: 200 crop: true quality: 100)

Feel free to change the $defaults array in kirbytext.extended.php to add the values which fit best to your project.


// customize this:

$defaults = array(
  'width'   => 200,
  'height'  => 200,
  'crop'    => false,
  'quality' => 100
);

Those defaults will make it possible to use the thumb tag without specifying all the attributes.

<?php
class kirbytextExtended extends kirbytext {
function __construct($text, $markdown=true) {
parent::__construct($text, $markdown);
$this->addTags('thumb');
$this->addAttributes('crop', 'quality');
}
function thumb($params) {
global $site;
$defaults = array(
'width' => 200,
'height' => 200,
'crop' => false,
'quality' => 100
);
$options = array_merge($defaults, $params);
$thumb = $params['thumb'];
$page = ($this->obj) ? $this->obj : $site->pages()->active();
$image = $page->images()->find($thumb);
if(!$image) return false;
return thumb($image, array(
'width' => $options['width'],
'height' => $options['height'],
'crop' => $options['crop'],
'quality' => $options['quality']
));
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment