You can resize images by passing a few parameters to the resize() method. The first two are the new dimensions of the image, and can be smart coordinate values. If one dimension isn’t specified (or null is given), it’s calculated from the ratio of the other dimension.

Resize an image into a 400×300 box. By default, resizing keeps the original image’s aspect ratio and the resulting image fits the given dimensions from the inside.

  1. $resized = $image->resize(400, 300);

This is equal to passing ‘inside’ as $fit value.

  1. $resized = $image->resize(400, 300, 'inside');

Resize an image to fit a 400×300 box from the outside by passing ‘outside’ to $fit parameter. This means that the image will be at least as big as 400×300, and aspect ratio will be kept.

  1. $resized = $image->resize(400, 300, 'outside');

Resize an image to exactly fit a 400×300 box by passing ‘fill’ as the value of $fit parameter. The image will be stretched as necessary, aspect ratio may not be kept.

  1. $resized = $image->resize(400, 300, 'fill');

The fourth parameter ($scale) determines when to scale an image. Possible values include any (default), down and up:

  • down – resize if image is larger than the new dimensions
  • up – resize if image is smaller than the new dimensions
  • any – resize regardless of the image size

There are two aliases for the resize method: resizeUp and resizeDown. These two are equal to calling resize() with $scale = ‘up’ and $scale = ‘down’ respectively.

  1. $resized = $image->resize(350, 500, 'inside', 'down');
  2. // is equal to
  3. $resized = $image->resizeDown(350, 500, 'inside');

See the resize demo.

Note: WideImage can also resize the canvas only, without stretching the actual image. See resizeCanvas demo and documentation.

Share and Enjoy:
  • Digg
  • Facebook
  • Google Bookmarks
  • Reddit
  • Tumblr
  • Twitter