Timber Logo

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

Timber\​ImageHelper

Implements the Twig image filters: https://timber.github.io/docs/v2/guides/cookbook-images/#arbitrary-resizing-of-images

  • resize
  • retina
  • letterbox
  • tojpg

Implementation:

  • public static functions provide the methods that are called by the filter
  • most of the work is common to all filters (URL analysis, directory gymnastics, file caching, error management) and done by private static functions
  • the specific part (actual image processing) is delegated to dedicated subclasses of TimberImageOperation

Overview #

Methods #

NameReturn TypeSummary/Returns
img_to_jpg()stringGenerates a new image by converting the source GIF or PNG into JPG.

Returns: The URL of the processed image.
is_animated_gif()boolChecks to see if the given file is an animated GIF.

Returns: True if it’s an animated GIF, false if not.
letterbox()stringGenerate a new image with the specified dimensions.
resize()stringGenerates a new image with the specified dimensions.

Returns: The URL of the resized image.
retina_resize()stringGenerates a new image with increased size, for display on Retina screens.

Returns: URL to the new image.

Class Methods #

resize() #

Generates a new image with the specified dimensions.

New dimensions are achieved by cropping to maintain ratio.

resize( string $src, int|string $w, int $h = '', string $crop = 'default', bool $force = false )

Returns: string The URL of the resized image.

NameTypeDescription
$srcstringA URL (absolute or relative) to the original image.
$wint or stringTarget width (int) or WordPress image size (WP-set or user-defined).
$hintOptional. Target height (ignored if $w is WP image size). If not set, will ignore and resize based on $w only. Default 0.
$cropstringOptional. Your choices are default, center, top, bottom, left, right. Default default.
$forceboolOptional. Whether to remove any already existing result file and force file generation. Default false.

Twig

<img src="{{ image.src | resize(300, 200, 'top') }}" />

HTML

<img src="https://example.org/wp-content/uploads/pic-300x200-c-top.jpg" />

retina_resize() #

Generates a new image with increased size, for display on Retina screens.

retina_resize( string $src, float $multiplier = 2, bool $force = false )

Returns: string URL to the new image.

NameTypeDescription
$srcstringURL of the file to read from.
$multiplierfloatOptional. Factor the original dimensions should be multiplied with. Default 2.
$forceboolOptional. Whether to remove any already existing result file and force file generation. Default false.

is_animated_gif() #

Checks to see if the given file is an animated GIF.

is_animated_gif( string $file )

Returns: bool True if it’s an animated GIF, false if not.

NameTypeDescription
$filestringLocal filepath to a file, not a URL.

letterbox() #

Generate a new image with the specified dimensions.

New dimensions are achieved by adding colored bands to maintain ratio.

letterbox( string $src, int $w, int $h, string|bool $color = false, bool $force = false )

Returns: string

NameTypeDescription
$srcstring
$wint
$hint
$colorstring or bool
$forcebool

img_to_jpg() #

Generates a new image by converting the source GIF or PNG into JPG.

img_to_jpg( string $src, string $bghex = '#FFFFFF', mixed $force = false )

Returns: string The URL of the processed image.

NameTypeDescription
$srcstringA URL or path to the image (https://example.org/wp-content/uploads/2014/image.jpg) or (/wp-content/uploads/2014/image.jpg).
$bghexstringThe hex color to use for transparent zones.