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.

toJxl()

Convert the image to JPEG XL 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 30
static void macro(string $name, callable|object $macro)

Register a custom macro.

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 44
static void mixin(object $mixin, bool $replace = true)

Mix another object into the class.

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 61
static bool hasMacro(string $name)

Check if macro is registered.

Parameters

string $name

Return Value

bool

in Macroable at line 72
static void flushMacros()

Flush the existing macros.

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

Return Value

void

in Macroable at line 83
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 108
mixed __call(string $method, array $parameters)

Dynamically handle calls to the class.

Parameters

string $method
array $parameters

Return Value

mixed

Exceptions

BadMethodCallException

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

Create a new image instance.

Parameters

Closure|string $contents
UploadedFile|null $file

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

Set the cover dimensions.

Parameters

int $width
int $height

Return Value

Image

Exceptions

ImageException

at line 118
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 133
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 148
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 162
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 175
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 191
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 205
Image orient()

Auto-orient the image based on EXIF data.

Return Value

Image

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

Apply a blur effect.

Parameters

int $amount

Return Value

Image

Exceptions

ImageException

at line 227
Image grayscale()

Convert the image to grayscale.

Return Value

Image

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

Sharpen the image.

Parameters

int $amount

Return Value

Image

Exceptions

ImageException

at line 249
Image flipVertically()

Flip the image vertically.

Return Value

Image

at line 257
Image flipHorizontally()

Flip the image horizontally.

Return Value

Image

at line 265
Image flip()

Flip the image vertically.

Return Value

Image

at line 273
Image flop()

Flip the image horizontally.

Return Value

Image

at line 281
Image transform(Transformation $transformation)

Add a transformation to the image pipeline.

Parameters

Transformation $transformation

Return Value

Image

at line 293
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 305
Image quality(int $quality)

Set the output quality.

Parameters

int $quality

Return Value

Image

Exceptions

ImageException

at line 317
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 327
Image toWebp()

Convert the image to WebP format.

Return Value

Image

at line 335
Image toJpg()

Convert the image to JPEG format.

Return Value

Image

at line 343
Image toJpeg()

Convert the image to JPEG format.

Return Value

Image

at line 351
Image toPng()

Convert the image to PNG format.

Return Value

Image

at line 359
Image toGif()

Convert the image to GIF format.

Return Value

Image

at line 367
Image toAvif()

Convert the image to AVIF format.

Return Value

Image

at line 375
Image toHeic()

Convert the image to HEIC format.

Return Value

Image

at line 383
Image toJxl()

Convert the image to JPEG XL format.

Return Value

Image

at line 391
Image toBmp()

Convert the image to BMP format.

Return Value

Image

at line 401
Image toFormat(string $format)

Set the output format.

Parameters

string $format

Return Value

Image

Exceptions

ImageException

at line 417
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 427
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 439
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 459
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 473
string hashName(string $path = '')

Get a hashed filename with the correct extension.

Parameters

string $path

Return Value

string

at line 485
string toBytes()

Process the image and return the raw bytes.

Return Value

string

at line 499
protected string process()

Process the image recipe.

Return Value

string

Exceptions

ImageException

at line 517
string toBase64()

Process the image and return as a base64 encoded string.

Return Value

string

at line 525
string toDataUri()

Process the image and return as a data URI.

Return Value

string

at line 533
string extension()

Get the file extension based on the MIME type.

Return Value

string

at line 553
string mimeType()

Get the MIME type of the processed image.

Return Value

string

at line 565
array dimensions()

Get the dimensions of the processed image.

Return Value

array

Exceptions

ImageException

at line 603
int width()

Get the width of the processed image.

Return Value

int

at line 611
int height()

Get the height of the processed image.

Return Value

int

at line 619
string dominantColor()

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

Return Value

string

at line 627
Image using(string $driver)

Set the driver to use for processing.

Parameters

string $driver

Return Value

Image

at line 639
Image usingGd()

Use the GD driver for processing.

Return Value

Image

at line 647
Image usingImagick()

Use the Imagick driver for processing.

Return Value

Image

at line 655
protected Driver resolveDriver()

Resolve the image processing driver.

Return Value

Driver

at line 668
UploadedFile|null file()

Get the underlying uploaded file instance.

Return Value

UploadedFile|null

at line 676
void __clone()

Clone the pipeline and reset all derived state.

Return Value

void

at line 689
protected Image newClone()

Create an immutable clone with copied options.

Return Value

Image

at line 697
protected Image withOutput(Closure $callback)

Create an immutable clone with updated output options.

Parameters

Closure $callback

Return Value

Image

at line 705
protected Image withClone(Closure $callback)

Create an immutable clone with the given callback applied.

Parameters

Closure $callback

Return Value

Image

at line 717
Response toResponse(Request $request)

Create an HTTP response that represents the image.

Parameters

Request $request

Return Value

Response

at line 729
never __serialize()

Prevent serialization of the image.

Return Value

never

Exceptions

ImageException

at line 737
string toString()

Get the string representation of the image.

Return Value

string

at line 745
string __toString()

Get the string representation of the image.

Return Value

string

at line 753
static void flushState()

Flush all static state.

Return Value

void