Skip to content

Commit

Permalink
adds the option to set a default source
Browse files Browse the repository at this point in the history
  • Loading branch information
jancbeck committed Sep 14, 2016
1 parent 43e9c53 commit 09f99bd
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
14 changes: 13 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,21 @@ c::set('responsiveimages.sources', array(
));
```

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

### Fallbacks

Browsers that support responive images will ignore the `src` attribute of the image and pick what ever is best for their device from the `srcsset` attribute.
However, you might want to control what older browsers that don't support responsive images load. For that use case there is an option to set the default size based on the key name of the size (explained in the previous section).

```
<?php
c::set('responsiveimages.defaultsource', 'small');
```

This will show older browsers the "small" image size. If this option is not set, browsers will load the original image.

## Support

Please [open an issue](https://github.com/jancbeck/kirby-responsive-images/issues/new) for support.
Expand Down
15 changes: 10 additions & 5 deletions responsive-images.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,13 @@

if(empty($alt)) $alt = pathinfo($url, PATHINFO_FILENAME);

$sources = kirby_get_sources_array();

// link builder
$_link = function($image) use($tag, $url, $link, $file) {
$_link = function($image) use($tag, $url, $link, $file, $sources) {

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

$sources = kirby_get_sources_array();

// build the href for the link
if($link == 'self') {
$href = $url;
Expand All @@ -80,16 +80,21 @@

};

//srcset builder
// srcset builder
if($file && empty($srcset)) {
$srcset = kirby_get_srcset($file);
}

//sizes builder
// sizes builder
if($file && empty($sizes)) {
$classes = ( ! empty( $tag->attr('imgclass'))) ? explode( ' ', $tag->attr('imgclass')) : '';
$sizes = kirby_get_sizes($file, $tag->attr('width'), $classes);
}
// allows src attribute to be overwritten
$defaultsource = kirby()->option('responsiveimages.defaultsource');
if ( isset($sources[$defaultsource])) {
$url = thumb($file, $sources[$defaultsource])->url();
}

// image builder
$_image = function($class) use($tag, $url, $alt, $title, $srcset, $sizes) {
Expand Down

0 comments on commit 09f99bd

Please sign in to comment.