Since: 1.3.0

class TemplateHelper (View source)

This class provides lots of helper functionality usually used in templates.

It can also manage looking up hashes and uploading images to rokka, see the docs for details.

Methods

__construct(string $organization, string $apiKey, AbstractCallbacks $callbacks = null, string|null $publicRokkaDomain = null, string|array|null $options = [])

No description

string|null
getHashMaybeUpload(AbstractLocalImage $image)

Returns the hash of an image.

string
getStackUrl(AbstractLocalImage|string|SplFileInfo $image, string $stack, string|null $format = 'jpg', string|null $seo = null, string|null $seoLanguage = 'de')

Gets the rokka URL for an image Uploads it, if we don't have a hash locally.

string
getResizeUrl(AbstractLocalImage|string|SplFileInfo $image, string|int $width, string|int|null $height = null, string $format = 'jpg', string|null $seo = null, string $seoLanguage = 'de')

Return the rokka URL for getting a resized image.

string
getResizeCropUrl(AbstractLocalImage|string|SplFileInfo $image, string|int $width, string|int $height, string $format = 'jpg', string|null $seo = null, string $seoLanguage = '')

Return the rokka URL for getting a resized and cropped image.

string
getOriginalSizeUrl(AbstractLocalImage|string|SplFileInfo $image, string $format = 'jpg', string|null $seo = null, string $seoLanguage = '')

Return the rokka URL for getting the image in it's original size.

static string
getSrcAttributes(string $url, array $sizes = ['2x'], bool $setWidthInUrl = true)

Returns a src and srcset attibrute (as one string) with the correct rokka render urls for responsive images.

static string
getBackgroundImageStyle(string $url, array $sizes = ['2x'])

Returns a background-image:url defintions (as one string) with the correct rokka render urls for responsive images.

string
getImagename(AbstractLocalImage $image = null)

Returns the filename of the image without extension.

string
generateRokkaUrl(string $hash, string|StackUri $stack, string|null $format = 'jpg', string|null $seo = null, string|null $seoLanguage = 'de')

Gets the rokka URL for an image hash and stack with optional seo filename in the URL.

getRokkaClient()

Gets the rokka image client used by this class.

static string
slugify(string $text, string $language = 'de')

Create a URL-safe text from $text.

getImageObject(AbstractLocalImage|string|SplFileInfo $input, string|null $identifier = null, mixed $context = null)

Returns a LocalImage object depending on the input.

Details

__construct(string $organization, string $apiKey, AbstractCallbacks $callbacks = null, string|null $publicRokkaDomain = null, string|array|null $options = [])

Since: 1.3.0

No description

Parameters

string $organization

Organization name

string $apiKey

API key

AbstractCallbacks $callbacks

Optional callbacks for read and write of hashes

string|null $publicRokkaDomain

Optional public rokka URL, if different from the standard one (org.render.rokka.io)

string|array|null $options

Optional options like api_base_url or proxy

string|null getHashMaybeUpload(AbstractLocalImage $image)

Since: 1.3.0

Returns the hash of an image.

If we don't have an image stored locally, it uploads it to rokka.

Parameters

AbstractLocalImage $image

Return Value

string|null

Exceptions

GuzzleException
RuntimeException

string getStackUrl(AbstractLocalImage|string|SplFileInfo $image, string $stack, string|null $format = 'jpg', string|null $seo = null, string|null $seoLanguage = 'de')

Since: 1.3.0

Gets the rokka URL for an image Uploads it, if we don't have a hash locally.

Parameters

AbstractLocalImage|string|SplFileInfo $image

The image

string $stack

The stack name

string|null $format

The image format of the image (jpg, png, webp, ...)

string|null $seo

if you want a different seo string than the default

string|null $seoLanguage

Optional language to be used for slugifying (eg. 'de' slugifies 'ö' to 'oe')

Return Value

string

Exceptions

GuzzleException
RuntimeException

string getResizeUrl(AbstractLocalImage|string|SplFileInfo $image, string|int $width, string|int|null $height = null, string $format = 'jpg', string|null $seo = null, string $seoLanguage = 'de')

Since: 1.3.0

Return the rokka URL for getting a resized image.

Parameters

AbstractLocalImage|string|SplFileInfo $image

The image to be resized

string|int $width

The width of the image

string|int|null $height

The height of the image

string $format

The image format of the image (jpg, png, webp, ...)

string|null $seo
string $seoLanguage

Return Value

string

Exceptions

GuzzleException
RuntimeException

string getResizeCropUrl(AbstractLocalImage|string|SplFileInfo $image, string|int $width, string|int $height, string $format = 'jpg', string|null $seo = null, string $seoLanguage = '')

Since: 1.3.0

Return the rokka URL for getting a resized and cropped image.

Parameters

AbstractLocalImage|string|SplFileInfo $image

The image to be resized

string|int $width

The width of the image

string|int $height

The height of the image

string $format

The image format of the image (jpg, png, webp, ...)

string|null $seo
string $seoLanguage

Return Value

string

Exceptions

GuzzleException
RuntimeException

string getOriginalSizeUrl(AbstractLocalImage|string|SplFileInfo $image, string $format = 'jpg', string|null $seo = null, string $seoLanguage = '')

Since: 1.3.0

Return the rokka URL for getting the image in it's original size.

Parameters

AbstractLocalImage|string|SplFileInfo $image

The image to be resized

string $format

The image format of the image (jpg, png, webp, ...)

string|null $seo
string $seoLanguage

Return Value

string

Exceptions

GuzzleException
RuntimeException

static string getSrcAttributes(string $url, array $sizes = ['2x'], bool $setWidthInUrl = true)

Since: 1.3.0

Returns a src and srcset attibrute (as one string) with the correct rokka render urls for responsive images.

To be used directly in your HTML templates.

Parameters

string $url

The render URL of the "non-retina" image

array $sizes

For which sizes srcset links should be generated, works with 'x' or 'w' style

bool $setWidthInUrl

If false, don't set the width as stack operation option, we provide it in $custom, usually as parameter

Return Value

string

Exceptions

RuntimeException

static string getBackgroundImageStyle(string $url, array $sizes = ['2x'])

Since: 1.3.0

Returns a background-image:url defintions (as one string) with the correct rokka render urls for responsive images.

To be used directly in your CSS templates or HTML tags.

Parameters

string $url

The render URL of the "non-retina" image

array $sizes

For which sizes srcset links should be generated, works with 'x' or 'w' style

Return Value

string

Exceptions

RuntimeException

string getImagename(AbstractLocalImage $image = null)

Since: 1.3.0

Returns the filename of the image without extension.

Parameters

AbstractLocalImage $image

Return Value

string

string generateRokkaUrl(string $hash, string|StackUri $stack, string|null $format = 'jpg', string|null $seo = null, string|null $seoLanguage = 'de')

Since: 1.3.0

Gets the rokka URL for an image hash and stack with optional seo filename in the URL.

Doesn't upload it, if we don't have a local hash for it. Use getStackUrl for that.

Parameters

string $hash

The rokka hash

string|StackUri $stack

The stack name or a StackUrl object

string|null $format

The image format of the image (jpg, png, webp, ...)

string|null $seo

If you want to use a seo string in the URL

string|null $seoLanguage

Optional language to be used for slugifying (eg. 'de' slugifies 'ö' to 'oe')

Return Value

string

Exceptions

RuntimeException

See also

TemplateHelper::getStackUrl

Image getRokkaClient()

Since: 1.3.0

Gets the rokka image client used by this class.

Return Value

Image

Exceptions

RuntimeException

static string slugify(string $text, string $language = 'de')

Since: 1.3.0

Create a URL-safe text from $text.

Parameters

string $text

Text to slugify

string $language

Optional language to be used for slugifying (eg. 'de' slugifies 'ö' to 'oe')

Return Value

string

A string that should work in urls. Empty string is only allowed if $emptyText is ''

Exceptions

RuntimeException

AbstractLocalImage getImageObject(AbstractLocalImage|string|SplFileInfo $input, string|null $identifier = null, mixed $context = null)

Since: 1.3.1

Returns a LocalImage object depending on the input.

If input is

  • LocalImageAbstract: returns that, sets $identidier and $context, if set
  • SplFileInfo: returns \Rokka\Client\LocalImage\FileInfo
  • string with hash pattern (/^[0-9a-f]{6,40}$/): returns \Rokka\Client\LocalImage\RokkaHash
  • other strings: returns \Rokka\Client\LocalImage\FileInfo with $input as the path to the image

Parameters

AbstractLocalImage|string|SplFileInfo $input
string|null $identifier
mixed $context

Return Value

AbstractLocalImage

Exceptions

RuntimeException