Skip to content

Commit

Permalink
lets users link to source sizes
Browse files Browse the repository at this point in the history
  • Loading branch information
jancbeck committed Sep 14, 2016
1 parent ba516b9 commit 43e9c53
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ Make sure your `/thumbs` directory is present and writable and ImageMagick is av

Although the plugin works without configuration, these options are available and can be added to your `/site/config/config.php`:

It is also possible to link the image to one of the generated sizes using the keyword set via the `responsiveimages.sources` config. The following example will link to the smallest possible image size:

`(image:[email protected] link:small width:1244)`

### Sizes

To control the sizes attribute you can use [media queries](https://ericportis.com/posts/2014/srcset-sizes/).
Expand Down Expand Up @@ -55,7 +59,7 @@ c::set('responsiveimages.sources', array(
));
```

1. The key names are optional and have no technical implications.
1. The key names may be used to link the image (see *Usage* section).
2. Each array item takes the same arguments as Kirbys [thumb()](http://getkirby.com/docs/cheatsheet/helpers/thumb) function (`quality`, `blur`, `upscale` etc..).

## Support
Expand Down
12 changes: 7 additions & 5 deletions responsive-images.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,15 @@

if(empty($link)) return $image;

$sources = kirby_get_sources_array();

// build the href for the link
if($link == 'self') {
$href = $url;
} else if($file and $link == $file->filename()) {
$href = $file->url();
} else if(isset($sources[$link])) {
$href = thumb($file, $sources[$link])->url();
} else if($tag->file($link)) {
$href = $tag->file($link)->url();
} else {
Expand Down Expand Up @@ -122,14 +126,14 @@
* Generates thumbnails on the fly
*
* @param File $file
* @uses kirby_get_srcset_array
* @uses kirby_get_sources_array
* @uses thumb
*
* @return string
*/
function kirby_get_srcset( $file ) {
$srcset = $file->url() .' '. $file->width() .'w';
$sources_arr = kirby_get_srcset_array( $file );
$sources_arr = kirby_get_sources_array( $file );

foreach ($sources_arr as $source) {
$thumb = thumb($file, $source);
Expand All @@ -141,11 +145,9 @@ function kirby_get_srcset( $file ) {
/**
* Returns the image sources for a given Kirby file
*
* @param File $file
*
* @return array
*/
function kirby_get_srcset_array( $file ) {
function kirby_get_sources_array() {
$sources_arr = kirby()->option('responsiveimages.sources');

// set some arbitrary defaults
Expand Down

0 comments on commit 43e9c53

Please sign in to comment.