Skip to main content
Timber

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

Timber\ImageHelper

Implements the Twig image filters: https://timber.github.io/docs/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 #

NameTypeReturns/Description
resizestring(ex: )

Class Methods #

_delete_generated_if_image #

_delete_generated_if_image( int $post_id )

returns: void

Checks if attachment is an image before deleting generated files

NameTypeDescription
$post_idintan attachment post id

add_relative_upload_dir_key #

add_relative_upload_dir_key( array $arr )

returns: array

Adds a 'relative' key to wp_upload_dir() result. It will contain the relative url to upload dir.

NameTypeDescription
$arrarray

analyze_url #

analyze_url( string $url )

returns: array an array (see keys in code below)

Takes in an URL and breaks it into components, that will then be used in the different steps of image processing. The image is expected to be either part of a theme, plugin, or an upload.

NameTypeDescription
$urlstringan URL (absolute or relative) pointing to an image

delete_attachment #

delete_attachment( int $post_id )

returns: void

Deletes all resized versions of an image when the source is deleted.

NameTypeDescription
$post_idintan attachment post id

delete_generated_files #

delete_generated_files( string $local_file )

returns: void

Deletes the auto-generated files for resize and letterboxing created by Timber

NameTypeDescription
$local_filestringex: /var/www/wp-content/uploads/2015/my-pic.jpg or: http://example.org/wp-content/uploads/2015/my-pic.jpg

generate_attachment_metadata #

generate_attachment_metadata( array $metadata, int $post_id )

returns: array

Delete all resized version of an image when its meta data is regenerated.

NameTypeDescription
$metadataarray
$post_idintan attachment post id

get_letterbox_file_path #

get_letterbox_file_path( mixed $url, mixed $w, mixed $h, mixed $color )

returns: mixed

NameTypeDescription
$urlmixed
$wmixed
$hmixed
$colormixed

get_letterbox_file_url #

get_letterbox_file_url( mixed $url, mixed $w, mixed $h, mixed $color )

returns: mixed

NameTypeDescription
$urlmixed
$wmixed
$hmixed
$colormixed

get_resize_file_path #

get_resize_file_path( mixed $url, mixed $w, mixed $h, mixed $crop )

returns: mixed

NameTypeDescription
$urlmixed
$wmixed
$hmixed
$cropmixed

get_resize_file_url #

get_resize_file_url( mixed $url, mixed $w, mixed $h, mixed $crop )

returns: mixed

NameTypeDescription
$urlmixed
$wmixed
$hmixed
$cropmixed

get_server_location #

get_server_location( string $url )

returns: string

Determines the filepath corresponding to a given URL

NameTypeDescription
$urlstring

get_sideloaded_file_loc #

get_sideloaded_file_loc( string $file )

returns: string

Determines the filepath where a given external file will be stored.

NameTypeDescription
$filestring

img_to_jpg #

img_to_jpg( string $src, string $bghex="#FFFFFF", bool $force=false )

returns: string

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

NameTypeDescription
$srcstringa url or path to the image (http://example.org/wp-content/uploads/2014/image.jpg) or (/wp-content/uploads/2014/image.jpg)
$bghexstring
$forcebool

img_to_webp #

img_to_webp( string $src, mixed/int $quality=80, bool $force=false )

returns: void

Generates a new image by converting the source into WEBP if supported by the server

NameTypeDescription
$srcstringa url or path to the image (http://example.org/wp-content/uploads/2014/image.webp) or (/wp-content/uploads/2014/image.jpg) If webp is not supported, a jpeg image will be generated
$qualitymixed/intranges from (worst quality, smaller file) to 100 (best quality, biggest file)
$forcebool

init #

init( )

returns: void


is_animated_gif #

is_animated_gif( string $file )

returns: boolean true if it's an animated gif, false if not

checks to see if the given file is an aimated gif

NameTypeDescription
$filestringlocal filepath to a file, not a URL

is_svg #

is_svg( string $file_path )

returns: bool True if SVG, false if not SVG or file doesn't exist.

Checks if file is an SVG.

NameTypeDescription
$file_pathstringFile path to check.

letterbox #

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

returns: string

Generate a new image with the specified dimensions. New dimensions are achieved by adding colored bands to maintain ratio.

NameTypeDescription
$srcstring
$wint
$hint
$colorbool/string
$forcebool

resize #

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

returns: string (ex: )

Generates a new image with the specified dimensions. New dimensions are achieved by cropping to maintain ratio.

NameTypeDescription
$srcstringan URL (absolute or relative) to the original image
$wmixed
$hinttarget height (ignored if $w is WP image size). If not set, will ignore and resize based on $w only.
$cropstringyour choices are 'default', 'center', 'top', 'bottom', 'left', 'right'
$forcebool

Twig

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

HTML

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

retina_resize #

retina_resize( string $src, mixed/\Timber\float $multiplier=2, bool/boolean $force=false )

returns: string url to the new image

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

NameTypeDescription
$srcstring
$multipliermixed/\Timber\float
$forcebool/boolean

sideload_image #

sideload_image( string $file )

returns: string the URL to the downloaded file

downloads an external image to the server and stores it on the server

NameTypeDescription
$filestringthe URL to the original file

theme_url_to_dir #

theme_url_to_dir( string $src )

returns: string full path to the file in question

Converts a URL located in a theme directory into the raw file path

NameTypeDescription
$srcstringa URL (http://example.org/wp-content/themes/twentysixteen/images/home.jpg)

is_in_theme_dir #

is_in_theme_dir( string $path )

returns: bool If the image is located in the theme directory it returns true. If not or $path doesn't exits it returns false.

Checks if uploaded image is located in theme.

NameTypeDescription
$pathstringimage path.

maybe_realpath #

maybe_realpath( string $path )

returns: string the resolved path

Runs realpath to resolve symbolic links (../, etc). But only if it's a path and not a URL

NameTypeDescription
$pathstring

process_delete_generated_files #

process_delete_generated_files( string $filename, string $ext, string $dir, string $search_pattern, mixed/string $match_pattern=null )

returns: void

Deletes resized versions of the supplied file name. So if passed a value like my-pic.jpg, this function will delete my-pic-500x200-c-left.jpg, my-pic-400x400-c-default.jpg, etc.

keeping these here so I know what the hell we're matching $match = preg_match("//srv/www/wordpress-develop/src/wp-content/uploads/2014/05/$filename-[0-9]x[0-9]-c-[a-z].jpg/", $found_file); $match = preg_match("//srv/www/wordpress-develop/src/wp-content/uploads/2014/05/arch-[0-9]x[0-9]-c-[a-z].jpg/", $filename);

NameTypeDescription
$filenamestringex: my-pic
$extstringex: jpg
$dirstring
$search_patternstringpattern of files to pluck from
$match_patternmixed/stringpattern of files to go forth and delete