PHP Class Timber\ImageHelper

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
Exibir arquivo Open project: jarednova/timber Class Usage Examples

Public Methods

Method Description
_delete_generated_if_image ( integer $post_id ) Checks if attachment is an image before deleting generated files
add_filters ( ) : void adds a 'relative' key to wp_upload_dir() result.
delete_generated_files ( string $local_file ) Deletes the auto-generated files for resize and letterboxing created by Timber
get_letterbox_file_path ( $url, $w, $h, $color )
get_letterbox_file_url ( $url, $w, $h, $color )
get_resize_file_path ( $url, $w, $h, $crop )
get_resize_file_url ( $url, $w, $h, $crop )
get_server_location ( string $url ) : string Determines the filepath corresponding to a given URL
get_sideloaded_file_loc ( string $file ) : string Determines the filepath where a given external file will be stored.
img_to_jpg ( string $src, string $bghex = '#FFFFFF', $force = false ) : string Generates a new image by converting the source GIF or PNG into JPG
init ( )
is_animated_gif ( string $file ) : boolean checks to see if the given file is an aimated gif
letterbox ( string $src, integer $w, integer $h, string $color = '#000000', boolean $force = false ) : string Generate a new image with the specified dimensions.
resize ( string $src, integer | string $w, integer $h, string $crop = 'default', boolean $force = false ) : string Generates a new image with the specified dimensions.
retina_resize ( string $src, float $multiplier = 2, boolean $force = false ) : string Generates a new image with increased size, for display on Retina screens.
sideload_image ( string $file ) : string downloads an external image to the server and stores it on the server

Protected Methods

Method Description
add_actions ( ) Deletes all resized versions of an image when the source is deleted or its meta data is regenerated
add_constants ( ) Adds a constant defining the path to the content directory relative to the site for example /wp-content or /content
process_delete_generated_files ( string $filename, string $ext, string $dir, string $search_pattern, string $match_pattern = null ) Deletes resized versions of the supplied file name.

Private Methods

Method Description
_get_file_path ( integer $base, string $subdir, string $filename ) : string Builds the absolute file system location of a file based on its different components
_get_file_url ( integer $base, string $subdir, string $filename, boolean $absolute ) : string Builds the public URL of a file based on its different components
_operate ( string $src, object $op, boolean $force = false ) : string Main method that applies operation to src image: 1. break down supplied URL into components 2. use components to determine result file and URL 3. check if a result file already exists 4. otherwise, delegate to supplied TimberImageOperation
analyze_url ( string $url ) : array Takes in an URL and breaks it into components, that will then be used in the different steps of image processing.
find_wp_dimensions ( string $size ) : false | array Find the sizes of an image based on a defined image size

Method Details

_delete_generated_if_image() public static method

Checks if attachment is an image before deleting generated files
public static _delete_generated_if_image ( integer $post_id )
$post_id integer an attachment post id

add_actions() protected static method

Deletes all resized versions of an image when the source is deleted or its meta data is regenerated
protected static add_actions ( )

add_constants() protected static method

Adds a constant defining the path to the content directory relative to the site for example /wp-content or /content
protected static add_constants ( )

add_filters() static public method

It will contain the relative url to upload dir.
static public add_filters ( ) : void
return void

delete_generated_files() static public method

Deletes the auto-generated files for resize and letterboxing created by Timber
static public delete_generated_files ( string $local_file )
$local_file string ex: /var/www/wp-content/uploads/2015/my-pic.jpg or: http://example.org/wp-content/uploads/2015/my-pic.jpg

get_letterbox_file_path() public static method

public static get_letterbox_file_path ( $url, $w, $h, $color )

get_letterbox_file_url() public static method

public static get_letterbox_file_url ( $url, $w, $h, $color )

get_resize_file_path() public static method

public static get_resize_file_path ( $url, $w, $h, $crop )

get_resize_file_url() public static method

public static get_resize_file_url ( $url, $w, $h, $crop )

get_server_location() public static method

Determines the filepath corresponding to a given URL
public static get_server_location ( string $url ) : string
$url string
return string

get_sideloaded_file_loc() public static method

Determines the filepath where a given external file will be stored.
public static get_sideloaded_file_loc ( string $file ) : string
$file string
return string

img_to_jpg() public static method

Generates a new image by converting the source GIF or PNG into JPG
public static img_to_jpg ( string $src, string $bghex = '#FFFFFF', $force = false ) : string
$src string a url or path to the image (http://example.org/wp-content/uploads/2014/image.jpg) or (/wp-content/uploads/2014/image.jpg)
$bghex string
return string

init() public static method

public static init ( )

is_animated_gif() public static method

checks to see if the given file is an aimated gif
public static is_animated_gif ( string $file ) : boolean
$file string local filepath to a file, not a URL
return boolean true if it's an animated gif, false if not

letterbox() public static method

New dimensions are achieved by adding colored bands to maintain ratio.
public static letterbox ( string $src, integer $w, integer $h, string $color = '#000000', boolean $force = false ) : string
$src string
$w integer
$h integer
$color string
$force boolean
return string

process_delete_generated_files() protected static method

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);
protected static process_delete_generated_files ( string $filename, string $ext, string $dir, string $search_pattern, string $match_pattern = null )
$filename string ex: my-pic
$ext string ex: jpg
$dir string var/www/wp-content/uploads/2015/
$search_pattern string pattern of files to pluck from
$match_pattern string pattern of files to go forth and delete

resize() public static method

New dimensions are achieved by cropping to maintain ratio.
public static resize ( string $src, integer | string $w, integer $h, string $crop = 'default', boolean $force = false ) : string
$src string an URL (absolute or relative) to the original image
$w integer | string target width(int) or WordPress image size (WP-set or user-defined).
$h integer target height (ignored if $w is WP image size). If not set, will ignore and resize based on $w only.
$crop string your choices are 'default', 'center', 'top', 'bottom', 'left', 'right'
$force boolean
return string (ex: )

retina_resize() public static method

Generates a new image with increased size, for display on Retina screens.
public static retina_resize ( string $src, float $multiplier = 2, boolean $force = false ) : string
$src string
$multiplier float
$force boolean
return string url to the new image

sideload_image() public static method

downloads an external image to the server and stores it on the server
public static sideload_image ( string $file ) : string
$file string the URL to the original file
return string the URL to the downloaded file