• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

timber / timber / 43920723901

16 Nov 2026 05:39PM UTC coverage: 90.236%. Remained the same
43920723901

push

travis-ci

web-flow
chore(deps): bump shivammathur/setup-php from 2.37.2 to 2.40.0 (#3316)

Bumps [shivammathur/setup-php](https://github.com/shivammathur/setup-php) from 2.37.2 to 2.40.0.
- [Release notes](https://github.com/shivammathur/setup-php/releases)
- [Commits](https://github.com/shivammathur/setup-php/compare/f3e473d11...eb7c497e1)

---
updated-dependencies:
- dependency-name: shivammathur/setup-php
  dependency-version: 2.40.0
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

4787 of 5305 relevant lines covered (90.24%)

72.15 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

97.1
/src/ExternalImage.php
1
<?php
2

3
namespace Timber;
4

5
/**
6
 * Class ExternalImage
7
 *
8
 * The `Timber\ExternalImage` class represents an image that is not part of the WordPress content (Attachment).
9
 * Instead, it’s an image that can be either a path (relative/absolute) on the same server, or a URL (either from the
10
 * same or from a different website). When you use a URL of an image on a different website, Timber will load it into
11
 * your WordPress installation once and then load it from there.
12
 * @phpstan-consistent-constructor
13
 * @api
14
 * @example
15
 * ```php
16
 * $context = Timber::context();
17
 *
18
 * // Lets say you have an external image that you want to use in your theme
19
 *
20
 * $context['cover_image'] = Timber::get_external_image($url);
21
 *
22
 * Timber::render('single.twig', $context);
23
 * ```
24
 *
25
 * ```twig
26
 * <article>
27
 *   <img src="{{ cover_image.src }}" class="cover-image" />
28
 *   <h1 class="headline">{{ post.title }}</h1>
29
 *   <div class="body">
30
 *     {{ post.content }}
31
 *   </div>
32
 * </article>
33
 * ```
34
 *
35
 * ```html
36
 * <article>
37
 *   <img src="https://example.org/wp-content/uploads/2015/06/nevermind.jpg" class="cover-image" />
38
 *   <h1 class="headline">Now you've done it!</h1>
39
 *   <div class="body">
40
 *     Whatever whatever
41
 *   </div>
42
 * </article>
43
 * ```
44
 */
45
class ExternalImage implements ImageInterface
46
{
47
    /**
48
     * Alt text.
49
     *
50
     * @api
51
     * @var string
52
     */
53
    protected string $alt_text;
54

55
    /**
56
     * Alt text.
57
     *
58
     * @api
59
     * @var string
60
     */
61
    protected string $caption;
62

63
    /**
64
     * Representation.
65
     *
66
     * @var string What does this class represent in WordPress terms?
67
     */
68
    public static $representation = 'image';
69

70
    /**
71
     * File location.
72
     *
73
     * @api
74
     * @var string The absolute path to the attachmend file in the filesystem
75
     *             (Example: `/var/www/htdocs/wp-content/themes/my-theme/images/`)
76
     */
77
    protected string $file_loc;
78

79
    /**
80
     * File extension.
81
     *
82
     * @api
83
     * @since 2.0.0
84
     * @var string A file extension.
85
     */
86
    protected string $file_extension;
87

88
    /**
89
     * Absolute URL.
90
     *
91
     * @var string The absolute URL to the attachment.
92
     */
93
    public $abs_url;
94

95
    /**
96
     * Size.
97
     *
98
     * @var integer|null
99
     */
100
    protected ?int $size = null;
101

102
    /**
103
     * File types.
104
     *
105
     * @var array An array of supported relative file types.
106
     */
107
    private $image_file_types = [
108
        'jpg',
109
        'jpeg',
110
        'png',
111
        'webp',
112
        'avif',
113
        'svg',
114
        'bmp',
115
        'ico',
116
        'gif',
117
        'tiff',
118
        'pdf',
119
    ];
120

121
    /**
122
     * Image dimensions.
123
     *
124
     * @internal
125
     * @var ImageDimensions|null stores Image Dimensions in a structured way.
126
     */
127
    protected ?ImageDimensions $image_dimensions = null;
128

129
    protected function __construct()
21 ✔
130
    {
131
    }
21 ✔
132

133
    /**
134
     * Inits the ExternalImage object.
135
     *
136
     * @internal
137
     * @param $url string URL or path to load the image from.
138
     * @param $args array An array of arguments for the image.
139
     */
140
    public static function build($url, array $args = []): ?ExternalImage
22 ✔
141
    {
142
        if (!\is_string($url) || \is_numeric($url)) {
22 ✔
143
            return null;
1 ✔
144
        }
145

146
        $external_image = new static();
21 ✔
147

148
        if (!empty($args['alt'])) {
21 ✔
149
            $external_image->alt_text = (string) $args['alt'];
1 ✔
150
        }
151
        if (!empty($args['caption'])) {
21 ✔
152
            $external_image->caption = (string) $args['caption'];
1 ✔
153
        }
154

155
        if (\str_contains($url, '://')) {
21 ✔
156
            // Assume URL.
157
            $external_image->init_with_url($url);
3 ✔
158

159
            return $external_image;
3 ✔
160
        } elseif (\str_contains($url, (string) ABSPATH)) {
18 ✔
161
            // Assume absolute path.
162
            $external_image->init_with_file_path($url);
15 ✔
163

164
            return $external_image;
15 ✔
165
        } else {
166
            // Check for image file types.
167
            foreach ($external_image->image_file_types as $type) {
3 ✔
168
                // Assume a relative path.
169
                if (\str_contains(\strtolower($url), (string) $type)) {
3 ✔
170
                    $external_image->init_with_relative_path($url);
3 ✔
171

172
                    return $external_image;
3 ✔
173
                }
174
            }
175
        }
176

177
        return null;
×
178
    }
179

180
    /**
181
     * Gets the source URL for the image.
182
     *
183
     * @api
184
     * @example
185
     * ```twig
186
     * <img src="{{ post.thumbnail.src }}">
187
     * <img src="{{ post.thumbnail.src('medium') }}">
188
     * ```
189
     * ```html
190
     * <img src="https://example.org/wp-content/uploads/2015/08/pic.jpg" />
191
     * <img src="https://example.org/wp-content/uploads/2015/08/pic-800-600.jpg">
192
     * ```
193
     *
194
     * @param string $size Ignored. For compatibility with Timber\Image.
195
     *
196
     * @return string The src URL for the image.
197
     */
198
    public function src($size = 'full'): string
13 ✔
199
    {
200
        return URLHelper::maybe_secure_url($this->abs_url);
13 ✔
201
    }
202

203
    /**
204
     * Gets the relative path to the file.
205
     *
206
     * @api
207
     * @example
208
     * ```twig
209
     * <img src="{{ image.path }}" />
210
     * ```
211
     * ```html
212
     * <img src="/wp-content/uploads/2015/08/pic.jpg" />
213
     * ```
214
     *
215
     * @return string The relative path to the image file.
216
     */
217
    public function path(): string
1 ✔
218
    {
219
        return URLHelper::get_rel_path($this->file_loc());
1 ✔
220
    }
221

222
    /**
223
     * Gets the absolute path to the image.
224
     *
225
     * @api
226
     *
227
     * @return string
228
     */
229
    public function file_loc(): string
5 ✔
230
    {
231
        return $this->file_loc ?? '';
5 ✔
232
    }
233

234
    /**
235
     * Gets filesize in a human-readable format.
236
     *
237
     * This can be useful if you want to display the human-readable filesize for a file. It’s
238
     * easier to read «16 KB» than «16555 bytes» or «1 MB» than «1048576 bytes».
239
     *
240
     * @api
241
     * @since 2.0.0
242
     * @example
243
     * Use filesize information in a link that downloads a file:
244
     *
245
     * ```twig
246
     * <a class="download" href="{{ attachment.src }}" download="{{ attachment.title }}">
247
     *     <span class="download-title">{{ attachment.title }}</span>
248
     *     <span class="download-info">(Download, {{ attachment.size }})</span>
249
     * </a>
250
     * ```
251
     *
252
     * @return null|int Filsize or null if the filesize couldn't be determined.
253
     */
254
    public function size(): ?int
1 ✔
255
    {
256
        if (isset($this->size)) {
1 ✔
257
            return $this->size;
×
258
        }
259

260
        /**
261
         * Filesize wasn't found in the metadata, so we'll try to get it from the file itself.
262
         *
263
         * We could have used `wp_filesize()` here, but it returns 0 when the file doesn't exist. Which is a perfectly valid filesize
264
         * and prevents us from telling the difference between a file that doesn't exist and a file that has a filesize of 0.
265
         *
266
         * @see https://developer.wordpress.org/reference/functions/wp_filesize/
267
         */
268
        $size = \filesize($this->file_loc());
1 ✔
269
        return $this->size = $size === false ? null : (int) $size;
1 ✔
270
    }
271

272
    /**
273
     * Gets the src for an attachment.
274
     *
275
     * @api
276
     *
277
     * @return string The src of the attachment.
278
     */
279
    public function __toString(): string
1 ✔
280
    {
281
        return $this->src();
1 ✔
282
    }
283

284
    /**
285
     * Gets the extension of the attached file.
286
     *
287
     * @api
288
     * @since 2.0.0
289
     * @example
290
     *
291
     * Use extension information in a link that downloads a file:
292
     *
293
     * ```twig
294
     * <a class="download" href="{{ attachment.src }}" download="{{ attachment.title }}">
295
     *     <span class="download-title">{{ attachment.title }}</span>
296
     *     <span class="download-info">
297
     *         (Download {{ attachment.extension|upper }}, {{ attachment.size }})
298
     *     </span>
299
     * </a>
300
     * ```
301
     *
302
     * @return string|null An uppercase extension string.
303
     */
304
    public function extension(): ?string
1 ✔
305
    {
306
        return $this->file_extension ?? ($this->file_extension = \pathinfo($this->file_loc(), PATHINFO_EXTENSION));
1 ✔
307
    }
308

309
    /**
310
     * Gets the width of the image in pixels.
311
     *
312
     * @api
313
     * @example
314
     * ```twig
315
     * <img src="{{ image.src }}" width="{{ image.width }}" />
316
     * ```
317
     * ```html
318
     * <img src="https://example.org/wp-content/uploads/2015/08/pic.jpg" width="1600" />
319
     * ```
320
     *
321
     * @return int|null The width of the image in pixels. Null if the width can’t be read, e.g. because the file doesn’t
322
     *                  exist.
323
     */
324
    public function width(): ?int
6 ✔
325
    {
326
        return $this->image_dimensions->width();
6 ✔
327
    }
328

329
    /**
330
     * Gets the height of the image in pixels.
331
     *
332
     * @api
333
     * @example
334
     * ```twig
335
     * <img src="{{ image.src }}" height="{{ image.height }}" />
336
     * ```
337
     * ```html
338
     * <img src="https://example.org/wp-content/uploads/2015/08/pic.jpg" height="900" />
339
     * ```
340
     *
341
     * @return int|null The height of the image in pixels. Null if the height can’t be read, e.g. because the file
342
     *                  doesn’t exist.
343
     */
344
    public function height(): ?int
6 ✔
345
    {
346
        return $this->image_dimensions->height();
6 ✔
347
    }
348

349
    /**
350
     * Gets the aspect ratio of the image.
351
     *
352
     * @api
353
     * @example
354
     * ```twig
355
     * {% if post.thumbnail.aspect < 1 %}
356
     *     {# handle vertical image #}
357
     *     <img src="{{ post.thumbnail.src|resize(300, 500) }}" alt="A basketball player" />
358
     * {% else %}
359
     *     <img src="{{ post.thumbnail.src|resize(500) }}" alt="A sumo wrestler" />
360
     * {% endif %}
361
     * ```
362
     *
363
     * @return float The aspect ratio of the image.
364
     */
365
    public function aspect()
4 ✔
366
    {
367
        return $this->image_dimensions->aspect();
4 ✔
368
    }
369

370
    /**
371
     * Sets the relative alt text of the image.
372
     *
373
     * @param string $alt Alt text for the image.
374
     */
375
    public function set_alt(string $alt)
1 ✔
376
    {
377
        $this->alt_text = $alt;
1 ✔
378
    }
379

380
    /**
381
     * Sets the relative alt text of the image.
382
     *
383
     * @param string $caption Caption text for the image
384
     */
385
    public function set_caption(string $caption)
1 ✔
386
    {
387
        $this->caption = $caption;
1 ✔
388
    }
389

390
    /**
391
     * Inits the object with an absolute path.
392
     *
393
     * @internal
394
     *
395
     * @param string $file_path An absolute path to a file.
396
     */
397
    protected function init_with_file_path($file_path)
15 ✔
398
    {
399
        $url = URLHelper::file_system_to_url($file_path);
15 ✔
400

401
        $this->abs_url = $url;
15 ✔
402
        $this->file_loc = $file_path;
15 ✔
403
        $this->image_dimensions = new ImageDimensions($file_path);
15 ✔
404
    }
405

406
    /**
407
     * Inits the object with a relative path.
408
     *
409
     * @internal
410
     *
411
     * @param string $relative_path A relative path to a file.
412
     */
413
    protected function init_with_relative_path($relative_path)
3 ✔
414
    {
415
        $file_path = URLHelper::get_full_path($relative_path);
3 ✔
416

417
        $this->abs_url = \home_url($relative_path);
3 ✔
418
        $this->file_loc = $file_path;
3 ✔
419
        $this->image_dimensions = new ImageDimensions($file_path);
3 ✔
420
    }
421

422
    /**
423
     * Inits the object with an URL.
424
     *
425
     * @internal
426
     *
427
     * @param string $url An URL on the same host.
428
     */
429
    protected function init_with_url($url)
3 ✔
430
    {
431
        if (!URLHelper::is_local($url)) {
3 ✔
432
            $url = ImageHelper::sideload_image($url);
2 ✔
433
        }
434

435
        $this->abs_url = $url;
3 ✔
436

437
        if (URLHelper::is_local($url)) {
3 ✔
438
            $this->file_loc = URLHelper::remove_double_slashes(
3 ✔
439
                ABSPATH . URLHelper::get_rel_url($url)
3 ✔
440
            );
3 ✔
441
            $this->image_dimensions = new ImageDimensions($this->file_loc);
3 ✔
442
        }
443
    }
444

445
    /**
446
     * Gets the alt text for an image.
447
     *
448
     * For better accessibility, you should always add an alt attribute to your images, even if it’s
449
     * empty.
450
     *
451
     * @api
452
     * @example
453
     * ```twig
454
     * <img src="{{ image.src }}" alt="{{ image.alt }}" />
455
     * ```
456
     * ```html
457
     * <img
458
     *     src="https://example.org/wp-content/uploads/2015/08/pic.jpg"
459
     *     alt="You should always add alt texts to your images for better accessibility"
460
     * />
461
     * ```
462
     *
463
     * @return string Alt text stored in WordPress.
464
     */
465
    public function alt(): ?string
1 ✔
466
    {
467
        return $this->alt_text ?? null;
1 ✔
468
    }
469

470
    public function caption(): ?string
1 ✔
471
    {
472
        return $this->caption ?? null;
1 ✔
473
    }
474
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc