JYAML::getImage

In JYAML 4.x is a PHP-Class for image processing built-in. So you can for example simply read only a unknown image size, resize or cut images.

The PHP-Class is extended from the PHPThumb class.

The following image formats are supported:

  • png
  • jpg
  • gif

JYAML::getImage Reference

Syntax: $image = JYAML::getImage(string $src, string $alt, array $options, string $attribs)

Parameters:

  • string $src: Image path (absolute or relative from the Joomla! root directory)
  • string $alt: <img> Alt Attribute
  • array $options: PhpThumb Options
  • string $attribs: Additional <img> Attributes (e.g. 'onclick="alert('hello world')" class="myClass"')

Return: object JYAMLImage: Reference object for further processing.

Image processing functions

  • $image->resize(int width [, int height])
    The proportions are not changed. It will automatically detect the largest possible aspect ratio of width to height determined. The height is optional.
  • $image->resizePercent(int percent)
    Percentage sizing
  • $image->adaptiveResize(int width, int height)
    Resize to the exact size specified on width and height. The image is croped, if necessary. The orientation of the cut is centered.
  • $image->cropFromCenter(int width[, int height])
    Cut the image to a specific size. The orientation of the cut is centered. The height is optional.
  • $image->crop(int x-position, int y-position, int width, int height)
    Cut the image to a specific size. The x and y position is the start position (coordinates), since where the image should be cropped.
  • $image->rotateImageNDegrees(int degree)
    Creates an image rotation by a certain degree value. Negative and positive numbers are allowed.
  • $image->rotateImage([string direction])
    Wrapper function of rotateImageNDegrees: Produces an image with an rotation of +90° (direction = 'CW') or -90° (direction is not specified)
  • $image->setFormat([string png, jpg or gif])
    Converts the image into another format. Supported are png, jpg and gif

Examples (PHPThumb Wiki): https://github.com/masterexploder/PHPThumb/wiki/Basic-Usage

Output functions

  • $image->toObject()
    Return: <img> Attributes as JRegistry object.
    Can be called after each image processing function. E.g. useful to examine a specific state of width or height before further processing.
  • $image->toArray()
    Return: Like toObject but as PHP-Array
    Can be called after each image processing function. E.g. useful to examine a specific state of width or height before further processing.
  • $image->toHtml() oder $image->toString()
    Return: XHTML <img> Tag with all attributes as string

All functions (methods) can be called with chaining.
Example: echo JYAML::getImage(...)->resize(...)->crop(...)->toHtml()

PhpThumb Options

Option
Description Default-Value Valid Values
resizeUp Determines whether an image that is smaller than a specified size adjustment should be increased. false true / false
jpegQuality JPEG format quality (compression, 100=without) 100 1-100
preserveAlpha Determines whether the alpha transparency channel in PNG format to be preserved true true / false
preserveTransparency Determines whether the transparency in GIF format should be preserved true true / false

Example

Here is an example of how can display a image with a specific size, e.g. the logo-image of the template settings.

<?php
if($_logoImge = trim($this->params->get('logo_image', ''))) {
  $_img = JYAML::getImage(
    $this->getUrl('root', $_logoImge), // image src
    JFactory::getApplication()->getCfg('sitename'), // image alt
    array('resizeUp' => true), // Thumbnailer options
    'class="site-logo"' // additional attributes
  );
  echo $_img->resize(400, 100)->toHtml(); // output image with best-of size within 400px x 100px
}
?>