Skip to content

Commit a1b4b6a

Browse files
committed
Merge branch 'develop'
2 parents 1340c12 + 94b503e commit a1b4b6a

File tree

6 files changed

+76
-19
lines changed

6 files changed

+76
-19
lines changed

README.md

+32-10
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ Visual image clipping / cropping.
1010
- [Consider a donation](#Consider-a-donation)
1111
- [Panel Usage](#Panel-usage)
1212
- [Frontend Usage](#Frontend-usage)
13+
- [Single Image](#single-image)
14+
- [Multiple Images](#multiple-images)
15+
- [File Methods](#file-methods)
16+
- [Responsive: srcset](#file-srcset)
1317
- [License](#License)
1418

1519
## Installation
@@ -93,30 +97,38 @@ __Note__: This is only about thumbnail quality in the panel. You don't need to m
9397
## Frontend Usage
9498
How to fetch the images defined in a `image-clip` field.
9599
Read about the `clip()` method a bit further down.
96-
#### Multiple Images
100+
101+
### Single Image
97102
```php
98-
foreach($page->myimages()->toImages() as $image) {
103+
if ($image = $page->myimages()->toImage()) {
99104
echo $image->clip(500);
100105
}
101106
```
102-
- **Important:** ~~toFiles~~ does not work, use `toImages()` instead.
103-
- `toImages()` returns a Files Collection with all it's functions.
107+
- **Important:** ~~toFile~~ does not work, use `toImage()` instead.
108+
- `toImage()` returns a File Object with all it's functions.
104109

105-
#### Single Image
110+
### Multiple Images
106111
```php
107-
if ($image = $page->myimages()->toImage()) {
112+
foreach($page->myimages()->toImages() as $image) {
108113
echo $image->clip(500);
109114
}
110115
```
111-
- **Important:** ~~toFile~~ does not work, use `toImage()` instead.
112-
- `toImage()` returns a File Object with all it's functions.
116+
- **Important:** ~~toFiles~~ does not work, use `toImages()` instead.
117+
- `toImages()` returns a Files Collection with all it's functions.
118+
113119

120+
### File Methods
114121

115122
#### `$file->clip()`
116123
Clip and resize. Generates a Thumbnail of the clip area.
117124

118125
Adapter for `$file->thumb()`. Returns a FileVersion|File Object.
119126
```php
127+
if ($image = $page->myimages()->toImage()) {
128+
echo $image->clip(500);
129+
}
130+
```
131+
```php
120132
$file->clip(200, 300); // bestfit
121133
$file->clip(200); // width 200px
122134
$file->clip(null, 300); // height 300px
@@ -127,7 +139,17 @@ $file->clip(); // clip area without resizing
127139
- if `width` and `height` are both defined, the image will be resized with `bestfit`
128140

129141

130-
### Improved `$file->thumb()`
142+
#### `$file->srcset()`
143+
Use this method to generate the srcset attribute for responsive images.
144+
Read more about it's functionality in the [Kirby3 Docs](https://getkirby.com/docs/guide/templates/resize-images-on-the-fly#responsive-images)
145+
```html
146+
<?php if ($image = $page->myimages()->toImage()): ?>
147+
<img srcset="<?= $image->srcset([300, 800, 1024]) ?>">
148+
<?php endif; ?>
149+
```
150+
151+
152+
#### `$file->thumb()`
131153
The thumb method accepts now the option `clip` and can be used with any resizable image.
132154
```php
133155
$file->thumb([
@@ -146,7 +168,7 @@ $file->thumb([
146168

147169
Read more about the `thumb` method in the [Kirby3 Docs](https://getkirby.com/docs/reference/objects/file/thumb)
148170

149-
### Helper `$file->getClip()`
171+
#### `$file->getClip()`
150172
Returns the clip data.
151173

152174
Can be useful e.g with the `$file->thumb()` method.

classes/File.php

+40
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,44 @@ public function clip($width = null, $height = null) {
3333
'clip' => $this->getClip()
3434
]);
3535
}
36+
37+
/**
38+
* Create a srcset definition for the given sizes
39+
* Sizes can be defined as a simple array. They can
40+
* also be set up in the config with the thumbs.srcsets option.
41+
*
42+
* From: https://github.com/getkirby/kirby/blob/2b0bfd0b47a308e09117bac95ac674fdf50ce36c/src/Cms/FileModifications.php#L102
43+
*
44+
* @param array|string $sizes
45+
* @return string
46+
*/
47+
public function srcset($sizes = null): ?string
48+
{
49+
if (empty($sizes) === true) {
50+
$sizes = $this->kirby()->option('thumbs.srcsets.default', []);
51+
}
52+
53+
if (is_string($sizes) === true) {
54+
$sizes = $this->kirby()->option('thumbs.srcsets.' . $sizes, []);
55+
}
56+
57+
if (is_array($sizes) === false || empty($sizes) === true) {
58+
return null;
59+
}
60+
61+
$set = [];
62+
foreach ($sizes as $key => $value) {
63+
if (is_string($value) === true) {
64+
$size = $key;
65+
$attr = $value;
66+
} else {
67+
$size = $value;
68+
$attr = $value . 'w';
69+
}
70+
71+
$set[] = $this->clip($size)->url() . ' ' . $attr;
72+
}
73+
74+
return implode(', ', $set);
75+
}
3676
}

index.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/components/clipDialog.vue

+1
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@
7777
isOpen (newVal, oldVal) {
7878
if (newVal === true) {
7979
this.setDialogWidth();
80+
this.showSpinner();
8081
// dialog opened
8182
this.$nextTick(() => {
8283
let el = document.getElementById('croppr');

src/components/clipListItem.vue

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
</span>
2323
</k-link>
2424
<nav class="k-list-item-options">
25-
<k-clip-button v-if="resizable" @clicked="openClipDialog" />
25+
<k-clip-button v-if="resizable && !disabled" @clicked="openClipDialog" />
2626
<slot name="options">
2727
<k-button
2828
v-if="flag"

src/fields/imageClip.vue

+1-7
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
v-else
4646
:layout="layout"
4747
icon="image"
48-
@click="shouldOpen"
48+
v-on="{ click: !disabled ? open : null }"
4949
>
5050
{{ empty || $t('field.files.empty') }}
5151
</k-empty>
@@ -94,12 +94,6 @@ export default {
9494
},
9595
},
9696
methods: {
97-
// quickfix https://github.com/getkirby/kirby/issues/1752
98-
shouldOpen() {
99-
if (this.more && !this.disabled) {
100-
this.open();
101-
}
102-
},
10397
openClipDialog(id) {
10498
this.clip_image = this.value.find(item => item.id === id);
10599
this.$refs.clip.open();

0 commit comments

Comments
 (0)