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

timber / timber / 5690593717

pending completion
5690593717

Pull #1617

github

web-flow
Merge f587ceffa into b563d274e
Pull Request #1617: 2.x

4433 of 4433 new or added lines in 57 files covered. (100.0%)

3931 of 4433 relevant lines covered (88.68%)

58.28 hits per line

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

44.44
/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="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
46
 *     src="http://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
    public $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
     * @return string the src of the file
79
     */
80
    public function __toString()
81
    {
82
        if ($src = $this->src()) {
3✔
83
            return $src;
3✔
84
        }
85
        return '';
×
86
    }
87

88
    /**
89
     * Gets the Image information.
90
     *
91
     * @internal
92
     *
93
     * @param array $data Data to update.
94
     * @return array
95
     */
96
    protected function get_info(array $data): array
97
    {
98
        $data = parent::get_info($data);
64✔
99

100
        if (isset($data['file_loc'])) {
64✔
101
            $data['image_dimensions'] = new ImageDimensions($data['file_loc']);
64✔
102
        }
103

104
        return $data;
64✔
105
    }
106

107
    /**
108
     * Processes an image's dimensions.
109
     * @deprecated 2.0.0, use `{{ image.width }}` or `{{ image.height }}` in Twig
110
     * @internal
111
     * @param string $dim
112
     * @return array|int
113
     */
114
    protected function get_dimensions($dim)
115
    {
116
        Helper::deprecated(
×
117
            'Image::get_dimensions()',
×
118
            'Image::get_width() | Image::get_height()',
×
119
            '2.0.0'
×
120
        );
×
121
        return [$this->image_dimensions->width(), $this->image_dimensions->height()];
×
122
    }
123

124
    /**
125
     * @deprecated 2.0.0, use Image::get_dimension_loaded
126
     * @internal
127
     * @param string|null $dim
128
     * @return array|int
129
     */
130
    protected function get_dimensions_loaded($dim)
131
    {
132
        Helper::deprecated(
×
133
            'Image::get_dimensions()',
×
134
            'Image::get_width() or Image::get_height()',
×
135
            '2.0.0'
×
136
        );
×
137

138
        return $this->image_dimensions->get_dimension($dim);
×
139
    }
140

141
    /**
142
     * @deprecated 2.0.0, use Image::meta to retrieve specific fields
143
     * @return array
144
     */
145
    protected function get_post_custom($iid)
146
    {
147
        Helper::deprecated(
×
148
            '{{ image.get_post_custom( image.id ) }}',
×
149
            "{{ image.meta('my_field') }}",
×
150
            '2.0.0'
×
151
        );
×
152
        $pc = \get_post_custom($iid);
×
153
        if (\is_bool($pc)) {
×
154
            return [];
×
155
        }
156
        return $pc;
×
157
    }
158

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

188
        $src = \wp_get_attachment_image_src($this->ID, $size);
40✔
189
        $src = $src[0];
40✔
190

191
        /**
192
         * Filters the src URL for a `Timber\Image`.
193
         *
194
         * @see \Timber\Image::src()
195
         * @since 0.21.7
196
         *
197
         * @param string $src The image src.
198
         * @param int    $id  The image ID.
199
         */
200
        $src = \apply_filters('timber/image/src', $src, $this->ID);
40✔
201

202
        /**
203
         * Filters the src URL for a `Timber\Image`.
204
         *
205
         * @deprecated 2.0.0, use `timber/image/src`
206
         */
207
        $src = \apply_filters_deprecated(
40✔
208
            'timber_image_src',
40✔
209
            [$src, $this->ID],
40✔
210
            '2.0.0',
40✔
211
            'timber/image/src'
40✔
212
        );
40✔
213

214
        return $src;
40✔
215
    }
216

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

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

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

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

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

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

331
    /**
332
     * Gets the srcset attribute for an image based on a WordPress image size.
333
     *
334
     * @api
335
     * @example
336
     * ```twig
337
     * <h1>{{ post.title }}</h1>
338
     * <img src="{{ post.thumbnail.src }}" srcset="{{ post.thumbnail.srcset }}" />
339
     * ```
340
     * ```html
341
     * <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" />
342
     * ```
343
     * @param string $size An image size known to WordPress (like "medium").
344
     *
345
     * @return string|null
346
     */
347
    public function srcset(string $size = 'full'): ?string
348
    {
349
        return \wp_get_attachment_image_srcset($this->ID, $size) ?: null;
1✔
350
    }
351

352
    /**
353
     * Gets the sizes attribute for an image based on a WordPress image size.
354
     *
355
     * @api
356
     * @example
357
     * ```twig
358
     * <h1>{{ post.title }}</h1>
359
     * <img src="{{ post.thumbnail.src }}" srcset="{{ post.thumbnail.srcset }}" sizes="{{ post.thumbnail.img_sizes }}" />
360
     * ```
361
     * ```html
362
     * <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" />
363
     * ```
364
     *        @param string $size An image size known to WordPress (like "medium").
365
     * @return string|null
366
     */
367
    public function img_sizes(string $size = 'full'): ?string
368
    {
369
        return \wp_get_attachment_image_sizes($this->ID, $size) ?: null;
1✔
370
    }
371
}
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