Skip to main content
Timber

You are reading the documentation for Timber v2.x. Switch to the documentation for Timber v1.x.

Timber\​ImageDimensions

Helper class to deal with Image Dimensions

Overview #

Properties #

NameTypeDescription
$file_locstringThe absolute path to the image in the filesystem (Example: /var/www/htdocs/wp-content/uploads/2015/08/my-pic.jpg)

Methods #

NameReturn TypeSummary/Returns
aspect()float or nullGets the aspect ratio of the image.

Returns: The aspect ratio of the image. Null if the aspect ratio can’t be calculated.
height()int or nullGets the height of the image in pixels.

Returns: The height of the image in pixels. Null if the height can’t be read, e.g. because the file doesn’t exist.
width()int or nullGets the width of the image in pixels.

Returns: The width of the image in pixels. Null if the width can’t be read, e.g. because the file doesn’t exist.

Class Methods #

aspect() #

Gets the aspect ratio of the image.

Returns: float|null The aspect ratio of the image. Null if the aspect ratio can’t be calculated.

Twig

{% if post.thumbnail.aspect < 1 %}
{# handle vertical image #}
<img src="{{ post.thumbnail.src|resize(300, 500) }}" alt="A basketball player" />
{% else %}
<img src="{{ post.thumbnail.src|resize(500) }}" alt="A sumo wrestler" />
{% endif %}

height() #

Gets the height of the image in pixels.

Returns: int|null The height of the image in pixels. Null if the height can’t be read, e.g. because the file doesn’t exist.

Twig

<img src="{{ image.src }}" height="{{ image.height }}" />

HTML

<img src="https://example.org/wp-content/uploads/2015/08/pic.jpg" height="900" />

width() #

Gets the width of the image in pixels.

Returns: int|null The width of the image in pixels. Null if the width can’t be read, e.g. because the file doesn’t exist.

Twig

<img src="{{ image.src }}" width="{{ image.width }}" />

HTML

<img src="https://example.org/wp-content/uploads/2015/08/pic.jpg" width="1600" />