JYAML::getImage

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

This PHP-Image-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

Plugins

  • $image->createWatermark(string absolute path to image[, string position, int padding])
    Overlayer on the image with a watermark. The path to the image must be absolute. The watermark can have the positions center ('cc', default), left top ('lt'), right top ('rt'), left bottom ('lb'), right bottom ('rb') center top ('ct ') and center bottom (' cb '). Position and padding are optional.
  • $image->createReflection([int percent, int reflection, int opacity, boolean border, string borderColor])
    All arguments are optional.
    Percent: Determines what percentage of the reflected image to be (default '40 ').
    Reflection: Determines the amount of reflection as percentage (relative to the original image height) (default '40 ').
    Opacity: Sets the opacity (based on a white background) in percent (default '80 ').
    Border: Specifies whether a border should be drawn around the original image (default false).
    Border Color: The color of the border (if enabled) in HEX-Color code (default '# a4a4a4').

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

OptionDescriptionDefault-ValueValid 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
}
?>