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)
  • webpAdd (bool): Also create a secondary .webp image variation? (default=false)
  • webpQuality (int): Quality setting for extra webp images (default=90).

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' />";

// Create image of size predefined in $config->imageSizes (3.0.151+)
$photo = $image->size('landscape'); 

Usage

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

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

Arguments

NameType(s)Description
widthint, string

Target width of new image or (3.0.151+) specify prefined image size name

height (optional)int, array

Target height of new image or (3.0.151+) options array if no height argument needed

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 will be returned.


Pageimage methods and properties

API reference based on ProcessWire core version 3.0.185

Twitter updates

  • ProcessWire 3.0.192 (dev branch) contains 11 issue fixes and 5 pull requests, over 20 commits— More
    7 January 2022
  • ProcessWire 3.0.191 core updates: updates to the installer and the profile exporter module— More
    24 December 2021
  • This week ProcessWire went on a diet, trimming the core size from 15.5 MB down to 7.5 MB by moving optional site profiles into their own GitHub repos— More
    17 December 2021

Latest news

  • ProcessWire Weekly #402
    In the 402nd issue of ProcessWire Weekly we'll check out ProcessWire 3.0.193, highlight recent ProcessWire projects and resources, and more. Read on!
    Weekly.pw / 22 January 2022
  • ProcessWire 3.0.190 core updates
    ProcessWire 3.0.190 has 15 commits relative to 3.0.189 and contains a mixture of issue resolutions and feature additions. This post covers them all, in addition to updates for the PageAutosave and ProFields Table modules.
    Blog / 3 December 2021
  • 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