-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy paththumbext.php
151 lines (128 loc) · 4.38 KB
/
thumbext.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
<?php
/**
* ThumbExt
* Extended thumbnail function with auto generated thumbnails for html5 srcset.
* only the pixel density descriptor is supported!
* width descriptor should be another plugin, because those images mustn´t be auto generated
*
* @package Kirby Plugins
* @author Jannik Beyerstedt <[email protected]>
* @link http://jannikbeyerstedt.com
* @copyright Jannik Beyerstedt
* @license http://www.gnu.org/licenses/gpl-3.0.txt GPLv3 License
*/
function ThumbExt($obj, $options=array()) {
$thumb_tag = new thumb_srcset($obj, $options);
return $thumb_tag;
}
class thumb_srcset {
static public $defaults = array(
'srcset' => '1x, 2x',
'inline-size' => true,
'srcset-only' => false
);
public $sourcePath = null;
public $thumbs = array(); // stores all thumb objects
public $options = array();
/**
* Constructor
*
* @param mixed $source
* @param array $params
*/
public function __construct($source, $params = array()) {
$this->sourcePath = $source;
$this->options = array_merge(static::$defaults, $this->params($params));
// decompose srcset descriptors
$descriptors = array_map('trim',explode(',', $this->options['srcset']));
// handle default pixel density descriptor
$this->thumbs['1x'] = Thumb($this->sourcePath, $this->options);
foreach ($descriptors as $desc) {
$factor = floatval($desc);
// only handle non 1x pixel density descriptors, 1x is handeled above as default.
if ($factor != 1) {
// copy all options and adjust the width and height with the custom factor
$scaledOptions = $this->options;
if (isset($this->options['width'])) { // width must be set every time! no height alone!
$scaledOptions['width'] = $this->options['width']*$factor;
if (isset($this->options['height'])) {
$scaledOptions['height'] = $this->options['height']*$factor;
}
}else {
throw new Error('No width or height value set');
}
$this->thumbs[$desc] = Thumb($this->sourcePath, $scaledOptions);
}
}
}
/**
* Returns the source media object
*
* @return Media
*/
public function source() {
return $this->source;
}
/**
* Makes it possible to pass a string of params
* which is shorter and more convenient than
* passing a full array of keys and values:
* width:300|height:200|crop:true
*
* @param array $params
* @return array
*/
public function params($params) {
if(is_array($params)) return $params;
$result = array();
foreach(explode('|', $params) as $param) {
$pos = strpos($param, ':');
$result[trim(substr($param, 0, $pos))] = trim(substr($param, $pos+1));
}
return $result;
}
/**
* Generates and returns the full html tag for the thumbnail
*
* @param array $attr An optional array of attributes, which should be added to the image tag
* @return string
*/
public function tag($attr = array()) {
if ($this->options['inline-size'] && !$this->options['srcset-only']) { //if srcset-only, then no inline-size possible
return html::img($this->thumbs['1x']->url(), array_merge(array(
'alt' => isset($this->options['alt']) ? $this->options['alt'] : $this->sourcePath->name(),
'width' => $this->thumbs['1x']->width(),
'height' => $this->thumbs['1x']->height(),
'class' => isset($this->options['class']) ? $this->options['class'] : null,
'srcset' => $this->srcset_string(),
), $attr));
}else if($this->options['srcset-only']){
return $this->srcset_string();
}else{
return html::img($this->thumbs['1x']->url(), array_merge(array(
'alt' => isset($this->options['alt']) ? $this->options['alt'] : $this->sourcePath->name(),
'class' => isset($this->options['class']) ? $this->options['class'] : null,
'srcset' => $this->srcset_string(),
), $attr));
}
}
/**
* Makes it possible to echo the entire object
*/
public function __toString() {
return $this->tag();
}
/**
* assembles the srcset string
*/
private function srcset_string() {
foreach ($this->thumbs as $tag=>$thumb) {
if ($tag == "1x") { // first is different
$result = $thumb->url() . " " . $tag;
}else {
$result .= ", " . $thumb->url() . " " . $tag;
}
}
return $result;
}
};