Basic usage | image | Spatie

 SPATIE

  Image
========

spatie.be/open-source

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

 Version   v3   v2   v1

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

  Basic usage
- [ Introduction ](https://spatie.be/docs/image/v3/introduction)
- [ Postcardware ](https://spatie.be/docs/image/v3/postcardware)
- [ Installation and setup ](https://spatie.be/docs/image/v3/installation-and-setup)
- [ Supported formats ](https://spatie.be/docs/image/v3/formats)
- [ Questions &amp; issues ](https://spatie.be/docs/image/v3/questions-and-issues)
- [ Changelog ](https://spatie.be/docs/image/v3/changelog)
- [ About us ](https://spatie.be/docs/image/v3/about-us)
- [ Upgrading ](https://spatie.be/docs/image/v3/upgrading)

Usage
-----

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

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

- [ Overview ](https://spatie.be/docs/image/v3/image-manipulations/overview)
- [ Resizing images ](https://spatie.be/docs/image/v3/image-manipulations/resizing-images)
- [ Optimizing images ](https://spatie.be/docs/image/v3/image-manipulations/optimizing-images)
- [ Adjustments ](https://spatie.be/docs/image/v3/image-manipulations/adjustments)
- [ Image canvas ](https://spatie.be/docs/image/v3/image-manipulations/image-canvas)
- [ Effects ](https://spatie.be/docs/image/v3/image-manipulations/effects)
- [ Adding a watermark ](https://spatie.be/docs/image/v3/image-manipulations/adding-a-watermark)
- [ Adding text ](https://spatie.be/docs/image/v3/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 the static `load` method on the `Image` and passing in the `$pathToImage`.

```
use Spatie\Image\Image;

$image = Image::load(string $pathToImage);
```

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

By default, the Imagick driver will be used. The package supports three drivers: **Imagick**, **GD**, and **Vips**.

```
use Spatie\Image\Image;
use Spatie\Image\Enums\ImageDriver;

// Use GD
Image::useImageDriver(ImageDriver::Gd)->loadFile(string $pathToImage);

// Use Vips (requires libvips and jcupitt/vips package)
Image::useImageDriver(ImageDriver::Vips)->loadFile(string $pathToImage);
```

To use the Vips driver, you need to have [libvips](https://www.libvips.org/) installed on your system and require the `jcupitt/vips` package:

```
composer require jcupitt/vips
```

It's also possible to pass an implementation of `ImageDriver` directly. Build your own driver from scratch, or extend one of the provided drivers (`ImagickDriver`, `GdDriver`, or `VipsDriver`).

```
use Spatie\Image\Image;

Image::useImageDriver(MyDriver::class)->loadFile(string $pathToImage);
```

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

Any of the [image manipulations](/docs/image/v3/image-manipulations/overview) can be applied to the loaded `Image` by calling the manipulation's method. All image manipulation methods can be chained.

```
use Spatie\Image\Image;

Image::load('example.jpg')
    ->sepia()
    ->blur(50)
    ->save();
```

![Sepia + blur manipulation](../images/example-sepia-blur.jpg)

Every manipulation you call will be applied. When calling a manipulation method multiple times each call will be applied immediately.

```
use Spatie\Image\Image;

// This will lower the brightness first by 40% and then by 20%
Image::load('example.jpg')
    ->brightness(-40)
    ->brightness(-20)
    ->save();
```

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

Calling the `save` method on an `Image` will save the modifications to the specified file.

```
use Spatie\Image\Image;

Image::load('example.jpg')
    ->width(50)
    ->save('modified-example.jpg');
```

To save the image in a different image format or with a different jpeg quality [see saving images](/docs/image/v3/usage/saving-images).

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

Calling the `base64` method on an `Image` will return a base64 string of the image.

```
use Spatie\Image\Image;

Image::load('example.jpg')
    ->base64();
```

By default the base64 string will be formatted as a jpeg and will include the mime type. You can alter this by passing extra parameters:

```
Image::load('example.jpg')
    ->base64('jpeg', $prefixWithFormat = false);
```

 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)
