resize — Resize current image

Description

public Intervention\Image\Image resize (integer $width, integer $height, [Closure $callback])

Resizes current image based on given width and/or height. To constraint the resize command, pass an optional Closure callback as third parameter.

Parameters

width

The new width of the image

height

The new height of the image

callback (optional)

Closure callback defining constraints on the resize. It’s possible to constraint the aspect-ratio and/or a unwanted upsizing of the image. See examples below.

aspectRatio

public Intervention\Image\Size aspectRatio()

Constraint the current aspect-ratio of the image. As a shortcut to proportional resizing you can use widen() or heighten().

upsize

public Intervention\Image\Size upsize()

Keep image from being upsized.

Return Values

Instance of Intervention\Image\Image

Examples

  1. // create instance
  2. $img = Image::make('public/foo.jpg');
  3. // resize image to fixed size
  4. $img->resize(300, 200);
  5. // resize only the width of the image
  6. $img->resize(300, null);
  7. // resize only the height of the image
  8. $img->resize(null, 200);
  9. // resize the image to a width of 300 and constrain aspect ratio (auto height)
  10. $img->resize(300, null, function ($constraint) {
  11. $constraint->aspectRatio();
  12. });
  13. // resize the image to a height of 200 and constrain aspect ratio (auto width)
  14. $img->resize(null, 200, function ($constraint) {
  15. $constraint->aspectRatio();
  16. });
  17. // prevent possible upsizing
  18. $img->resize(null, 400, function ($constraint) {
  19. $constraint->aspectRatio();
  20. $constraint->upsize();
  21. });
  22. // resize the image so that the largest side fits within the limit; the smaller
  23. // side will be scaled to maintain the original aspect ratio
  24. $img->resize(400, 400, function ($constraint) {
  25. $constraint->aspectRatio();
  26. $constraint->upsize();
  27. });

See also