Basic usage | image-go | Spatie       

 SPATIE  

image-go
========

spatie.be/open-source

  [Docs](https://spatie.be/docs)  [Image-go](https://spatie.be/docs/image-go/v1)  Usage  Basic usage

 Version   v1      

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

  Basic usage    
- [ 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)

 Basic usage
===========

###  On this page 

1. [ Loading the image ](#content-loading-the-image)
2. [ Selecting a driver ](#content-selecting-a-driver)
3. [ Applying manipulations ](#content-applying-manipulations)
4. [ Saving the image ](#content-saving-the-image)
5. [ Retrieve a base64 string ](#content-retrieve-a-base64-string)

Loading the image
-----------------------------------------------------------------------------------------------------------

Load an image by calling `Load` and passing in the local path:

```
photo, err := image.Load(ctx, "example.jpg")
if err != nil {
    return err
}
```

Use `FromBytes(ctx, data)` to decode bytes. Both apply EXIF orientation automatically. Use `New(ctx, width, height, "none")` to create a transparent canvas, or pass a background color.

Selecting a driver
--------------------------------------------------------------------------------------------------------------

By default, the ImageMagick 7 driver will be used:

```
photo, err := image.Load(ctx, "example.jpg",
    image.WithDriver(image.MagickDriver{Binary: "/usr/local/bin/magick"}),
)
```

Implement `Driver.Execute(context.Context, []byte, ...string) ([]byte, error)` to replace execution. Input is provided on stdin, output is returned as bytes, and arguments follow ImageMagick syntax. You can embed `MagickDriver` to add logging, limits or sandbox execution. The same driver handles loading, manipulation, text, pixel reads and encoding. No global driver registry is used.

Applying manipulations
--------------------------------------------------------------------------------------------------------------------------

Any of the [image manipulations](/docs/image-go/v1/image-manipulations/overview) can be applied to the loaded `Image`. All manipulation methods can be chained:

```
return photo.Sepia().Blur(50).Save("")
```

Every manipulation runs in call order. Calling a manipulation more than once applies it more than once:

```
return photo.Brightness(-40).Brightness(-20).Save("")
```

The first error stops subsequent processing and is returned by `Err()`, `Save` or readers such as `Size`. Instances are mutable and not safe for concurrent access. Use `Clone()` for an independently editable copy. Overlays and watermarks are not mutated. The driver and optimizer configuration are shared by clones, so custom implementations must follow your application's concurrency requirements.

Use `Transform` for additional trusted ImageMagick operations, for example `photo.Transform("-negate")`.

Saving the image
--------------------------------------------------------------------------------------------------------

Calling `Save` saves the modifications to the specified file:

```
return photo.Width(50).Save("modified-example.jpg")
```

Pass an empty path to overwrite the original file. A new canvas or byte-loaded image needs an explicit path.

Retrieve a base64 string
--------------------------------------------------------------------------------------------------------------------------------

Calling `Base64` returns a base64 string in the requested format:

```
encoded, err := photo.Base64("jpeg", true)
```

The second argument controls whether the string includes a data URI prefix. `Encode("png")` returns raw encoded bytes. Neither runs the file optimizer.

 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)
