-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathresponsive-images.php
250 lines (215 loc) · 7.09 KB
/
responsive-images.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
<?php
// image tag
$kirby->set('tag', 'image', array(
'attr' => array(
'width',
'height',
'alt',
'text',
'title',
'class',
'imgclass',
'linkclass',
'caption',
'link',
'target',
'popup',
'rel',
'srcset',
'sizes'
),
'html' => function($tag) {
$url = $tag->attr('image');
$alt = $tag->attr('alt');
$title = $tag->attr('title');
$link = $tag->attr('link');
$caption = $tag->attr('caption');
$srcset = $tag->attr('srcset');
$sizes = $tag->attr('sizes');
$file = $tag->file($url);
// use the file url if available and otherwise the given url
$url = $file ? $file->url() : url($url);
// alt is just an alternative for text
if($text = $tag->attr('text')) $alt = $text;
// try to get the title from the image object and use it as alt text
if($file) {
if(empty($alt) and $file->alt() != '') {
$alt = $file->alt();
}
if(empty($title) and $file->title() != '') {
$title = $file->title();
}
}
if(empty($alt)) $alt = pathinfo($url, PATHINFO_FILENAME);
$sources = kirby_get_sources_array();
// link builder
$_link = function($image) use($tag, $url, $link, $file, $sources) {
if(empty($link)) return $image;
// 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 {
$href = $link;
}
return html::a(url($href), $image, array(
'rel' => $tag->attr('rel'),
'class' => $tag->attr('linkclass'),
'title' => $tag->attr('title'),
'target' => $tag->target()
));
};
// srcset builder
if($file && empty($srcset)) {
$srcset = kirby_get_srcset($file);
}
// 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) {
return html::img($url, array(
'width' => $tag->attr('width'),
'height' => $tag->attr('height'),
'class' => $class,
'title' => $title,
'alt' => $alt,
'srcset' => $srcset,
'sizes' => $sizes
));
};
if(kirby()->option('kirbytext.image.figure') or !empty($caption)) {
$image = $_link($_image($tag->attr('imgclass')));
$figure = new Brick('figure');
$figure->addClass($tag->attr('class'));
$figure->append($image);
if(!empty($caption)) {
$figure->append('<figcaption>' . html($caption) . '</figcaption>');
}
return $figure;
} else {
$class = trim($tag->attr('class') . ' ' . $tag->attr('imgclass'));
return $_link($_image($class));
}
}
));
/**
* Returns the srcset attribute value for a given Kirby file
* Generates thumbnails on the fly
*
* @param File $file
* @uses kirby_get_sources_array
* @uses thumb
*
* @return string
*/
function kirby_get_srcset( $file ) {
$srcset = $file->url() .' '. $file->width() .'w';
$sources_arr = kirby_get_sources_array( $file );
foreach ($sources_arr as $source) {
$thumb = thumb($file, $source);
$srcset .= ', '. $thumb->url() .' '. $thumb->width() .'w';
}
return $srcset;
}
/**
* Returns the image sources for a given Kirby file
*
* @return array
*/
function kirby_get_sources_array() {
$sources_arr = kirby()->option('responsiveimages.sources');
// set some arbitrary defaults
if (empty($sources_arr)) {
$sources_arr = array(
'small' => array('width' => 480),
'medium' => array('width' => 768),
'large' => array('width' => 1200),
);
}
return $sources_arr;
}
/**
* Returns the sizes attribute value for a given Kirby file
*
* @param File $file
* @param int $width Optional. Use when you want to force image to a certain width (retina/high-PPi usecase)
* @uses kirby_get_sizes_array()
*
* @return string
*/
function kirby_get_sizes( $file, $width = null, $imgclass = array() ) {
$sizes = '';
$sizes_arr = kirby_get_sizes_array( $file, $width );
foreach ( $sizes_arr as $key => $size ) {
// skip if the size should only be applied to a given class
if (is_string($key) && ! empty($imgclass) && ! in_array($key, $imgclass)) {
continue;
}
// Use 100vw as the size value unless something else is specified.
$size_value = ( $size['size_value'] ) ? $size['size_value'] : '100vw';
// If a media length is specified, build the media query.
if ( ! empty( $size['mq_value'] ) ) {
$media_length = $size['mq_value'];
// Use max-width as the media condition unless min-width is specified.
$media_condition = ( ! empty( $size['mq_name'] ) ) ? $size['mq_name'] : 'max-width';
// If a media_length was set, create the media query.
$media_query = '(' . $media_condition . ": " . $media_length . ') ';
} else {
// If not meda length was set, $media_query is blank.
$media_query = '';
}
// Add to the source size list string.
$sizes .= $media_query . $size_value . ', ';
}
// Remove the trailing comma and space from the end of the string.
$sizes = substr( $sizes, 0, -2 );
return $sizes;
}
/**
* Returns the sizes for a given Kirby file
*
* Uses 'responsiveimages.sizes' option to let site owners overwrite the defaults
*
* @param File $file
* @param int $width Optional. Use when you want to force image to a certain width (retina/high-PPi usecase)
*
* @return array
*/
function kirby_get_sizes_array( $file, $width = null ) {
// let users overwrite the sizes via config
$sizes_arr = kirby()->option('responsiveimages.sizes');
// let users overwrite the native image size via attribute
$img_width = ( empty($width) ? $file->width() : $width ) . 'px';
// default to the image width
if (empty($sizes_arr)) {
$sizes_arr = array(
array(
'size_value' => '100vw',
'mq_value' => $img_width,
'mq_name' => 'max-width'
),
array(
'size_value' => $img_width
),
);
} else {
$sizes_arr = array_map(function($value) use ($img_width) {
// allow config rules relative to native image size
return str_replace( '$img_width', $img_width, $value );
}, $sizes_arr);
}
return $sizes_arr;
}