class Image

Constants

ShrinkOnly

Prevent from getting resized to a bigger size than the original

Stretch

Resizes to a specified width and height without keeping aspect ratio

OrSmaller

Resizes to fit into a specified width and height and preserves aspect ratio

OrBigger

Resizes while bounding the smaller dimension to the specified width or height and preserves aspect ratio

Cover

Resizes to the smallest possible size to completely cover specified width and height and reserves aspect ratio

deprecated SHRINK_ONLY

deprecated STRETCH

deprecated FIT

deprecated FILL

deprecated EXACT

deprecated EMPTY_GIF

JPEG

image types

PNG

image types

GIF

image types

WEBP

image types

AVIF

image types

BMP

image types

EmptyGIF

private Formats

private Sentinel

Methods

static array
rgb(int $red, int $green, int $blue, int $transparency = 0) deprecated

Returns RGB color (0..255) and transparency (0..127).

static Image
fromFile(string $file, int|null $type = null, string|null $warnings = self::Sentinel)

Reads an image from a file and returns its type in $type. If $warnings is passed, recoverable decoder warnings are returned in it instead of being raised as a PHP warning.

static Image
fromString(string $s, int|null $type = null, string|null $warnings = self::Sentinel)

Reads an image from a string and returns its type in $type. If $warnings is passed, recoverable decoder warnings are returned in it instead of being raised as a PHP warning.

static Image
fromBlank(int $width, int $height, ImageColor|array|null $color = null)

Creates a new true color image of the given dimensions. The default color is black.

static int|null
detectTypeFromFile(string $file, mixed $width = null, mixed $height = null)

Returns the type of image from file.

static int|null
detectTypeFromString(string $s, mixed $width = null, mixed $height = null)

Returns the type of image from string.

static string
typeToExtension(int $type)

Returns the file extension for the given image type.

static int
extensionToType(string $extension)

Returns the image type for given file extension.

static string
typeToMimeType(int $type)

Returns the mime type for the given image type.

static bool
isTypeSupported(int $type)

Checks whether the given image type is supported by the GD extension.

static array
getSupportedTypes()

Returns list of image types supported by the GD extension.

__construct(GdImage $image)

Wraps GD image.

int
getWidth()

Returns image width.

int
getHeight()

Returns image height.

setImageResource(GdImage $image)

Sets image resource.

GdImage
getImageResource()

Returns image GD resource.

resize(int|string|null $width, int|string|null $height, int $mode = self::OrSmaller)

Scales an image. Width and height accept pixels or percent.

static array
calculateSize(int $srcWidth, int $srcHeight, int|string|null $newWidth, int|string|null $newHeight, int $mode = self::OrSmaller)

Calculates dimensions of resized image. Width and height accept pixels or percent.

crop(int|string $left, int|string $top, int|string $width, int|string $height)

Crops image. Arguments accepts pixels or percent.

static array
calculateCutout(int $srcWidth, int $srcHeight, int|string $left, int|string $top, int|string $newWidth, int|string $newHeight)

Calculates dimensions of cutout in image. Arguments accepts pixels or percent.

sharpen()

Sharpens image a little bit.

place(Image $image, int|string $left = 0, int|string $top = 0, int $opacity = 100)

Puts another image into this image. Left and top accepts pixels or percent.

static array
calculateTextBox(string $text, string $fontFile, float $size, float $angle = 0, array $options = [])

Calculates the bounding box for a TrueType text. Returns keys left, top, width and height.

void
rectangleWH(int $x, int $y, int $width, int $height, ImageColor $color)

Draws a rectangle using top-left coordinates and dimensions instead of two corner coordinates.

void
filledRectangleWH(int $x, int $y, int $width, int $height, ImageColor $color)

Draws a filled rectangle using top-left coordinates and dimensions instead of two corner coordinates.

void
save(string $file, int|null $quality = null, int|null $type = null)

Saves image to the file. Quality is in the range 0..100 for JPEG (default 85), WEBP (default 80) and AVIF (default 30) and 0..9 for PNG (default 9).

string
toString(int $type = ImageType::JPEG, int|null $quality = null)

Outputs image to string. Quality is in the range 0..100 for JPEG (default 85), WEBP (default 80) and AVIF (default 30) and 0..9 for PNG (default 9).

string
__toString()

Outputs image to string.

void
send(int $type = ImageType::JPEG, int|null $quality = null)

Outputs image to browser. Quality is in the range 0..100 for JPEG (default 85), WEBP (default 80) and AVIF (default 30) and 0..9 for PNG (default 9).

mixed
__call(string $name, array $args)

Call to undefined method.

__clone()

No description

array
__serialize()

Prevents serialization.

int
resolveColor(ImageColor|array $color)

Resolves a color to a GD color index for the current image.

Details

at line 150
static array rgb(int $red, int $green, int $blue, int $transparency = 0) deprecated

deprecated use ImageColor::rgb()

Returns RGB color (0..255) and transparency (0..127).

Parameters

int $red
int $green
int $blue
int $transparency

Return Value

array

at line 168
static Image fromFile(string $file, int|null $type = null, string|null $warnings = self::Sentinel)

Reads an image from a file and returns its type in $type. If $warnings is passed, recoverable decoder warnings are returned in it instead of being raised as a PHP warning.

Parameters

string $file
int|null $type
string|null $warnings

Return Value

Image

Exceptions

NotSupportedException
UnknownImageFileException

at line 187
static Image fromString(string $s, int|null $type = null, string|null $warnings = self::Sentinel)

Reads an image from a string and returns its type in $type. If $warnings is passed, recoverable decoder warnings are returned in it instead of being raised as a PHP warning.

Parameters

string $s
int|null $type
string|null $warnings

Return Value

Image

Exceptions

NotSupportedException
ImageException

at line 233
static Image fromBlank(int $width, int $height, ImageColor|array|null $color = null)

Creates a new true color image of the given dimensions. The default color is black.

Parameters

int $width
int $height
ImageColor|array|null $color

Return Value

Image

Exceptions

NotSupportedException

at line 257
static int|null detectTypeFromFile(string $file, mixed $width = null, mixed $height = null)

Returns the type of image from file.

Parameters

string $file
mixed $width
mixed $height

Return Value

int|null

at line 270
static int|null detectTypeFromString(string $s, mixed $width = null, mixed $height = null)

Returns the type of image from string.

Parameters

string $s
mixed $width
mixed $height

Return Value

int|null

at line 282
static string typeToExtension(int $type)

Returns the file extension for the given image type.

Parameters

int $type

Return Value

string

at line 296
static int extensionToType(string $extension)

Returns the image type for given file extension.

Parameters

string $extension

Return Value

int

at line 312
static string typeToMimeType(int $type)

Returns the mime type for the given image type.

Parameters

int $type

Return Value

string

at line 322
static bool isTypeSupported(int $type)

Checks whether the given image type is supported by the GD extension.

Parameters

int $type

Return Value

bool

at line 341
static array getSupportedTypes()

Returns list of image types supported by the GD extension.

Return Value

array

at line 359
__construct(GdImage $image)

Wraps GD image.

Parameters

GdImage $image

at line 370
int getWidth()

Returns image width.

Return Value

int

at line 380
int getHeight()

Returns image height.

Return Value

int

at line 389
protected Image setImageResource(GdImage $image)

Sets image resource.

Parameters

GdImage $image

Return Value

Image

at line 399
GdImage getImageResource()

Returns image GD resource.

Return Value

GdImage

at line 409
Image resize(int|string|null $width, int|string|null $height, int $mode = self::OrSmaller)

Scales an image. Width and height accept pixels or percent.

Parameters

int|string|null $width
int|string|null $height
int $mode

Return Value

Image

at line 451
static array calculateSize(int $srcWidth, int $srcHeight, int|string|null $newWidth, int|string|null $newHeight, int $mode = self::OrSmaller)

Calculates dimensions of resized image. Width and height accept pixels or percent.

Parameters

int $srcWidth
int $srcHeight
int|string|null $newWidth
int|string|null $newHeight
int $mode

Return Value

array

at line 518
Image crop(int|string $left, int|string $top, int|string $width, int|string $height)

Crops image. Arguments accepts pixels or percent.

Parameters

int|string $left
int|string $top
int|string $width
int|string $height

Return Value

Image

at line 539
static array calculateCutout(int $srcWidth, int $srcHeight, int|string $left, int|string $top, int|string $newWidth, int|string $newHeight)

Calculates dimensions of cutout in image. Arguments accepts pixels or percent.

Parameters

int $srcWidth
int $srcHeight
int|string $left
int|string $top
int|string $newWidth
int|string $newHeight

Return Value

array

at line 572
Image sharpen()

Sharpens image a little bit.

Return Value

Image

at line 587
Image place(Image $image, int|string $left = 0, int|string $top = 0, int $opacity = 100)

Puts another image into this image. Left and top accepts pixels or percent.

Parameters

Image $image
int|string $left
int|string $top
int $opacity 0..100

Return Value

Image

at line 644
static array calculateTextBox(string $text, string $fontFile, float $size, float $angle = 0, array $options = [])

Calculates the bounding box for a TrueType text. Returns keys left, top, width and height.

Parameters

string $text
string $fontFile
float $size
float $angle
array $options

Return Value

array

at line 666
void rectangleWH(int $x, int $y, int $width, int $height, ImageColor $color)

Draws a rectangle using top-left coordinates and dimensions instead of two corner coordinates.

Parameters

int $x
int $y
int $width
int $height
ImageColor $color

Return Value

void

at line 677
void filledRectangleWH(int $x, int $y, int $width, int $height, ImageColor $color)

Draws a filled rectangle using top-left coordinates and dimensions instead of two corner coordinates.

Parameters

int $x
int $y
int $width
int $height
ImageColor $color

Return Value

void

at line 690
void save(string $file, int|null $quality = null, int|null $type = null)

Saves image to the file. Quality is in the range 0..100 for JPEG (default 85), WEBP (default 80) and AVIF (default 30) and 0..9 for PNG (default 9).

Parameters

string $file
int|null $quality
int|null $type

Return Value

void

Exceptions

ImageException

at line 701
string toString(int $type = ImageType::JPEG, int|null $quality = null)

Outputs image to string. Quality is in the range 0..100 for JPEG (default 85), WEBP (default 80) and AVIF (default 30) and 0..9 for PNG (default 9).

Parameters

int $type
int|null $quality

Return Value

string

at line 712
string __toString()

Outputs image to string.

Return Value

string

at line 723
void send(int $type = ImageType::JPEG, int|null $quality = null)

Outputs image to browser. Quality is in the range 0..100 for JPEG (default 85), WEBP (default 80) and AVIF (default 30) and 0..9 for PNG (default 9).

Parameters

int $type
int|null $quality

Return Value

void

Exceptions

ImageException

at line 766
mixed __call(string $name, array $args)

Call to undefined method.

Parameters

string $name
array $args

Return Value

mixed

Exceptions

MemberAccessException

at line 790
__clone()

No description

at line 816
array __serialize()

Prevents serialization.

Return Value

array

at line 826
int resolveColor(ImageColor|array $color)

Resolves a color to a GD color index for the current image.

Parameters

ImageColor|array $color

Return Value

int

Traits

Strict class for better experience.