Timber\ImageDimensions
Helper class to deal with Image Dimensions
Overview #
Properties #
| Name | Type | Description |
|---|---|---|
| $file_loc | string | The absolute path to the image in the filesystem (Example: /var/www/htdocs/wp-content/uploads/2015/08/my-pic.jpg) |
Methods #
| Name | Return Type | Summary/Returns |
|---|---|---|
| aspect() | float or null | Gets 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 null | Gets 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 null | Gets 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" />