diff --git a/README.md b/README.md index 4a605b5..c3d09d5 100644 --- a/README.md +++ b/README.md @@ -7,22 +7,10 @@ It adds the [srcset](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/i `composer require pascalmh/kirby-responsiveimage` -## Usage - -Instead of using `(image: filename.jpg)` like you are used to use `(responsiveimage: filename.jpg)`. - -This will output - -```xml - -``` - -The following attributes are available: `alt`, `width`, `height`, `class`, `link`, `popup`, `caption`. - ## Options You can use the following [Options](http://getkirby.com/docs/advanced/options) -### tag.responsiveimage.widths +### responsiveimage.widths Type: `array` Default value: `[2400, 2200, 2000, 1800, 1400, 1200, 1000, 800, 600, 400, 320]` diff --git a/kirby-responsiveimage.php b/kirby-responsiveimage.php index c3078da..5000168 100644 --- a/kirby-responsiveimage.php +++ b/kirby-responsiveimage.php @@ -1,24 +1,17 @@ set('tag', 'responsiveimage', [ - 'attr' => [ - 'alt', - 'width', - 'height', - 'class', - 'link', - 'popup', - 'caption', - ], - 'html' => function ($tag) { - $image = $tag->file($tag->attr('responsiveimage')); +$oldFunction = kirbytext::$tags['image']; +kirbytext::$tags['image'] = [ + 'attr' => $oldFunction['attr'], + 'html' => function ($tag) use ($oldFunction) { + $image = $tag->file($tag->attr('image')); if (!$image) { - return null; + return $result = call($oldFunction['html'], $tag); } $widths = c::get( - 'tag.responsiveimage.widths', + 'responsiveimage.widths', [ 2400, 2200, @@ -44,45 +37,9 @@ $srcset[] = $imageResized->url() . ' ' . $imageResized->width() . 'w'; } - $img = brick('img'); - $img->attr('src', array_values(array_slice($srcset, -1))[0]); - $img->attr('srcset', implode(', ', $srcset)); - $img->attr('alt', $tag->attr('alt')); - $img->attr('width', $tag->attr('width')); - $img->attr('height', $tag->attr('height')); - $img->attr('class', $tag->attr('class')); - $html = $img; + $result = call($oldFunction['html'], $tag); + $result = str_replace('toString()); - if ($href = $tag->attr('link')) { - $link = brick('a'); - $link->attr('href', url($href)); - - if ($tag->attr('popup') === 'yes') { - $link->attr('target', '_blank'); - } - - $link->append(function () use ($html) { - return $html; - }); - - $html = $link; - } - - if (c::get('kirbytext.image.figure', true)) { - $figure = brick('figure'); - $figure->append(function () use ($html) { - return $html; - }); - - if ($caption = $tag->attr('caption')) { - $figure->append(function() use ($caption) { - return brick('figcaption', $caption); - }); - } - - $html = $figure; - } - - return $html; + return $result; } -]); +];