1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Timber; |
4
|
|
|
|
5
|
|
|
use Timber\CoreInterface; |
6
|
|
|
use Timber\Helper; |
7
|
|
|
use Timber\Post; |
8
|
|
|
use Timber\URLHelper; |
9
|
|
|
use Timber\PathHelper; |
10
|
|
|
|
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* If TimberPost is the class you're going to spend the most time, Timber\Image is the class you're going to have the most fun with. |
14
|
|
|
* @example |
15
|
|
|
* ```php |
16
|
|
|
* $context = Timber::context(); |
17
|
|
|
* $post = new Timber\Post(); |
18
|
|
|
* $context['post'] = $post; |
19
|
|
|
* |
20
|
|
|
* // lets say you have an alternate large 'cover image' for your post stored in a custom field which returns an image ID |
21
|
|
|
* $cover_image_id = $post->cover_image; |
22
|
|
|
* $context['cover_image'] = new Timber\Image($cover_image_id); |
23
|
|
|
* Timber::render('single.twig', $context); |
24
|
|
|
* ``` |
25
|
|
|
* |
26
|
|
|
* ```twig |
27
|
|
|
* <article> |
28
|
|
|
* <img src="{{cover_image.src}}" class="cover-image" /> |
29
|
|
|
* <h1 class="headline">{{post.title}}</h1> |
30
|
|
|
* <div class="body"> |
31
|
|
|
* {{post.content}} |
32
|
|
|
* </div> |
33
|
|
|
* |
34
|
|
|
* <img src="{{ Image(post.custom_field_with_image_id).src }}" alt="Another way to initialize images as TimberImages, but within Twig" /> |
35
|
|
|
* </article> |
36
|
|
|
* ``` |
37
|
|
|
* |
38
|
|
|
* ```html |
39
|
|
|
* <article> |
40
|
|
|
* <img src="http://example.org/wp-content/uploads/2015/06/nevermind.jpg" class="cover-image" /> |
41
|
|
|
* <h1 class="headline">Now you've done it!</h1> |
42
|
|
|
* <div class="body"> |
43
|
|
|
* Whatever whatever |
44
|
|
|
* </div> |
45
|
|
|
* <img src="http://example.org/wp-content/uploads/2015/06/kurt.jpg" alt="Another way to initialize images as TimberImages, but within Twig" /> |
46
|
|
|
* </article> |
47
|
|
|
* ``` |
48
|
|
|
*/ |
49
|
|
|
class Image extends Post implements CoreInterface { |
50
|
|
|
|
51
|
|
|
protected $_can_edit; |
52
|
|
|
protected $_dimensions; |
53
|
|
|
public $abs_url; |
54
|
|
|
/** |
55
|
|
|
* @var string $object_type what does this class represent in WordPress terms? |
56
|
|
|
*/ |
57
|
|
|
public $object_type = 'image'; |
58
|
|
|
/** |
59
|
|
|
* @var string $representation what does this class represent in WordPress terms? |
60
|
|
|
*/ |
61
|
|
|
public static $representation = 'image'; |
62
|
|
|
/** |
63
|
|
|
* @var array of supported relative file types |
64
|
|
|
*/ |
65
|
|
|
private $file_types = array('jpg', 'jpeg', 'png', 'svg', 'bmp', 'ico', 'gif', 'tiff', 'pdf'); |
66
|
|
|
/** |
67
|
|
|
* @api |
68
|
|
|
* @var string $file_loc the location of the image file in the filesystem (ex: `/var/www/htdocs/wp-content/uploads/2015/08/my-pic.jpg`) |
69
|
|
|
*/ |
70
|
|
|
public $file_loc; |
71
|
|
|
public $file; |
72
|
|
|
/** |
73
|
|
|
* @api |
74
|
|
|
* @var integer the ID of the image (which is a WP_Post) |
75
|
|
|
*/ |
76
|
|
|
public $id; |
77
|
|
|
public $sizes = array(); |
78
|
|
|
/** |
79
|
|
|
* @api |
80
|
|
|
* @var string $caption the string stored in the WordPress database |
81
|
|
|
*/ |
82
|
|
|
public $caption; |
83
|
|
|
/** |
84
|
|
|
* @var $_wp_attached_file the file as stored in the WordPress database |
|
|
|
|
85
|
|
|
*/ |
86
|
|
|
protected $_wp_attached_file; |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Creates a new Timber\Image object |
90
|
|
|
* @example |
91
|
|
|
* ```php |
92
|
|
|
* // You can pass it an ID number |
93
|
|
|
* $myImage = new Timber\Image(552); |
94
|
|
|
* |
95
|
|
|
* //Or send it a URL to an image |
96
|
|
|
* $myImage = new Timber\Image('http://google.com/logo.jpg'); |
97
|
|
|
* ``` |
98
|
|
|
* @param bool|int|string $iid |
99
|
|
|
*/ |
100
|
|
|
public function __construct( $iid ) { |
101
|
|
|
$this->init($iid); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @return string the src of the file |
106
|
|
|
*/ |
107
|
|
|
public function __toString() { |
108
|
|
|
if ( $src = $this->src() ) { |
109
|
|
|
return $src; |
110
|
|
|
} |
111
|
|
|
return ''; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Get a PHP array with pathinfo() info from the file |
116
|
|
|
* @return array |
117
|
|
|
*/ |
118
|
|
|
public function get_pathinfo() { |
119
|
|
|
return PathHelper::pathinfo($this->file); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @internal |
124
|
|
|
* @param string $dim |
125
|
|
|
* @return array|int |
126
|
|
|
*/ |
127
|
|
|
protected function get_dimensions( $dim ) { |
128
|
|
|
if ( isset($this->_dimensions) ) { |
129
|
|
|
return $this->get_dimensions_loaded($dim); |
130
|
|
|
} |
131
|
|
|
if ( file_exists($this->file_loc) && filesize($this->file_loc) ) { |
132
|
|
|
if ( ImageHelper::is_svg( $this->file_loc ) ) { |
133
|
|
|
$svg_size = $this->svgs_get_dimensions( $this->file_loc ); |
134
|
|
|
$this->_dimensions = array(); |
135
|
|
|
$this->_dimensions[0] = $svg_size->width; |
136
|
|
|
$this->_dimensions[1] = $svg_size->height; |
137
|
|
|
} else { |
138
|
|
|
list($width, $height) = getimagesize($this->file_loc); |
139
|
|
|
$this->_dimensions = array(); |
140
|
|
|
$this->_dimensions[0] = $width; |
141
|
|
|
$this->_dimensions[1] = $height; |
142
|
|
|
} |
143
|
|
|
return $this->get_dimensions_loaded($dim); |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Retrieve dimensions from SVG file |
149
|
|
|
* |
150
|
|
|
* @internal |
151
|
|
|
* @param string $svg SVG Path |
152
|
|
|
* @return array |
153
|
|
|
*/ |
154
|
|
|
protected function svgs_get_dimensions( $svg ) { |
155
|
|
|
$svg = simplexml_load_file( $svg ); |
156
|
|
|
$width = '0'; |
157
|
|
|
$height = '0'; |
158
|
|
|
|
159
|
|
|
if ( false !== $svg ) { |
160
|
|
|
$attributes = $svg->attributes(); |
161
|
|
|
if ( isset( $attributes->viewBox ) ) { |
162
|
|
|
$viewbox = explode( ' ', $attributes->viewBox ); |
163
|
|
|
$width = $viewbox[2]; |
164
|
|
|
$height = $viewbox[3]; |
165
|
|
|
} elseif ( $attributes->width && $attributes->height ) { |
166
|
|
|
$width = (string) $attributes->width; |
167
|
|
|
$height = (string) $attributes->height; |
168
|
|
|
} |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
return (object) array( |
172
|
|
|
'width' => $width, |
173
|
|
|
'height' => $height, |
174
|
|
|
); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* @internal |
179
|
|
|
* @param string|null $dim |
180
|
|
|
* @return array|int |
181
|
|
|
*/ |
182
|
|
|
protected function get_dimensions_loaded( $dim ) { |
183
|
|
|
$dim = strtolower($dim); |
|
|
|
|
184
|
|
|
if ( $dim == 'h' || $dim == 'height' ) { |
185
|
|
|
return $this->_dimensions[1]; |
186
|
|
|
} |
187
|
|
|
return $this->_dimensions[0]; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* @return array |
192
|
|
|
*/ |
193
|
|
|
protected function get_post_custom( $iid ) { |
194
|
|
|
$pc = get_post_custom($iid); |
195
|
|
|
if ( is_bool($pc) ) { |
196
|
|
|
return array(); |
197
|
|
|
} |
198
|
|
|
return $pc; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* @internal |
203
|
|
|
* @param int $iid the id number of the image in the WP database |
204
|
|
|
*/ |
205
|
|
|
protected function get_image_info( $iid ) { |
206
|
|
|
$image_info = $iid; |
207
|
|
|
if ( is_numeric($iid) ) { |
|
|
|
|
208
|
|
|
$image_info = wp_get_attachment_metadata($iid); |
209
|
|
|
if ( !is_array($image_info) ) { |
210
|
|
|
$image_info = array(); |
211
|
|
|
} |
212
|
|
|
$image_custom = self::get_post_custom($iid); |
|
|
|
|
213
|
|
|
$basic = get_post($iid); |
214
|
|
|
if ( $basic ) { |
215
|
|
|
if ( isset($basic->post_excerpt) ) { |
216
|
|
|
$this->caption = $basic->post_excerpt; |
217
|
|
|
} |
218
|
|
|
$image_custom = array_merge($image_custom, get_object_vars($basic)); |
|
|
|
|
219
|
|
|
} |
220
|
|
|
return array_merge($image_info, $image_custom); |
221
|
|
|
} |
222
|
|
|
if ( is_array($image_info) && isset($image_info['image']) ) { |
223
|
|
|
return $image_info['image']; |
224
|
|
|
} |
225
|
|
|
if ( is_object($image_info) ) { |
226
|
|
|
return get_object_vars($image_info); |
227
|
|
|
} |
228
|
|
|
return $iid; |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
/** |
232
|
|
|
* @internal |
233
|
|
|
* @param string $url for evaluation |
234
|
|
|
* @return string with http/https corrected depending on what's appropriate for server |
235
|
|
|
*/ |
236
|
|
|
protected static function _maybe_secure_url( $url ) { |
237
|
|
|
if ( is_ssl() && strpos($url, 'https') !== 0 && strpos($url, 'http') === 0 ) { |
238
|
|
|
$url = 'https'.substr($url, strlen('http')); |
239
|
|
|
} |
240
|
|
|
return $url; |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
/** |
244
|
|
|
* Gets cached version of wp_upload_dir(). |
245
|
|
|
* |
246
|
|
|
* Because wp_upload_dir() returns a different result for each site in a multisite, we shouldn’t |
247
|
|
|
* return the cached version when we switched to a different site in a multisite environment. |
248
|
|
|
* |
249
|
|
|
* @todo Deprecate this function in the future and use wp_upload_dir() directly. |
250
|
|
|
*/ |
251
|
|
|
public static function wp_upload_dir() { |
252
|
|
|
static $wp_upload_dir = false; |
253
|
|
|
|
254
|
|
|
if ( ! $wp_upload_dir || ( is_multisite() && ms_is_switched() ) ) { |
255
|
|
|
$wp_upload_dir = wp_upload_dir(); |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
return $wp_upload_dir; |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
/** |
262
|
|
|
* @internal |
263
|
|
|
* @param int|bool|string $iid |
264
|
|
|
*/ |
265
|
|
|
public function init( $iid = false ) { |
266
|
|
|
//Make sure we actually have something to work with |
267
|
|
|
if ( !$iid ) { Helper::error_log('Initalized TimberImage without providing first parameter.'); return; } |
268
|
|
|
|
269
|
|
|
//If passed TimberImage, grab the ID and continue |
270
|
|
|
if ( $iid instanceof self ) { |
|
|
|
|
271
|
|
|
$iid = (int) $iid->ID; |
272
|
|
|
} |
273
|
|
|
|
274
|
|
|
//If passed ACF image array |
275
|
|
|
if ( is_array($iid) && isset($iid['ID']) ) { |
|
|
|
|
276
|
|
|
$iid = $iid['ID']; |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
if ( !is_numeric($iid) && is_string($iid) ) { |
280
|
|
|
if ( strpos($iid, '//') === 0 || strstr($iid, '://') ) { |
281
|
|
|
$this->init_with_url($iid); |
282
|
|
|
return; |
283
|
|
|
} |
284
|
|
|
if ( strstr($iid, ABSPATH) ) { |
285
|
|
|
$this->init_with_file_path($iid); |
286
|
|
|
return; |
287
|
|
|
} |
288
|
|
|
|
289
|
|
|
$relative = false; |
290
|
|
|
$iid_lower = strtolower($iid); |
291
|
|
|
foreach ( $this->file_types as $type ) { if ( strstr($iid_lower, $type) ) { $relative = true; break; } }; |
292
|
|
|
if ( $relative ) { |
293
|
|
|
$this->init_with_relative_path($iid); |
294
|
|
|
return; |
295
|
|
|
} |
296
|
|
|
} else if ( $iid instanceof \WP_Post ) { |
297
|
|
|
$ref = new \ReflectionClass($this); |
298
|
|
|
$post = $ref->getParentClass()->newInstance($iid->ID); |
299
|
|
|
if ( isset($post->_thumbnail_id) && $post->_thumbnail_id ) { |
300
|
|
|
return $this->init((int) $post->_thumbnail_id); |
301
|
|
|
} |
302
|
|
|
return $this->init($iid->ID); |
303
|
|
|
} else if ( $iid instanceof Post ) { |
304
|
|
|
/** |
305
|
|
|
* This will catch TimberPost and any post classes that extend TimberPost, |
306
|
|
|
* see http://php.net/manual/en/internals2.opcodes.instanceof.php#109108 |
307
|
|
|
* and https://timber.github.io/docs/guides/extending-timber/ |
308
|
|
|
*/ |
309
|
|
|
$iid = (int) $iid->_thumbnail_id; |
|
|
|
|
310
|
|
|
} |
311
|
|
|
|
312
|
|
|
$image_info = $this->get_image_info($iid); |
313
|
|
|
|
314
|
|
|
$this->import($image_info); |
315
|
|
|
$basedir = self::wp_upload_dir(); |
316
|
|
|
$basedir = $basedir['basedir']; |
317
|
|
|
if ( isset($this->file) ) { |
318
|
|
|
$this->file_loc = $basedir.DIRECTORY_SEPARATOR.$this->file; |
319
|
|
|
} else if ( isset($this->_wp_attached_file) ) { |
320
|
|
|
$this->file = reset($this->_wp_attached_file); |
321
|
|
|
$this->file_loc = $basedir.DIRECTORY_SEPARATOR.$this->file; |
322
|
|
|
} |
323
|
|
|
if ( isset($image_info['id']) ) { |
324
|
|
|
$this->ID = $image_info['id']; |
325
|
|
|
} else if ( is_numeric($iid) ) { |
326
|
|
|
$this->ID = $iid; |
327
|
|
|
} |
328
|
|
|
if ( isset($this->ID) ) { |
329
|
|
|
$custom = self::get_post_custom($this->ID); |
|
|
|
|
330
|
|
|
foreach ( $custom as $key => $value ) { |
331
|
|
|
$this->$key = $value[0]; |
332
|
|
|
} |
333
|
|
|
$this->id = $this->ID; |
|
|
|
|
334
|
|
|
} else { |
335
|
|
|
if ( is_array($iid) || is_object($iid) ) { |
336
|
|
|
Helper::error_log('Not able to init in TimberImage with iid='); |
337
|
|
|
Helper::error_log($iid); |
338
|
|
|
} else { |
339
|
|
|
Helper::error_log('Not able to init in TimberImage with iid='.$iid); |
340
|
|
|
} |
341
|
|
|
} |
342
|
|
|
} |
343
|
|
|
|
344
|
|
|
/** |
345
|
|
|
* @internal |
346
|
|
|
* @param string $relative_path |
347
|
|
|
*/ |
348
|
|
|
protected function init_with_relative_path( $relative_path ) { |
349
|
|
|
$this->abs_url = home_url($relative_path); |
350
|
|
|
$file_path = URLHelper::get_full_path($relative_path); |
351
|
|
|
$this->file_loc = $file_path; |
352
|
|
|
$this->file = $file_path; |
353
|
|
|
} |
354
|
|
|
|
355
|
|
|
/** |
356
|
|
|
* @internal |
357
|
|
|
* @param string $file_path |
358
|
|
|
*/ |
359
|
|
|
protected function init_with_file_path( $file_path ) { |
360
|
|
|
$url = URLHelper::file_system_to_url($file_path); |
361
|
|
|
$this->abs_url = $url; |
362
|
|
|
$this->file_loc = $file_path; |
363
|
|
|
$this->file = $file_path; |
364
|
|
|
} |
365
|
|
|
|
366
|
|
|
/** |
367
|
|
|
* @internal |
368
|
|
|
* @param string $url |
369
|
|
|
*/ |
370
|
|
|
protected function init_with_url( $url ) { |
371
|
|
|
$this->abs_url = $url; |
372
|
|
|
if ( URLHelper::is_local($url) ) { |
373
|
|
|
$this->file = URLHelper::remove_double_slashes(ABSPATH.URLHelper::get_rel_url($url)); |
374
|
|
|
$this->file_loc = URLHelper::remove_double_slashes(ABSPATH.URLHelper::get_rel_url($url)); |
375
|
|
|
} |
376
|
|
|
} |
377
|
|
|
|
378
|
|
|
/** |
379
|
|
|
* @api |
380
|
|
|
* @example |
381
|
|
|
* ```twig |
382
|
|
|
* <img src="{{ image.src }}" alt="{{ image.alt }}" /> |
383
|
|
|
* ``` |
384
|
|
|
* ```html |
385
|
|
|
* <img src="http://example.org/wp-content/uploads/2015/08/pic.jpg" alt="W3 Checker told me to add alt text, so I am" /> |
386
|
|
|
* ``` |
387
|
|
|
* @return string alt text stored in WordPress |
388
|
|
|
*/ |
389
|
|
|
public function alt() { |
390
|
|
|
$alt = trim(strip_tags(get_post_meta($this->ID, '_wp_attachment_image_alt', true))); |
|
|
|
|
391
|
|
|
return $alt; |
392
|
|
|
} |
393
|
|
|
|
394
|
|
|
/** |
395
|
|
|
* @api |
396
|
|
|
* @example |
397
|
|
|
* ```twig |
398
|
|
|
* {% if post.thumbnail.aspect < 1 %} |
399
|
|
|
* {# handle vertical image #} |
400
|
|
|
* <img src="{{ post.thumbnail.src|resize(300, 500) }}" alt="A basketball player" /> |
401
|
|
|
* {% else %} |
402
|
|
|
* <img src="{{ post.thumbnail.src|resize(500) }}" alt="A sumo wrestler" /> |
403
|
|
|
* {% endif %} |
404
|
|
|
* ``` |
405
|
|
|
* @return float |
406
|
|
|
*/ |
407
|
|
|
public function aspect() { |
408
|
|
|
$w = intval($this->width()); |
409
|
|
|
$h = intval($this->height()); |
410
|
|
|
return $w / $h; |
411
|
|
|
} |
412
|
|
|
|
413
|
|
|
/** |
414
|
|
|
* @api |
415
|
|
|
* @example |
416
|
|
|
* ```twig |
417
|
|
|
* <img src="{{ image.src }}" height="{{ image.height }}" /> |
418
|
|
|
* ``` |
419
|
|
|
* ```html |
420
|
|
|
* <img src="http://example.org/wp-content/uploads/2015/08/pic.jpg" height="900" /> |
421
|
|
|
* ``` |
422
|
|
|
* @return int |
423
|
|
|
*/ |
424
|
|
|
public function height() { |
425
|
|
|
return $this->get_dimensions('height'); |
426
|
|
|
} |
427
|
|
|
|
428
|
|
|
/** |
429
|
|
|
* Returns the link to an image attachment's Permalink page (NOT the link for the image itself!!) |
430
|
|
|
* @api |
431
|
|
|
* @example |
432
|
|
|
* ```twig |
433
|
|
|
* <a href="{{ image.link }}"><img src="{{ image.src }} "/></a> |
434
|
|
|
* ``` |
435
|
|
|
* ```html |
436
|
|
|
* <a href="http://example.org/my-cool-picture"><img src="http://example.org/wp-content/uploads/2015/whatever.jpg"/></a> |
437
|
|
|
* ``` |
438
|
|
|
*/ |
439
|
|
|
public function link() { |
440
|
|
|
if (!empty($this->abs_url)) { |
441
|
|
|
return $this->abs_url; |
442
|
|
|
} |
443
|
|
|
return get_permalink($this->ID); |
|
|
|
|
444
|
|
|
} |
445
|
|
|
|
446
|
|
|
/** |
447
|
|
|
* @api |
448
|
|
|
* @return bool|TimberPost |
|
|
|
|
449
|
|
|
*/ |
450
|
|
|
public function parent() { |
451
|
|
|
if ( !$this->post_parent ) { |
452
|
|
|
return false; |
453
|
|
|
} |
454
|
|
|
return new $this->PostClass($this->post_parent); |
455
|
|
|
} |
456
|
|
|
|
457
|
|
|
/** |
458
|
|
|
* @api |
459
|
|
|
* @example |
460
|
|
|
* ```twig |
461
|
|
|
* <img src="{{ image.path }}" /> |
462
|
|
|
* ``` |
463
|
|
|
* ```html |
464
|
|
|
* <img src="/wp-content/uploads/2015/08/pic.jpg" /> |
465
|
|
|
* ``` |
466
|
|
|
* @return string the /relative/path/to/the/file |
467
|
|
|
*/ |
468
|
|
|
public function path() { |
469
|
|
|
return URLHelper::get_rel_path($this->src()); |
470
|
|
|
} |
471
|
|
|
|
472
|
|
|
/** |
473
|
|
|
* @param string $size a size known to WordPress (like "medium") |
474
|
|
|
* @api |
475
|
|
|
* @example |
476
|
|
|
* ```twig |
477
|
|
|
* <h1>{{ post.title }}</h1> |
478
|
|
|
* <img src="{{ post.thumbnail.src }}" /> |
479
|
|
|
* ``` |
480
|
|
|
* ```html |
481
|
|
|
* <img src="http://example.org/wp-content/uploads/2015/08/pic.jpg" /> |
482
|
|
|
* ``` |
483
|
|
|
* @return bool|string |
484
|
|
|
*/ |
485
|
|
|
public function src( $size = 'full' ) { |
486
|
|
|
if (!empty($this->abs_url)) { |
487
|
|
|
return $this->_maybe_secure_url($this->abs_url); |
488
|
|
|
} |
489
|
|
|
|
490
|
|
|
if (!$this->is_image()) { |
491
|
|
|
return wp_get_attachment_url($this->ID); |
|
|
|
|
492
|
|
|
} |
493
|
|
|
|
494
|
|
|
$src = wp_get_attachment_image_src($this->ID, $size); |
|
|
|
|
495
|
|
|
$src = $src[0]; |
496
|
|
|
$src = apply_filters('timber/image/src', $src, $this->ID); |
497
|
|
|
$src = apply_filters('timber_image_src', $src, $this->ID); |
498
|
|
|
return $src; |
499
|
|
|
} |
500
|
|
|
|
501
|
|
|
/** |
502
|
|
|
* @param string $size a size known to WordPress (like "medium") |
503
|
|
|
* @api |
504
|
|
|
* @example |
505
|
|
|
* ```twig |
506
|
|
|
* <h1>{{ post.title }}</h1> |
507
|
|
|
* <img src="{{ post.thumbnail.src }}" srcset="{{ post.thumbnail.srcset }}" /> |
508
|
|
|
* ``` |
509
|
|
|
* ```html |
510
|
|
|
* <img src="http://example.org/wp-content/uploads/2018/10/pic.jpg" srcset="http://example.org/wp-content/uploads/2018/10/pic.jpg 1024w, http://example.org/wp-content/uploads/2018/10/pic-600x338.jpg 600w, http://example.org/wp-content/uploads/2018/10/pic-300x169.jpg 300w" /> |
511
|
|
|
* ``` |
512
|
|
|
* @return bool|string |
513
|
|
|
*/ |
514
|
|
|
public function srcset( $size = "full" ) { |
515
|
|
|
if( $this->is_image() ){ |
516
|
|
|
return wp_get_attachment_image_srcset($this->ID, $size); |
|
|
|
|
517
|
|
|
} |
518
|
|
|
} |
519
|
|
|
|
520
|
|
|
/** |
521
|
|
|
* @param string $size a size known to WordPress (like "medium") |
522
|
|
|
* @api |
523
|
|
|
* @example |
524
|
|
|
* ```twig |
525
|
|
|
* <h1>{{ post.title }}</h1> |
526
|
|
|
* <img src="{{ post.thumbnail.src }}" srcset="{{ post.thumbnail.srcset }}" sizes="{{ post.thumbnail.img_sizes }}" /> |
527
|
|
|
* ``` |
528
|
|
|
* ```html |
529
|
|
|
* <img src="http://example.org/wp-content/uploads/2018/10/pic.jpg" srcset="http://example.org/wp-content/uploads/2018/10/pic.jpg 1024w, http://example.org/wp-content/uploads/2018/10/pic-600x338.jpg 600w, http://example.org/wp-content/uploads/2018/10/pic-300x169.jpg 300w sizes="(max-width: 1024px) 100vw, 102" /> |
530
|
|
|
* ``` |
531
|
|
|
* @return bool|string |
532
|
|
|
*/ |
533
|
|
|
public function img_sizes( $size = "full" ) { |
534
|
|
|
if( $this->is_image() ){ |
535
|
|
|
return wp_get_attachment_image_sizes($this->ID, $size); |
|
|
|
|
536
|
|
|
} |
537
|
|
|
} |
538
|
|
|
|
539
|
|
|
/** |
540
|
|
|
* @internal |
541
|
|
|
* @return bool true if media is an image |
542
|
|
|
*/ |
543
|
|
|
protected function is_image() { |
544
|
|
|
$src = wp_get_attachment_url($this->ID); |
|
|
|
|
545
|
|
|
$image_exts = array( 'gif', 'jpg', 'jpeg', 'jpe', 'png', 'webp' ); |
546
|
|
|
$check = wp_check_filetype(PathHelper::basename($src), null); |
|
|
|
|
547
|
|
|
return in_array($check['ext'], $image_exts); |
548
|
|
|
} |
549
|
|
|
|
550
|
|
|
/** |
551
|
|
|
* @api |
552
|
|
|
* @example |
553
|
|
|
* ```twig |
554
|
|
|
* <img src="{{ image.src }}" width="{{ image.width }}" /> |
555
|
|
|
* ``` |
556
|
|
|
* ```html |
557
|
|
|
* <img src="http://example.org/wp-content/uploads/2015/08/pic.jpg" width="1600" /> |
558
|
|
|
* ``` |
559
|
|
|
* @return int |
560
|
|
|
*/ |
561
|
|
|
public function width() { |
562
|
|
|
return $this->get_dimensions('width'); |
563
|
|
|
} |
564
|
|
|
|
565
|
|
|
/** |
566
|
|
|
* @deprecated 0.21.9 use TimberImage::src |
567
|
|
|
* @codeCoverageIgnore |
568
|
|
|
* @internal |
569
|
|
|
* @param string $size |
570
|
|
|
* @return bool|string |
571
|
|
|
*/ |
572
|
|
|
public function get_src( $size = '' ) { |
573
|
|
|
Helper::warn('{{image.get_src}} is deprecated and will be removed in 1.1; use {{image.src}}'); |
574
|
|
|
return $this->src($size); |
575
|
|
|
} |
576
|
|
|
|
577
|
|
|
|
578
|
|
|
/** |
579
|
|
|
* @deprecated since 0.21.9 use src() instead |
580
|
|
|
* @codeCoverageIgnore |
581
|
|
|
* @return string |
582
|
|
|
*/ |
583
|
|
|
public function url( $size = '' ) { |
584
|
|
|
Helper::warn('{{image.url}} is deprecated and will be removed in 1.1; use {{image.src}}'); |
585
|
|
|
return $this->src($size); |
586
|
|
|
} |
587
|
|
|
|
588
|
|
|
|
589
|
|
|
/** |
590
|
|
|
* @deprecated since 0.21.9 use src() instead |
591
|
|
|
* @codeCoverageIgnore |
592
|
|
|
* @return string |
593
|
|
|
*/ |
594
|
|
|
public function get_url( $size = '' ) { |
595
|
|
|
Helper::warn('{{image.get_url}} is deprecated and will be removed in 1.1; use {{image.src}}'); |
596
|
|
|
return $this->src($size); |
597
|
|
|
} |
598
|
|
|
} |
599
|
|
|
|