class Image implements Responsable, Stringable

Traits

Properties

static protected array $macros

The registered string macros.

from  Macroable
protected ImageSource $source

The original image source.

protected ImagePipeline $pipeline

The image processing pipeline.

protected string|null $driver

The driver override.

protected string|null $processedContents

The processed image contents.

protected string|null $mimeType

The cached MIME type.

protected null|array{0: int, 1: int} $dimensions

The cached image dimensions.

protected string|null $dominantColor

The cached dominant color.

protected string|null $hashName

The cached hash name.

Methods

mixed
when(mixed $value = null, callable|null $callback = null, callable|null $default = null)

Apply the callback if the given "value" is (or resolves to) truthy.

mixed
unless(mixed $value = null, callable|null $callback = null, callable|null $default = null)

Apply the callback if the given "value" is (or resolves to) falsy.

static void
macro(string $name, callable|object $macro)

Register a custom macro.

static void
mixin(object $mixin, bool $replace = true)

Mix another object into the class.

static bool
hasMacro(string $name)

Check if macro is registered.

static void
flushMacros()

Flush the existing macros.

static mixed
__callStatic(string $method, array $parameters)

Dynamically handle calls to the class.

mixed
__call(string $method, array $parameters)

Dynamically handle calls to the class.

__construct(Closure|string $contents, UploadedFile|null $file = null)

Create a new image instance.

cover(int $width, int $height)

Set the cover dimensions.

contain(int $width, int $height, string|null $background = null)

Set the contain dimensions.

crop(int $width, int $height, int $x = 0, int $y = 0)

Crop the image to the given dimensions and position.

resize(int|null $width = null, int|null $height = null)

Resize the image to the given dimensions.

rotate(float $angle, string|null $background = null)

Rotate the image clockwise by the given angle.

scale(int|null $width = null, int|null $height = null)

Set the scale dimensions.

void
ensureValidDimensions(int|null $width, int|null $height)

Ensure the image dimensions are valid.

orient()

Auto-orient the image based on EXIF data.

blur(int $amount = 5)

Apply a blur effect.

grayscale()

Convert the image to grayscale.

sharpen(int $amount = 10)

Sharpen the image.

flipVertically()

Flip the image vertically.

flipHorizontally()

Flip the image horizontally.

flip()

Flip the image vertically.

flop()

Flip the image horizontally.

transform(Transformation $transformation)

Add a transformation to the image pipeline.

optimize(string $format = 'webp', int $quality = ImageOutputOptions::DEFAULT_QUALITY)

Set the optimization options.

quality(int $quality)

Set the output quality.

void
ensureValueIsBetween(string $name, int $value, int $minimum, int $maximum)

Ensure the value is within the given range.

toWebp()

Convert the image to WebP format.

toJpg()

Convert the image to JPEG format.

toJpeg()

Convert the image to JPEG format.

toPng()

Convert the image to PNG format.

toGif()

Convert the image to GIF format.

toAvif()

Convert the image to AVIF format.

toHeic()

Convert the image to HEIC format.

toBmp()

Convert the image to BMP format.

toFormat(string $format)

Set the output format.

string|false
store(string $path = '', UnitEnum|string|null $disk = null, array $options = [])

Store the processed image on a filesystem disk.

string|false
storePublicly(string $path = '', UnitEnum|string|null $disk = null, array $options = [])

Store the processed image on a filesystem disk with public visibility.

string|false
storeAs(string $path, string|null $name = null, UnitEnum|string|null $disk = null, array $options = [])

Store the processed image on a filesystem disk with a given name.

string|false
storePubliclyAs(string $path, string|null $name = null, UnitEnum|string|null $disk = null, array $options = [])

Store the processed image on a filesystem disk with public visibility and a given name.

string
hashName(string $path = '')

Get a hashed filename with the correct extension.

string
toBytes()

Process the image and return the raw bytes.

string
process()

Process the image recipe.

string
toBase64()

Process the image and return as a base64 encoded string.

string
toDataUri()

Process the image and return as a data URI.

string
extension()

Get the file extension based on the MIME type.

string
mimeType()

Get the MIME type of the processed image.

array
dimensions()

Get the dimensions of the processed image.

int
width()

Get the width of the processed image.

int
height()

Get the height of the processed image.

string
dominantColor()

Get the dominant (average) color of the image as a hex string.

using(string $driver)

Set the driver to use for processing.

usingGd()

Use the GD driver for processing.

usingImagick()

Use the Imagick driver for processing.

resolveDriver()

Resolve the image processing driver.

file()

Get the underlying uploaded file instance.

void
__clone()

Clone the pipeline and reset all derived state.

newClone()

Create an immutable clone with copied options.

withOutput(Closure $callback)

Create an immutable clone with updated output options.

withClone(Closure $callback)

Create an immutable clone with the given callback applied.

Response
toResponse(Request $request)

Create an HTTP response that represents the image.

never
__serialize()

Prevent serialization of the image.

string
toString()

Get the string representation of the image.

string
__toString()

Get the string representation of the image.

static void
flushState()

Flush all static state.

Details

in Conditionable at line 23
mixed when(mixed $value = null, callable|null $callback = null, callable|null $default = null)

Apply the callback if the given "value" is (or resolves to) truthy.

Parameters

mixed $value
callable|null $callback
callable|null $default

Return Value

mixed

in Conditionable at line 56
mixed unless(mixed $value = null, callable|null $callback = null, callable|null $default = null)

Apply the callback if the given "value" is (or resolves to) falsy.

Parameters

mixed $value
callable|null $callback
callable|null $default

Return Value

mixed

in Macroable at line 28
static void macro(string $name, callable|object $macro)

Register a custom macro.

Boot-only. Macros persist in a static property for the worker lifetime and apply to every subsequent call on the macroable class.

Parameters

string $name
callable|object $macro

Return Value

void

in Macroable at line 41
static void mixin(object $mixin, bool $replace = true)

Mix another object into the class.

Boot-only. Delegates to macro() for each method; registered macros persist in a static property for the worker lifetime.

Parameters

object $mixin
bool $replace

Return Value

void

Exceptions

ReflectionException

in Macroable at line 57
static bool hasMacro(string $name)

Check if macro is registered.

Parameters

string $name

Return Value

bool

in Macroable at line 68
static void flushMacros()

Flush the existing macros.

Boot or tests only. Clears worker-wide macros; concurrent coroutines may resolve different methods depending on timing.

Return Value

void

in Macroable at line 78
static mixed __callStatic(string $method, array $parameters)

Dynamically handle calls to the class.

Parameters

string $method
array $parameters

Return Value

mixed

Exceptions

BadMethodCallException

in Macroable at line 103
mixed __call(string $method, array $parameters)

Dynamically handle calls to the class.

Parameters

string $method
array $parameters

Return Value

mixed

Exceptions

BadMethodCallException

at line 86
__construct(Closure|string $contents, UploadedFile|null $file = null)

Create a new image instance.

Parameters

Closure|string $contents
UploadedFile|null $file

at line 102
Image cover(int $width, int $height)

Set the cover dimensions.

Parameters

int $width
int $height

Return Value

Image

Exceptions

ImageException

at line 117
Image contain(int $width, int $height, string|null $background = null)

Set the contain dimensions.

Parameters

int $width
int $height
string|null $background

Return Value

Image

Exceptions

ImageException

at line 132
Image crop(int $width, int $height, int $x = 0, int $y = 0)

Crop the image to the given dimensions and position.

Parameters

int $width
int $height
int $x
int $y

Return Value

Image

Exceptions

ImageException

at line 147
Image resize(int|null $width = null, int|null $height = null)

Resize the image to the given dimensions.

Parameters

int|null $width
int|null $height

Return Value

Image

Exceptions

ImageException

at line 161
Image rotate(float $angle, string|null $background = null)

Rotate the image clockwise by the given angle.

Parameters

float $angle
string|null $background

Return Value

Image

at line 174
Image scale(int|null $width = null, int|null $height = null)

Set the scale dimensions.

Parameters

int|null $width
int|null $height

Return Value

Image

Exceptions

ImageException

at line 190
protected void ensureValidDimensions(int|null $width, int|null $height)

Ensure the image dimensions are valid.

Parameters

int|null $width
int|null $height

Return Value

void

Exceptions

ImageException

at line 204
Image orient()

Auto-orient the image based on EXIF data.

Return Value

Image

at line 216
Image blur(int $amount = 5)

Apply a blur effect.

Parameters

int $amount

Return Value

Image

Exceptions

ImageException

at line 226
Image grayscale()

Convert the image to grayscale.

Return Value

Image

at line 238
Image sharpen(int $amount = 10)

Sharpen the image.

Parameters

int $amount

Return Value

Image

Exceptions

ImageException

at line 248
Image flipVertically()

Flip the image vertically.

Return Value

Image

at line 256
Image flipHorizontally()

Flip the image horizontally.

Return Value

Image

at line 264
Image flip()

Flip the image vertically.

Return Value

Image

at line 272
Image flop()

Flip the image horizontally.

Return Value

Image

at line 280
Image transform(Transformation $transformation)

Add a transformation to the image pipeline.

Parameters

Transformation $transformation

Return Value

Image

at line 292
Image optimize(string $format = 'webp', int $quality = ImageOutputOptions::DEFAULT_QUALITY)

Set the optimization options.

Parameters

string $format
int $quality

Return Value

Image

Exceptions

ImageException

at line 304
Image quality(int $quality)

Set the output quality.

Parameters

int $quality

Return Value

Image

Exceptions

ImageException

at line 316
protected void ensureValueIsBetween(string $name, int $value, int $minimum, int $maximum)

Ensure the value is within the given range.

Parameters

string $name
int $value
int $minimum
int $maximum

Return Value

void

Exceptions

ImageException

at line 326
Image toWebp()

Convert the image to WebP format.

Return Value

Image

at line 334
Image toJpg()

Convert the image to JPEG format.

Return Value

Image

at line 342
Image toJpeg()

Convert the image to JPEG format.

Return Value

Image

at line 350
Image toPng()

Convert the image to PNG format.

Return Value

Image

at line 358
Image toGif()

Convert the image to GIF format.

Return Value

Image

at line 366
Image toAvif()

Convert the image to AVIF format.

Return Value

Image

at line 374
Image toHeic()

Convert the image to HEIC format.

Return Value

Image

at line 382
Image toBmp()

Convert the image to BMP format.

Return Value

Image

at line 392
Image toFormat(string $format)

Set the output format.

Parameters

string $format

Return Value

Image

Exceptions

ImageException

at line 408
string|false store(string $path = '', UnitEnum|string|null $disk = null, array $options = [])

Store the processed image on a filesystem disk.

Parameters

string $path
UnitEnum|string|null $disk
array $options

Return Value

string|false

at line 418
string|false storePublicly(string $path = '', UnitEnum|string|null $disk = null, array $options = [])

Store the processed image on a filesystem disk with public visibility.

Parameters

string $path
UnitEnum|string|null $disk
array $options

Return Value

string|false

at line 430
string|false storeAs(string $path, string|null $name = null, UnitEnum|string|null $disk = null, array $options = [])

Store the processed image on a filesystem disk with a given name.

Parameters

string $path
string|null $name
UnitEnum|string|null $disk
array $options

Return Value

string|false

at line 450
string|false storePubliclyAs(string $path, string|null $name = null, UnitEnum|string|null $disk = null, array $options = [])

Store the processed image on a filesystem disk with public visibility and a given name.

Parameters

string $path
string|null $name
UnitEnum|string|null $disk
array $options

Return Value

string|false

at line 464
string hashName(string $path = '')

Get a hashed filename with the correct extension.

Parameters

string $path

Return Value

string

at line 476
string toBytes()

Process the image and return the raw bytes.

Return Value

string

at line 490
protected string process()

Process the image recipe.

Return Value

string

Exceptions

ImageException

at line 504
string toBase64()

Process the image and return as a base64 encoded string.

Return Value

string

at line 512
string toDataUri()

Process the image and return as a data URI.

Return Value

string

at line 520
string extension()

Get the file extension based on the MIME type.

Return Value

string

at line 539
string mimeType()

Get the MIME type of the processed image.

Return Value

string

at line 551
array dimensions()

Get the dimensions of the processed image.

Return Value

array

Exceptions

ImageException

at line 585
int width()

Get the width of the processed image.

Return Value

int

at line 593
int height()

Get the height of the processed image.

Return Value

int

at line 601
string dominantColor()

Get the dominant (average) color of the image as a hex string.

Return Value

string

at line 609
Image using(string $driver)

Set the driver to use for processing.

Parameters

string $driver

Return Value

Image

at line 621
Image usingGd()

Use the GD driver for processing.

Return Value

Image

at line 629
Image usingImagick()

Use the Imagick driver for processing.

Return Value

Image

at line 637
protected Driver resolveDriver()

Resolve the image processing driver.

Return Value

Driver

at line 650
UploadedFile|null file()

Get the underlying uploaded file instance.

Return Value

UploadedFile|null

at line 658
void __clone()

Clone the pipeline and reset all derived state.

Return Value

void

at line 671
protected Image newClone()

Create an immutable clone with copied options.

Return Value

Image

at line 679
protected Image withOutput(Closure $callback)

Create an immutable clone with updated output options.

Parameters

Closure $callback

Return Value

Image

at line 687
protected Image withClone(Closure $callback)

Create an immutable clone with the given callback applied.

Parameters

Closure $callback

Return Value

Image

at line 699
Response toResponse(Request $request)

Create an HTTP response that represents the image.

Parameters

Request $request

Return Value

Response

at line 711
never __serialize()

Prevent serialization of the image.

Return Value

never

Exceptions

ImageException

at line 719
string toString()

Get the string representation of the image.

Return Value

string

at line 727
string __toString()

Get the string representation of the image.

Return Value

string

at line 735
static void flushState()

Flush all static state.

Return Value

void