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

timber / timber / 20695674007

04 Jan 2026 04:14PM UTC coverage: 89.681% (+1.5%) from 88.211%
20695674007

push

travis-ci

nlemoine
test: Fix ancestors post tests

4615 of 5146 relevant lines covered (89.68%)

63.45 hits per line

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

48.48
/src/Image.php
1
<?php
2

3
namespace Timber;
4

5
/**
6
 * Class Image
7
 *
8
 * The `Timber\Image` class represents WordPress attachments that are images.
9
 *
10
 * @api
11
 * @example
12
 * ```php
13
 * $context = Timber::context();
14
 *
15
 * // Lets say you have an alternate large 'cover image' for your post
16
 * // stored in a custom field which returns an image ID.
17
 * $cover_image_id = $context['post']->cover_image;
18
 *
19
 * $context['cover_image'] = Timber::get_post($cover_image_id);
20
 *
21
 * Timber::render('single.twig', $context);
22
 * ```
23
 *
24
 * ```twig
25
 * <article>
26
 *   <img src="{{cover_image.src}}" class="cover-image" />
27
 *   <h1 class="headline">{{post.title}}</h1>
28
 *   <div class="body">
29
 *     {{post.content}}
30
 *   </div>
31
 *
32
 *  <img
33
 *    src="{{ get_image(post.custom_field_with_image_id).src }}"
34
 *    alt="Another way to initialize images as Timber\Image objects, but within Twig" />
35
 * </article>
36
 * ```
37
 *
38
 * ```html
39
 * <article>
40
 *   <img src="https://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
46
 *     src="https://example.org/wp-content/uploads/2015/06/kurt.jpg"
47
 *     alt="Another way to initialize images as Timber\Image objects, but within Twig" />
48
 * </article>
49
 * ```
50
 */
51
class Image extends Attachment implements ImageInterface
52
{
53
    /**
54
     * Representation.
55
     *
56
     * @api
57
     * @var string What does this class represent in WordPress terms?
58
     */
59
    public static $representation = 'image';
60

61
    /**
62
     * Image sizes.
63
     *
64
     * @api
65
     * @var array An array of available sizes for the image.
66
     */
67
    protected array $sizes;
68

69
    /**
70
     * Image dimensions.
71
     *
72
     * @internal
73
     * @var ImageDimensions stores Image Dimensions in a structured way.
74
     */
75
    protected ImageDimensions $image_dimensions;
76

77
    /**
78
     * Gets the Image information.
79
     *
80
     * @internal
81
     *
82
     * @param array $data Data to update.
83
     * @return array
84
     */
85
    protected function get_info(array $data): array
66✔
86
    {
87
        $data = parent::get_info($data);
66✔
88

89
        if ($this->file_loc()) {
66✔
90
            $data['image_dimensions'] = new ImageDimensions($this->file_loc());
66✔
91
        }
92

93
        return $data;
66✔
94
    }
95

96
    /**
97
     * Processes an image's dimensions.
98
     * @deprecated 2.0.0, use `{{ image.width }}` or `{{ image.height }}` in Twig
99
     * @internal
100
     * @param string $dim
101
     * @return array|int
102
     */
103
    protected function get_dimensions($dim)
×
104
    {
105
        Helper::deprecated(
×
106
            'Image::get_dimensions()',
×
107
            'Image::get_width() | Image::get_height()',
×
108
            '2.0.0'
×
109
        );
×
110
        return [$this->image_dimensions->width(), $this->image_dimensions->height()];
×
111
    }
112

113
    /**
114
     * @deprecated 2.0.0, use Image::get_dimension_loaded
115
     * @internal
116
     * @param string|null $dim
117
     * @return array|int
118
     */
119
    protected function get_dimensions_loaded($dim)
×
120
    {
121
        Helper::deprecated(
×
122
            'Image::get_dimensions()',
×
123
            'Image::get_width() or Image::get_height()',
×
124
            '2.0.0'
×
125
        );
×
126

127
        return $this->image_dimensions->get_dimension($dim);
×
128
    }
129

130
    /**
131
     * @deprecated 2.0.0, use Image::meta to retrieve specific fields
132
     * @return array
133
     */
134
    protected function get_post_custom($iid)
×
135
    {
136
        Helper::deprecated(
×
137
            '{{ image.get_post_custom( image.id ) }}',
×
138
            "{{ image.meta('my_field') }}",
×
139
            '2.0.0'
×
140
        );
×
141
        $pc = \get_post_custom($iid);
×
142
        if (\is_bool($pc)) {
×
143
            return [];
×
144
        }
145
        return $pc;
×
146
    }
147

148
    /**
149
     * Gets the source URL for the image.
150
     *
151
     * You can use WordPress image sizes (including the ones you registered with your theme or
152
     * plugin) by passing the name of the size to this function (like `medium` or `large`). If the
153
     * WordPress size has not been generated, it will return an empty string.
154
     *
155
     * @api
156
     * @example
157
     * ```twig
158
     * <img src="{{ post.thumbnail.src }}">
159
     * <img src="{{ post.thumbnail.src('medium') }}">
160
     * ```
161
     * ```html
162
     * <img src="https://example.org/wp-content/uploads/2015/08/pic.jpg" />
163
     * <img src="https://example.org/wp-content/uploads/2015/08/pic-800-600.jpg">
164
     * ```
165
     *
166
     * @param string $size Optional. The requested image size. This can be a size that was in
167
     *                     WordPress. Example: `medium` or `large`. Default `full`.
168
     *
169
     * @return string The src URL for the image.
170
     */
171
    public function src($size = 'full'): string
41✔
172
    {
173
        if (isset($this->abs_url)) {
41✔
174
            return URLHelper::maybe_secure_url($this->abs_url);
×
175
        }
176

177
        $src = \wp_get_attachment_image_src($this->ID, $size);
41✔
178
        $src = $src[0];
41✔
179

180
        /**
181
         * Filters the src URL for a `Timber\Image`.
182
         *
183
         * @see \Timber\Image::src()
184
         * @since 0.21.7
185
         *
186
         * @param string $src The image src.
187
         * @param int    $id  The image ID.
188
         */
189
        $src = \apply_filters('timber/image/src', $src, $this->ID);
41✔
190

191
        /**
192
         * Filters the src URL for a `Timber\Image`.
193
         *
194
         * @deprecated 2.0.0, use `timber/image/src`
195
         */
196
        $src = \apply_filters_deprecated(
41✔
197
            'timber_image_src',
41✔
198
            [$src, $this->ID],
41✔
199
            '2.0.0',
41✔
200
            'timber/image/src'
41✔
201
        );
41✔
202

203
        return $src;
41✔
204
    }
205

206
    /**
207
     * Get image sizes.
208
     *
209
     * @return array
210
     */
211
    public function sizes(): array
4✔
212
    {
213
        return $this->sizes ?? ($this->sizes = (array) $this->metadata('sizes'));
4✔
214
    }
215

216
    /**
217
     * Gets the width of the image in pixels.
218
     *
219
     * @api
220
     * @example
221
     * ```twig
222
     * <img src="{{ image.src }}" width="{{ image.width }}" />
223
     * ```
224
     * ```html
225
     * <img src="https://example.org/wp-content/uploads/2015/08/pic.jpg" width="1600" />
226
     * ```
227
     *
228
     * @return int The width of the image in pixels.
229
     */
230
    public function width()
2✔
231
    {
232
        return $this->image_dimensions->width();
2✔
233
    }
234

235
    /**
236
     * Gets the height of the image in pixels.
237
     *
238
     * @api
239
     * @example
240
     * ```twig
241
     * <img src="{{ image.src }}" height="{{ image.height }}" />
242
     * ```
243
     * ```html
244
     * <img src="https://example.org/wp-content/uploads/2015/08/pic.jpg" height="900" />
245
     * ```
246
     *
247
     * @return int The height of the image in pixels.
248
     */
249
    public function height()
2✔
250
    {
251
        return $this->image_dimensions->height();
2✔
252
    }
253

254
    /**
255
     * Gets the aspect ratio of the image.
256
     *
257
     * @api
258
     * @example
259
     * ```twig
260
     * {% if post.thumbnail.aspect < 1 %}
261
     *   {# handle vertical image #}
262
     *   <img src="{{ post.thumbnail.src|resize(300, 500) }}" alt="A basketball player" />
263
     * {% else %}
264
     *   <img src="{{ post.thumbnail.src|resize(500) }}" alt="A sumo wrestler" />
265
     * {% endif %}
266
     * ```
267
     *
268
     * @return float The aspect ratio of the image.
269
     */
270
    public function aspect()
1✔
271
    {
272
        return $this->image_dimensions->aspect();
1✔
273
    }
274

275
    /**
276
     * Gets the alt text for an image.
277
     *
278
     * For better accessibility, you should always add an alt attribute to your images, even if it’s
279
     * empty.
280
     *
281
     * @api
282
     * @example
283
     * ```twig
284
     * <img src="{{ image.src }}" alt="{{ image.alt }}" />
285
     * ```
286
     * ```html
287
     * <img src="https://example.org/wp-content/uploads/2015/08/pic.jpg"
288
     *     alt="You should always add alt texts to your images for better accessibility" />
289
     * ```
290
     *
291
     * @return string|null Alt text stored in WordPress.
292
     */
293
    public function alt(): ?string
1✔
294
    {
295
        $alt = $this->meta('_wp_attachment_image_alt');
1✔
296
        return \trim((string) \wp_strip_all_tags($alt));
1✔
297
    }
298

299
    /**
300
     * Gets dimension for an image.
301
     * @deprecated 2.0.0, use `{{ image.width }}` or `{{ image.height }}` in Twig
302
     * @internal
303
     *
304
     * @param string $dimension The requested dimension. Either `width` or `height`.
305
     * @return int|null The requested dimension. Null if image file couldn’t be found.
306
     */
307
    protected function get_dimension($dimension)
×
308
    {
309
        Helper::deprecated(
×
310
            'Image::get_dimension()',
×
311
            'Image::get_width() or Image::get_height()',
×
312
            '2.0.0'
×
313
        );
×
314
        return $this->image_dimensions->get_dimension($dimension);
×
315
    }
316

317
    /**
318
     * Gets already loaded dimension values.
319
     *
320
     * @internal
321
     *
322
     * @param string|null $dim Optional. The requested dimension. Either `width` or `height`.
323
     * @return int The requested dimension in pixels.
324
     */
325
    protected function get_dimension_loaded($dim = null)
×
326
    {
327
        return $this->image_dimensions->get_dimension($dim);
×
328
    }
329

330
    /**
331
     * Gets the srcset attribute for an image based on a WordPress image size.
332
     *
333
     * @api
334
     * @example
335
     * ```twig
336
     * <h1>{{ post.title }}</h1>
337
     * <img src="{{ post.thumbnail.src }}" srcset="{{ post.thumbnail.srcset }}" />
338
     * ```
339
     * ```html
340
     * <img src="https://example.org/wp-content/uploads/2018/10/pic.jpg" srcset="https://example.org/wp-content/uploads/2018/10/pic.jpg 1024w, https://example.org/wp-content/uploads/2018/10/pic-600x338.jpg 600w, https://example.org/wp-content/uploads/2018/10/pic-300x169.jpg 300w" />
341
     * ```
342
     * @param string $size An image size known to WordPress (like "medium").
343
     *
344
     * @return string|null
345
     */
346
    public function srcset(string $size = 'full'): ?string
1✔
347
    {
348
        return \wp_get_attachment_image_srcset($this->ID, $size) ?: null;
1✔
349
    }
350

351
    /**
352
     * Gets the sizes attribute for an image based on a WordPress image size.
353
     *
354
     * @api
355
     * @example
356
     * ```twig
357
     * <h1>{{ post.title }}</h1>
358
     * <img src="{{ post.thumbnail.src }}" srcset="{{ post.thumbnail.srcset }}" sizes="{{ post.thumbnail.img_sizes }}" />
359
     * ```
360
     * ```html
361
     * <img src="https://example.org/wp-content/uploads/2018/10/pic.jpg" srcset="https://example.org/wp-content/uploads/2018/10/pic.jpg 1024w, https://example.org/wp-content/uploads/2018/10/pic-600x338.jpg 600w, https://example.org/wp-content/uploads/2018/10/pic-300x169.jpg 300w sizes="(max-width: 1024px) 100vw, 102" />
362
     * ```
363
     *        @param string $size An image size known to WordPress (like "medium").
364
     * @return string|null
365
     */
366
    public function img_sizes(string $size = 'full'): ?string
1✔
367
    {
368
        return \wp_get_attachment_image_sizes($this->ID, $size) ?: null;
1✔
369
    }
370
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc