Resize Images
Change the Image Size in Different Ways
4.9M Downloads / Month
Open Source MIT License
4.9M Downloads / Month
Open Source MIT License
Discover comprehensive image resizing techniques with the Intervention Image library. Learn methods for scaling, cropping, and adjusting image canvas sizes, all while maintaining aspect ratios.
public Image::resize(null|int|Fraction $width = null, null|int|Fraction $height = null): ImageInterface
The method resize() stretches the image to the desired size without maintaining the original aspect ratio. Use
resizeDown() to change the size without exceeding the original size of the
image. The method supports named arguments
to target only one axis for the modification.
| Name | Type | Description |
|---|---|---|
| width | null, integer or Fraction |
Desired image width. |
| height | null, integer or Fraction |
Desired image height. |
use Intervention\Image\ImageManager; use Intervention\Image\Drivers\Imagick\Driver; // create new image instance $image = ImageManager::usingDriver(Driver::class) ->decode('images/example.jpg'); // resize to 300 x 200 pixel $image->resize(300, 200); // resize only image height to 200 pixel $image->resize(height: 200);
public Image::resizeDown(null|int|Fraction $width = null, null|int|Fraction $height = null): ImageInterface
The resizeDown() method does the same as resize(). It simply stretches the
image to the specified size, but does not exceed the original size of the image. You can
use named arguments
to resize just one axis.
| Name | Type | Description |
|---|---|---|
| width | null, integer or Fraction |
Desired image width. |
| height | null, integer or Fraction |
Desired image height. |
use Intervention\Image\ImageManager; use Intervention\Image\Drivers\Imagick\Driver; // create new image instance (800 x 600) $image = ImageManager::usingDriver(Driver::class)->decode('images/example.jpg'); $image = $image->resizeDown(2000, 100); // 800 x 100 // resize only image width to 200 pixel and do not exceed the original width $image->resizeDown(width: 200);
public Image::scale(null|int|Fraction $width = null, null|int|Fraction $height = null): ImageInterface
Often it's desirable to resize an image without distorting the original aspect ratio. For this kind of modification you can simply use the methods
scale() or scaleDown().
Note that the resulting size may differ from the given arguments, as the aspect ratio is preferably preserved.
| Name | Type | Description |
|---|---|---|
| width | null, integer or Fraction |
Desired image width. |
| height | null, integer or Fraction |
Desired image height. |
use Intervention\Image\ImageManager; use Intervention\Image\Drivers\Imagick\Driver; $manager = ImageManager::usingDriver(Driver::class); // create new image instance with 800 x 600 (4:3) $image = $manager->decode('images/example.jpg'); // scale to fixed height $image->scale(height: 300); // 400 x 300 (4:3) // scale to 120 x 100 pixel $image->scale(120, 100); // 120 x 90 (4:3)
public Image::scaleDown(null|int|Fraction $width = null, null|int|Fraction $height = null): ImageInterface
The method scale() resizes the image while maintaining the original aspect ratio. While
scaleDown() is similar to scale(), the only difference is that it doesn't exceed the original size of the
image. The method support named arguments
to target only one axis for the modification.
Note that the size of the result may differ from the given parameter values.
| Name | Type | Description |
|---|---|---|
| width | null, integer or Fraction |
Desired image width. |
| height | null, integer or Fraction |
Desired image height. |
use Intervention\Image\ImageManager; use Intervention\Image\Drivers\Gd\Driver; // create new image instance $manager = ImageManager::usingDriver(Driver::class); $image = $manager->decode('images/example.jpg'); // 800 x 600 // scale down to fixed width $image->scaleDown(width: 200); // 200 x 150 // scale down to fixed height $image->scaleDown(height: 300); // 400 x 300
public Image::cover(int|Fraction $width, int|Fraction $height, string|Alignment $alignment = Alignment::CENTER): ImageInterface
The cover() method is a two-step combination of cropping and resizing to
achieve a given result size. This method scales the image to the largest possible
size that fits within the given dimensions. The method then positions this size
on the original image, crops it, and resizes the result to the desired
size from the arguments.
This method requires both width and height arguments. You can optionally specify an alignment position to determine which part of the image should remain in focus.
| Name | Type | Description |
|---|---|---|
| width | integer or Fraction |
Desired image width. |
| height | integer or Fraction |
Desired image height. |
| alignment (optional) | string or Alignment |
Crop alignment. |
use Intervention\Image\ImageManager; use Intervention\Image\Drivers\Gd\Driver; use Intervention\Image\Alignment; // create new image instance (800 x 600) $manager = ImageManager::usingDriver(Driver::class); $image = $manager->decode('images/example.jpg'); // crop the best fitting 5:3 (600x360) ratio and resize to 600x360 pixel $img->cover(600, 360); // crop the best fitting 1:1 ratio (200x200) and resize to 200x200 pixel $img->cover(200, 200); // cover a size of 300x300 and position crop on the left $image->cover(300, 300, Alignment::LEFT); // 300 x 300 px
public Image::coverDown(int|Fraction $width, int|Fraction $height, string|Alignment $alignment = Alignment::CENTER): ImageInterface
This method has the same purpose and the same signature as cover() but the
final pixel size will never be larger than the original image. Use this if
you want to avoid upsampling your image.
Note that the size of the result may differ from the given parameter values.
| Name | Type | Description |
|---|---|---|
| width | integer or Fraction |
Desired image width. |
| height | integer or Fraction |
Desired image height. |
| alignment (optional) | string or Alignment |
Position |
use Intervention\Image\ImageManager; use Intervention\Image\Drivers\Imagick\Driver; use Intervention\Image\Alignment; // create new image instance $image = ImageManager::usingDriver(Driver::class)->decode('images/example.jpg'); // 800 x 600 // resize down to 1200x720 (5:3) $img->coverDown(1200, 720); // 800 x 480 (5:3) // resize down to 900x900 (1:1) $img->coverDown(900, 900); // 600 x 600 // resize down to 900x450 (2:1) and position left $image->coverDown(900, 450, Alignment::LEFT); // 800 x 400 px
public Image::contain(int|Fraction $width, int|Fraction $height, null|string|ColorInterface $background = null, string|Alignment $alignment = Alignment::CENTER): ImageInterface
Resizes the image to fit within the given dimensions while maintaining the aspect ratio. New areas are filled with the given background color.
| Name | Type | Description |
|---|---|---|
| width | integer or Fraction |
Image width. |
| height | integer or Fraction |
Image height. |
| background (optional) | null, string or ColorInterface | Background color for the new areas of the image. By default the background color from the configuration is used. |
| alignment (optional) | string | Alignment position where the original image is placed. |
use Intervention\Image\ImageManager; use Intervention\Image\Drivers\Gd\Driver; // create new image instance (800 x 600) $image = ImageManager::usingDriver(Driver::class)->decode('images/example.jpg'); // resize padded without upsizing $image->contain(900, 600); // padded resizing with gray background color $image->contain(500, 500, 'efefef');
public Image::containDown(int|Fraction $width, int|Fraction $height, null|string|ColorInterface $background = null, string|Alignment $alignment = Alignment::CENTER): ImageInterface
Resizes the image to fit within the given dimensions while maintaining the aspect ratio without exceeding the original dimensions. New areas are filled with the given background color.
| Name | Type | Description |
|---|---|---|
| width | integer or Fraction |
Image width. |
| height | integer or Fraction |
Image height. |
| background (optional) | null, string or ColorInterface | Background color for the new areas of the image. By default the background color from the configuration is used. |
| alignment (optional) | string | Alignment position where the original image is placed. |
use Intervention\Image\ImageManager; use Intervention\Image\Drivers\Imagick\Driver; use Intervention\Image\Alignment; // create new image instance $manager = ImageManager::usingDriver(new Driver()); $image = $manager->decode('images/example.jpg'); // resize padded to 300 x 200 $image->containDown(300, 200, 'ccc'); // resize padded with alignment position $image->containDown(500, 500, alignment: Alignment::TOP_LEFT);
public Image::crop(int|Fraction $width, int|Fraction $height, int $x = 0, int $y = 0, null|string|ColorInterface $background = null, string|Alignment $position = Alignment::TOP_LEFT): ImageInterface
Crops a rectangular area of a given width and height from the current image at a given position. Pass optional x, y offset coordinates to move the crop by the specified number of pixels.
You can also specify a background color. This color is used to fill any new areas that may be created, for example if the cropped area is larger than the original image format.
| Name | Type | Description |
|---|---|---|
| width | integer or Fraction |
Width of the rectangular cutout |
| height | integer or Fraction |
Height of the rectangular cutout |
| x (optional) | int | Pixel offset of the cutout on the x-axis |
| y (optional) | int | Pixel offset of the cutout on the y-axis |
| background (optional) | null, string or ColorInterface | Color to fill any newly created areas. By default the globally configured background color is used. |
| alignment (optional) | string or Alignment |
Alignment position at which the cutout will be aligned |
Caution: The signature has changed in version 3.3 by the additional parameter background
use Intervention\Image\ImageManager; use Intervention\Image\Drivers\Imagick\Driver; // create new image instance $manager = ImageManager::usingDriver(Driver::class) $image = $manager->decode('images/example.jpg'); // cut out a 200 x 150 pixel cutout at position 45,90 $image->crop(200, 150, 45, 90); // crop a 40 x 40 pixel cutout from the bottom-right and move it 30 pixel down $image->crop(200, 150, 0, 30, position: Alignment::BOTTOM_RIGHT);
public Image::resizeCanvas(null|int|Fraction $width = null, null|int|Fraction $height = null, null|string|ColorInterface $background = null, string|Alignment $alignment = Alignment::CENTER): ImageInterface
This function changes the canvas size without resampling the image. If the specified sizes are larger than the original, new area is added in the specified color. If the specified sizes are smaller, the image is cropped. The specified position is taken into account and determines where the original image is fixed.
| Name | Type | Description |
|---|---|---|
| width | null, integer or Fraction |
Width of the new image area |
| height | null, integer or Fraction |
Height of the new image area |
| background (optional) | null, string or ColorInterface |
Background color for the new areas of the image. By default the globally configured background color is used. |
| alignment (optional) | string or Alignment |
Alignment position where the original image will be fixed |
use Intervention\Image\ImageManager; use Intervention\Image\Drivers\Imagick\Driver; // create new image instance $manager = ImageManager::usingDriver(Driver::class) $image = $manager->decode('images/example.jpg'); // resize image area to 800 x 600 and fill new area with yellow $image->resizeCanvas(800, 600, 'ff0');
public Image::resizeCanvasRelative(null|int|Fraction $width = null, null|int|Fraction $height = null, null|string|ColorInterface $background = null, string|Alignment $alignment = Alignment::CENTER): ImageInterface
This function behaves the same way as resizeCanvas(), but you specify
relative values which are either added (positive) or subtracted (negative) from the original size.
| Name | Type | Description |
|---|---|---|
| width | null, integer or Fraction |
Width in pixels to be added or subtracted to the original width |
| height | null, integer or Fraction |
Height in pixels to be added or subtracted to the original height |
| background (optional) | null, string or ColorInterface |
Background color for the new areas of the image. By default the globally configured background color is used. |
| alignment (optional) | string or Alignment |
Alignment position where the original image will be fixed |
use Intervention\Image\ImageManager; use Intervention\Image\Drivers\Imagick\Driver; use Intervention\Image\Color; // create new image instance $manager = ImageManager::usingDriver(Driver::class) $image = $manager->decode('images/example.jpg'); // add 50 pixels in green at each side of the image $image->resizeCanvas(50, 50, Color::rgb(0, 255, 0)); // add 20 red pixels to the height at the bottom of the image $image->resizeCanvas(height: 20, background: 'ff0000', alignment: Alignment::BOTTOM);
All resizing methods are capable of processing relative values in addition to absolute pixel values. These can be passed as enum values from Fraction.
This makes it possible to refer to the width or height of the original image without having to calculate it yourself.
use Intervention\Image\ImageManager; use Intervention\Image\Drivers\Gd\Driver; use Intervention\Image\Fraction; // create new image instance $image = ImageManager::usingDriver(Driver::class)->decode('example.jpg'); // resize image to half width and quarter height $image->resize(Fraction::HALF, Fraction::QUARTER); // scale image to double height $image->scale(height: Fraction::DOUBLE); // cover resize with quarter image size $image->cover(Fraction::QUARTER, Fraction::QUARTER);
public Image::trim(int $tolerance = 0): ImageInterface
Removes border areas on all sides that have similar colors to the edge pixels. The
color similarity can be varied using the optional tolerance parameter.
This means that with a tolerance value of 0, only color areas with exactly
the same value will be removed. The higher the tolerance, the more similar
color areas are included. Usually values up to 20 make sense.
Note that the results can vary greatly depending on the driver and the image you are processing.
| Name | Type | Description |
|---|---|---|
| tolerance | integer | Tolerance value that determines how similar in color a border area may be in order to be removed. |
use Intervention\Image\ImageManager; use Intervention\Image\Drivers\Gd\Driver; // create new image instance $image = ImageManager::usingDriver(Driver::class)->decode('images/example.jpg'); // trim with a tolerance of 5 $image->trim(5);