Pageimage::size() method

Return an image (Pageimage) sized/cropped to the specified dimensions.

$thumb = $image->size($width, $height, $options);

The default behavior of this method is to simply create and return a new resized version of the image, leaving the original in tact. Width and height of the target size are the the first two arguments. The third argument called $options enables you to modify the default behavior of the size() method in various ways. This method only creates the newly sized image once, and then it caches it. Future calls simply refer back to the previously resized image.

About the $options argument

  • If given a string, it is assumed to be the value for the "cropping" option.
  • If given an integer, it is assumed you are specifying a "quality" value (1-100).
  • If given a boolean, it is assumed you are specifying whether or not you want to allow "upscaling".
  • If given an array, you may specify any of the options below together:

All available $options

  • quality (int): Quality setting 1-100 (default=90, or as specified in /site/config.php).
  • upscaling (bool): Allow image to be upscaled? (default=true).
  • cropping (string|bool|array): Cropping mode, see possible values in "cropping" section below (default=center).
  • suffix (string|array): Suffix word to identify the new image, or use array of words for multiple (default=none).
  • forceNew (bool): Force re-creation of the image even if it already exists? (default=false).
  • sharpening (string): Sharpening mode: "none", "soft", "medium", or "strong" (default=soft).
  • autoRotation (bool): Automatically correct rotation of images that provide this info? (default=true)
  • rotate (int): Rotate the image this many degrees, specify one of: 0, -270, -180, -90, 90, 180, or 270 (default=0).
  • flip (string): To flip, specify either "vertical" or "horizontal" (default=none).
  • hidpi (bool): Use HiDPI/retina pixel doubling? (default=false).
  • hidpiQuality (bool): Quality setting for HiDPI (default=40, typically lower than regular quality setting).
  • cleanFilename (bool): Clean filename of historical resize information for shorter filenames? (default=false).
  • nameWidth (int): Width to use for filename (default is to use specified $width argument).
  • nameHeight (int): Height to use for filename (default is to use specified $height argument).
  • focus (bool): Should resizes that result in crop use focus area if available? (default=true). In order for focus to be applicable, resize must include both width and height.
  • allowOriginal (bool): Return original if already at width/height? May not be combined with other options. (default=false)

Possible values for "cropping" option

  • true (bool): Auto detect and allow use of focus (default).
  • false (bool): Disallow cropping.
  • center (string): to crop to center of image.
  • x111y222 (string): to crop by pixels, 111px from left and 222px from top (replacing 111 and 222 with your values).
  • north (string): Crop North (top), may also be just "n".
  • northwest (string): Crop from Northwest (top left), may also be just "nw".
  • northeast (string): Crop from Northeast (top right), may also be just "ne".
  • south (string): Crop South (bottom), may also be just "s".
  • southwest (string): Crop Southwest (bottom left), may also be just "sw".
  • southeast (string): Crop Southeast (bottom right), may also be just "se".
  • west (string): Crop West (left), may also be just "w".
  • east (string): Crop East (right), may alos be just "e".
  • blank (string): Specify a blank string to disallow cropping during resize.
  • array(111,222) (array): Array of integers index 0 is left pixels and index 1 is top pixels.
  • array('11%','22%') (array): Array of '%' appended strings where index 0 is left percent and index 1 is top percent.

Note about "quality" and "upscaling" options

ProcessWire doesn't keep separate copies of images with different "quality" or "upscaling" values. If you change these and a variation image at the existing dimensions already exists, then you'll still get the old version. To clear out an old version of an image, use the Pageimage::removeVariations() method in this class before calling size() with new quality or upscaling settings.

Example

// Get an image to resize
$image = $page->images->first();

// Create 400x300 thumbnail cropped to center
$thumb = $image->size(400, 300);

// Create thumbnail with cropping to top
$thumb = $image->size(400, 300, 'north');

// Create thumbnail while specifying $options
$thumb = $image->size(400, 300, [
  'cropping' => 'north',
  'quality' => 60,
  'upscaling' => false,
  'sharpening' => 'medium'
]);

// Output thumbnail
echo "<img src='$thumb->url' />";

Usage

// basic usage
$pageimage = $pageimage->size(int $width, int $height);

// usage with all arguments
$pageimage = $pageimage->size(int $width, int $height, $options = []);

Arguments

NameType(s)Description
widthint

Target width of new image

heightint

Target height of new image

options (optional)array, string, int

Array of options to override default behavior:

  • Specify array of options as indicated in the section above.
  • Or you may specify type string containing "cropping" value.
  • Or you may specify type int containing "quality" value.
  • Or you may specify type bool containing "upscaling" value.

Return value

Pageimage

Returns a new Pageimage object that is a variation of the original. If the specified dimensions/options are the same as the original, then the original then the original will be returned.


Pageimage methods and properties

API reference based on ProcessWire core version 3.0.124

Twitter updates

  • New post: In this week’s post, we’ll take a look a look at the new website and focus on some parts of it and how they were built. Then we’ll dive into the latest version of ProcessWire on the dev branch, version 3.0.124— More
    11 January 2019
  • New ProcessWire main site rebuild ready for preview— More
    4 January 2019
  • Happy Holidays! This latest master version of ProcessWire contains hundreds of new additions, fixes, optimizations and more. This post covers all the highlights— More
    21 December 2018

Latest news

  • ProcessWire Weekly #245
    In the 245th issue of ProcessWire Weekly we're going to focus on some new documentation additions made just this week, cover a couple of new third party modules from Bernhard Baumrock, and introduce a magnificent new travel website called Uncover Colombia. Read on!
    Weekly.pw / 19 January 2019
  • ProcessWire 3.0.124 and new website launched
    In this week’s post, we’ll take a look a look at the new website and focus on some parts of it and how they were built. Then we’ll dive into the latest version of ProcessWire on the dev branch, version 3.0.124.
    Blog / 11 January 2019
  • Subscribe to weekly ProcessWire news

“We were really happy to build our new portfolio website on ProcessWire! We wanted something that gave us plenty of control on the back-end, without any bloat on the front end - just a nice, easy to access API for all our content that left us free to design and build however we liked.” —Castus, web design agency in Sheffield, UK