Pageimage::render() method

Render markup for this image (optionally using a provided markup template string and/or image size options)

Given template string can contain any of the placeholders, which will be replaced:

  • {url} or {src} Image URL (typically used for src attribute)
  • {httpUrl} File URL with scheme and hostname (alternate for src attribute)
  • {URL} Same as url but with cache busting query string
  • {HTTPURL} Same as httpUrl but with cache busting query string
  • {description} or {alt} Image description (typically used in alt attribute)
  • {tags} File tags (might be useful in class attribute)
  • {width} Width of image
  • {height} Height of image
  • {hidpiWidth} HiDPI width of image
  • {hidpiHeight} HiDPI height of image
  • {ext} File extension
  • {original.name} Replace “name” with any of the properties above to refer to original image. If there is no original image then these just refer back to the current image.

Available since version 3.0.126.

Example

$image = $page->images->first();
if($image) {
  // default output
  echo $image->render();

  // custom output
  echo $image->render("<img class='pw-image' src='{url}' alt='{alt}'>");

  // custom output with options
  echo $image->render("<img src='{url}' alt='{alt}'>", [ 'width' => 300 ]);

  // options can go in first argument if you prefer
  echo $image->render([ 'width' => 300, 'height' => 200 ]);

  // if only width/height are needed, they can also be specified as a string (1st or 2nd arg)
  echo $image->render('300x200');

  // custom output with link to original/full-size and square crop of 300x300 for thumbnail
  echo $image->render([
    'markup' => "<a href='{original.url}'><img src='{url}' alt='{alt}'></a>",
    'width' => 300,
    'height' => 300
  ]);
}

Usage

// basic usage
$string = $pageimage->render();

// usage with all arguments
$string = $pageimage->render($markup = '', $options = []);

Arguments

NameType(s)Description
markup (optional)string, array

Markup template string or optional $options array if you do not want the template string here.

options (optional)array, string

Optionally resize image with these options sent to size() method:

  • width (int): Target width or 0 for current image size (or proportional if height specified).
  • height (int): Target height or 0 for current image size (or proportional if width specified).
  • markup (string): Markup template string (same as $markup argument), or omit for default (same as $markup argument).
  • link (bool): Link image to original size? Though you may prefer to do this with your own $markup (see examples). (default=false)
  • alt (string): Text to use for “alt” attribute (default=text from image description).
  • class (string): Text to use for “class” attribute, if {class} present in markup (default='').
  • Plus any option available to the $options argument on the Pageimage::size() method.
  • If you only need width and/or height, you can specify a width x height string, i.e. 123x456 (use 0 for proportional).

Return value

string


Hooking Pageimage::render(…)

You can add your own hook events that are executed either before or after the Pageimage::render(…) method is executed. Examples of both are included below. A good place for hook code such as this is in your /site/ready.php file.

Hooking before

The 'before' hooks are called immediately before each Pageimage::render(…) method call is executed. This type of hook is especially useful for modifying arguments before they are sent to the method.

$this->addHookBefore('Pageimage::render', function(HookEvent $event) {
  // Get the object the event occurred on, if needed
  $Pageimage = $event->object;

  // Get values of arguments sent to hook (and optionally modify them)
  $markup = $event->arguments(0);
  $options = $event->arguments(1);

  /* Your code here, perhaps modifying arguments */

  // Populate back arguments (if you have modified them)
  $event->arguments(0, $markup);
  $event->arguments(1, $options);
});

Hooking after

The 'after' hooks are called immediately after each Pageimage::render(…) method call is executed. This type of hook is especially useful for modifying the value that was returned by the method call.

$this->addHookAfter('Pageimage::render', function(HookEvent $event) {
  // Get the object the event occurred on, if needed
  $Pageimage = $event->object;

  // An 'after' hook can retrieve and/or modify the return value
  $return = $event->return;

  // Get values of arguments sent to hook (if needed)
  $markup = $event->arguments(0);
  $options = $event->arguments(1);

  /* Your code here, perhaps modifying the return value */

  // Populate back return value, if you have modified it
  $event->return = $return;
});

See Also


Pageimage methods and properties

API reference based on ProcessWire core version 3.0.155

Twitter updates

  • This post covers a few of the bigger updates in ProcessWire 3.0.154+3.0.155. This includes new live replacement of text in core and modules, a new method for creating canonical URLs, and some major upgrades to our input->urlSegment() method! More
    24 April 2020
  • A brief look at what's new in ProcessWire 3.0.154 in this forum post: More
    17 April 2020
  • This week we’ve got a few new and interesting core updates in progress, though none quite ready to release just yet. So rather than releasing version 3.0.154 today, I thought we'd instead take a brief look at what’s coming over the next few weeks… More
    3 April 2020

Latest news

  • ProcessWire Weekly #311
    In the 311th issue of ProcessWire Weekly we're going to talk a bit about the latest dev versions of ProcessWire, introduce a couple of new third party modules, and more. Read on!
    Weekly.pw / 25 April 2020
  • ProcessWire 3.0.154 and 3.0.155 core updates
    This post covers a few of the bigger updates in ProcessWire 3.0.154 and 3.0.155 on the dev branch. This includes a new function for live replacement of text in core and modules, a new method for creating canonical URLs, and some major upgrades to our $input->urlSegment() method that I think you’ll like!
    Blog / 24 April 2020
  • Subscribe to weekly ProcessWire news

I just love the easy and intuitive ProcessWire API. ProcessWire rocks!” —Jens Martsch, Web developer