Resizing images | image-go | Spatie       

 SPATIE  

image-go
========

spatie.be/open-source

  [Docs](https://spatie.be/docs)  [Image-go](https://spatie.be/docs/image-go/v1)  Image-manipulations  Resizing images

 Version   v1      

 Other versions for crawler [v1](https://spatie.be/docs/image-go/v1) 

  Resizing images    
- [ Introduction ](https://spatie.be/docs/image-go/v1/introduction)
- [ Postcardware ](https://spatie.be/docs/image-go/v1/postcardware)
- [ Installation and setup ](https://spatie.be/docs/image-go/v1/installation-and-setup)
- [ Supported formats ](https://spatie.be/docs/image-go/v1/formats)
- [ Questions &amp; issues ](https://spatie.be/docs/image-go/v1/questions-and-issues)
- [ Changelog ](https://spatie.be/docs/image-go/v1/changelog)
- [ About us ](https://spatie.be/docs/image-go/v1/about-us)
- [ Upgrading ](https://spatie.be/docs/image-go/v1/upgrading)
- [ Testing ](https://spatie.be/docs/image-go/v1/testing)
- [ Releasing ](https://spatie.be/docs/image-go/v1/releasing)

Usage
-----

- [ Basic usage ](https://spatie.be/docs/image-go/v1/usage/basic-usage)
- [ Saving images ](https://spatie.be/docs/image-go/v1/usage/saving-images)
- [ Retrieving properties ](https://spatie.be/docs/image-go/v1/usage/retrieving-properties)
- [ Colors ](https://spatie.be/docs/image-go/v1/usage/colors)

Image manipulations
-------------------

- [ Overview ](https://spatie.be/docs/image-go/v1/image-manipulations/overview)
- [ Resizing images ](https://spatie.be/docs/image-go/v1/image-manipulations/resizing-images)
- [ Optimizing images ](https://spatie.be/docs/image-go/v1/image-manipulations/optimizing-images)
- [ Adjustments ](https://spatie.be/docs/image-go/v1/image-manipulations/adjustments)
- [ Image canvas ](https://spatie.be/docs/image-go/v1/image-manipulations/image-canvas)
- [ Effects ](https://spatie.be/docs/image-go/v1/image-manipulations/effects)
- [ Adding a watermark ](https://spatie.be/docs/image-go/v1/image-manipulations/adding-a-watermark)
- [ Adding text ](https://spatie.be/docs/image-go/v1/image-manipulations/text)

 Resizing images
===============

###  On this page 

1. [ Width and height ](#content-width-and-height)
2. [ Resize ](#content-resize)
3. [ Fit ](#content-fit)
4. [ Crop ](#content-crop)
5. [ Focal crop ](#content-focal-crop)
6. [ Manual crop ](#content-manual-crop)

Width and height
--------------------------------------------------------------------------------------------------------

The width and height of the image can be modified by calling `Width` and `Height` with the desired dimensions in pixels. Both preserve the original aspect ratio by default:

```
return photo.Width(250).Height(250).Save("")
```

To prevent enlargement, pass `image.PreserveAspectRatio, image.DoNotUpsize` as constraints.

Resize
--------------------------------------------------------------------------

To resize both height and width at the same time, use `Resize`. Without constraints it stretches to exact dimensions:

```
photo.Resize(250, 200)
photo.Resize(250, 200, image.PreserveAspectRatio)
```

`DoNotUpsize` prevents enlargement. Dimensions must be positive. Passing an explicit constraint list replaces the default constraints on `Width` and `Height`.

Fit
-----------------------------------------------------------------

The `Fit` method fits the image within the given width and height using a fit method:

```
photo.Fit(image.Contain, 450, 150)
```

#### `image.Contain`

Resizes to fit within the width and height boundaries without cropping, distorting or altering the aspect ratio. Allows enlargement.

#### `image.Max`

Like Contain, but will not increase the size of an image smaller than the output size.

#### `image.Fill`

Like Max, centered on a canvas of the requested dimensions. The remaining space is filled with the background color.

#### `image.Stretch`

Stretches to fit the dimensions exactly, without maintaining the aspect ratio.

#### `image.Crop`

Resizes to fill the boundaries and crops excess image data from the center. The resulting image matches the requested dimensions without distortion.

#### `image.FillMax`

Like Contain, centered on a canvas of the requested dimensions. Unlike Fill, it enlarges small images. Use `Background` to select the fill color:

```
photo.Background("#ff5733").Fit(image.FillMax, 497, 290)
```

A zero Fit dimension keeps the corresponding original dimension. Negative dimensions are rejected. To resize the canvas relatively, use `ResizeCanvas` separately.

Crop
--------------------------------------------------------------------

The `Crop` method crops to the given dimensions. Use a position to select which part to keep:

```
photo.Crop(250, 250, image.TopRight)
```

Positions are `TopLeft`, `Top`, `TopRight`, `Left`, `Center`, `Right`, `BottomLeft`, `Bottom` and `BottomRight`. Requested dimensions are clamped to the original size.

Focal crop
--------------------------------------------------------------------------------------

`FocalCrop` crops around a center in **pixels**, following the current PHP source rather than its outdated percentage description:

```
photo.FocalCrop(250, 250, 600, 400)
```

The crop is clamped to the image bounds. `FocalCropAndResize` instead takes center coordinates in **percent**, from `0` to `100`. It crops to the target aspect ratio around that point and then resizes:

```
photo.FocalCropAndResize(400, 300, 75, 25)
```

Manual crop
-----------------------------------------------------------------------------------------

`ManualCrop` crops a specific area with pixel offsets:

```
photo.ManualCrop(600, 400, 20, 620)
```

Rectangles outside the source image return an error instead of creating transparent or padded pixels.

 A good
match?
-------------

### What we do best

- All things Laravel
- Custom frontend components
- Building APIs
- AI-powered features
- Simplifying things
- Clean solutions
- Integrating services

### Not our cup of tea

- WordPress themes
- Cutting corners
- Free mockups to win a job
- "Just execute the briefing"

 In short: we'd like to be a **substantial part** of your project.

 [ Get in touch via email ](mailto:info@spatie.be?subject=A%20good%20match%21&body=Tell%20us%20as%20much%20as%20you%20can%20about%0A-%20your%20online%20project%0A-%20your%20planning%0A-%20your%20budget%0A-%20%E2%80%A6%0A%0AAnything%20that%20helps%20us%20to%20start%20straightforward%21)
