class Image

Basic manipulation with images. Supported types are JPEG, PNG, GIF, WEBP, AVIF and BMP.

$image = Image::fromFile('nette.jpg');
$image->resize(150, 100);
$image->sharpen();
$image->send();

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

Properties

positive-int read-only $width
positive-int read-only $height
GdImage read-only $imageResource

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)

Reads an image from a file and returns its type in $type.

static Image
fromString(string $s, int|null $type = null)

Reads an image from a string and returns its type in $type.

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, $width = null, $height = null)

Returns the type of image from file.

static int|null
detectTypeFromString(string $s, $width = null, $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)

No description

static array
getSupportedTypes()

No description

__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, $newWidth, $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)

Draw a rectangle.

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

Draw a filled rectangle.

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
__sleep()

Prevents serialization.

int
resolveColor(ImageColor|array $color)

No description

affine(array $affine, ?array $clip = null)

No description

void
alphaBlending(bool $enable)

No description

void
antialias(bool $enable)

No description

void
arc(int $centerX, int $centerY, int $width, int $height, int $startAngle, int $endAngle, ImageColor $color)

No description

int
colorAllocate(int $red, int $green, int $blue)

No description

int
colorAllocateAlpha(int $red, int $green, int $blue, int $alpha)

No description

int
colorAt(int $x, int $y)

No description

int
colorClosest(int $red, int $green, int $blue)

No description

int
colorClosestAlpha(int $red, int $green, int $blue, int $alpha)

No description

int
colorClosestHWB(int $red, int $green, int $blue)

No description

void
colorDeallocate(int $color)

No description

int
colorExact(int $red, int $green, int $blue)

No description

int
colorExactAlpha(int $red, int $green, int $blue, int $alpha)

No description

void
colorMatch(Image $image2)

No description

int
colorResolve(int $red, int $green, int $blue)

No description

int
colorResolveAlpha(int $red, int $green, int $blue, int $alpha)

No description

void
colorSet(int $index, int $red, int $green, int $blue, int $alpha)

No description

array
colorsForIndex(int $color)

No description

int
colorsTotal()

No description

int
colorTransparent(?int $color = null)

No description

void
convolution(array $matrix, float $div, float $offset)

No description

void
copy(Image $src, int $dstX, int $dstY, int $srcX, int $srcY, int $srcW, int $srcH)

No description

void
copyMerge(Image $src, int $dstX, int $dstY, int $srcX, int $srcY, int $srcW, int $srcH, int $pct)

No description

void
copyMergeGray(Image $src, int $dstX, int $dstY, int $srcX, int $srcY, int $srcW, int $srcH, int $pct)

No description

void
copyResampled(Image $src, int $dstX, int $dstY, int $srcX, int $srcY, int $dstW, int $dstH, int $srcW, int $srcH)

No description

void
copyResized(Image $src, int $dstX, int $dstY, int $srcX, int $srcY, int $dstW, int $dstH, int $srcW, int $srcH)

No description

cropAuto(int $mode = IMG_CROP_DEFAULT, float $threshold = .5, ImageColor $color = null)

No description

void
ellipse(int $centerX, int $centerY, int $width, int $height, ImageColor $color)

No description

void
fill(int $x, int $y, ImageColor $color)

No description

void
filledArc(int $centerX, int $centerY, int $width, int $height, int $startAngle, int $endAngle, ImageColor $color, int $style)

No description

void
filledEllipse(int $centerX, int $centerY, int $width, int $height, ImageColor $color)

No description

void
filledPolygon(array $points, ImageColor $color)

No description

void
filledRectangle(int $x1, int $y1, int $x2, int $y2, ImageColor $color)

No description

void
fillToBorder(int $x, int $y, ImageColor $borderColor, ImageColor $color)

No description

void
filter(int $filter, mixed $args)

No description

void
flip(int $mode)

No description

array
ftText(float $size, float $angle, int $x, int $y, ImageColor $color, string $fontFile, string $text, array $options = [])

No description

void
gammaCorrect(float $inputgamma, float $outputgamma)

No description

array
getClip()

No description

int
getInterpolation()

No description

int
interlace(?bool $enable = null)

No description

bool
isTrueColor()

No description

void
layerEffect(int $effect)

No description

void
line(int $x1, int $y1, int $x2, int $y2, ImageColor $color)

No description

void
openPolygon(array $points, ImageColor $color)

No description

void
paletteCopy(Image $source)

No description

void
paletteToTrueColor()

No description

void
polygon(array $points, ImageColor $color)

No description

void
rectangle(int $x1, int $y1, int $x2, int $y2, ImageColor $color)

No description

mixed
resolution(?int $resolutionX = null, ?int $resolutionY = null)

No description

rotate(float $angle, ImageColor $backgroundColor)

No description

void
saveAlpha(bool $enable)

No description

scale(int $newWidth, int $newHeight = -1, int $mode = IMG_BILINEAR_FIXED)

No description

void
setBrush(Image $brush)

No description

void
setClip(int $x1, int $y1, int $x2, int $y2)

No description

void
setInterpolation(int $method = IMG_BILINEAR_FIXED)

No description

void
setPixel(int $x, int $y, ImageColor $color)

No description

void
setStyle(array $style)

No description

void
setThickness(int $thickness)

No description

void
setTile(Image $tile)

No description

void
trueColorToPalette(bool $dither, int $ncolors)

No description

array
ttfText(float $size, float $angle, int $x, int $y, ImageColor $color, string $fontfile, string $text, array $options = [])

No description

Details

at line 148
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 164
static Image fromFile(string $file, int|null $type = null)

Reads an image from a file and returns its type in $type.

Parameters

string $file
int|null $type

Return Value

Image

Exceptions

NotSupportedException
UnknownImageFileException

at line 181
static Image fromString(string $s, int|null $type = null)

Reads an image from a string and returns its type in $type.

Parameters

string $s
int|null $type

Return Value

Image

Exceptions

NotSupportedException
ImageException

at line 216
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 238
static int|null detectTypeFromFile(string $file, $width = null, $height = null)

Returns the type of image from file.

Parameters

string $file
$width
$height

Return Value

int|null

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

Returns the type of image from string.

Parameters

string $s
$width
$height

Return Value

int|null

at line 261
static string typeToExtension(int $type)

Returns the file extension for the given image type.

Parameters

int $type

Return Value

string

at line 275
static int extensionToType(string $extension)

Returns the image type for given file extension.

Parameters

string $extension

Return Value

int

at line 291
static string typeToMimeType(int $type)

Returns the mime type for the given image type.

Parameters

int $type

Return Value

string

at line 300
static bool isTypeSupported(int $type)

No description

Parameters

int $type

Return Value

bool

at line 316
static array getSupportedTypes()

No description

Return Value

array

at line 334
__construct(GdImage $image)

Wraps GD image.

Parameters

GdImage $image

at line 345
int getWidth()

Returns image width.

Return Value

int

at line 355
int getHeight()

Returns image height.

Return Value

int

at line 364
protected Image setImageResource(GdImage $image)

Sets image resource.

Parameters

GdImage $image

Return Value

Image

at line 374
GdImage getImageResource()

Returns image GD resource.

Return Value

GdImage

at line 384
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 421
static array calculateSize(int $srcWidth, int $srcHeight, $newWidth, $newHeight, int $mode = self::OrSmaller)

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

Parameters

int $srcWidth
int $srcHeight
$newWidth
$newHeight
int $mode

Return Value

array

at line 488
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 508
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 552
Image sharpen()

Sharpens image a little bit.

Return Value

Image

at line 567
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 628
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 650
void rectangleWH(int $x, int $y, int $width, int $height, ImageColor $color)

Draw a rectangle.

Parameters

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

Return Value

void

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

Draw a filled rectangle.

Parameters

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

Return Value

void

at line 674
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 685
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 696
string __toString()

Outputs image to string.

Return Value

string

at line 707
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 764
mixed __call(string $name, array $args)

Call to undefined method.

Parameters

string $name
array $args

Return Value

mixed

Exceptions

MemberAccessException

at line 787
__clone()

No description

at line 812
array __sleep()

Prevents serialization.

Return Value

array

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

No description

Parameters

ImageColor|array $color

Return Value

int

at line 91
Image affine(array $affine, ?array $clip = null)

No description

Parameters

array $affine
?array $clip

Return Value

Image

at line 91
void alphaBlending(bool $enable)

No description

Parameters

bool $enable

Return Value

void

at line 91
void antialias(bool $enable)

No description

Parameters

bool $enable

Return Value

void

at line 91
void arc(int $centerX, int $centerY, int $width, int $height, int $startAngle, int $endAngle, ImageColor $color)

No description

Parameters

int $centerX
int $centerY
int $width
int $height
int $startAngle
int $endAngle
ImageColor $color

Return Value

void

at line 91
int colorAllocate(int $red, int $green, int $blue)

No description

Parameters

int $red
int $green
int $blue

Return Value

int

at line 91
int colorAllocateAlpha(int $red, int $green, int $blue, int $alpha)

No description

Parameters

int $red
int $green
int $blue
int $alpha

Return Value

int

at line 91
int colorAt(int $x, int $y)

No description

Parameters

int $x
int $y

Return Value

int

at line 91
int colorClosest(int $red, int $green, int $blue)

No description

Parameters

int $red
int $green
int $blue

Return Value

int

at line 91
int colorClosestAlpha(int $red, int $green, int $blue, int $alpha)

No description

Parameters

int $red
int $green
int $blue
int $alpha

Return Value

int

at line 91
int colorClosestHWB(int $red, int $green, int $blue)

No description

Parameters

int $red
int $green
int $blue

Return Value

int

at line 91
void colorDeallocate(int $color)

No description

Parameters

int $color

Return Value

void

at line 91
int colorExact(int $red, int $green, int $blue)

No description

Parameters

int $red
int $green
int $blue

Return Value

int

at line 91
int colorExactAlpha(int $red, int $green, int $blue, int $alpha)

No description

Parameters

int $red
int $green
int $blue
int $alpha

Return Value

int

at line 91
void colorMatch(Image $image2)

No description

Parameters

Image $image2

Return Value

void

at line 91
int colorResolve(int $red, int $green, int $blue)

No description

Parameters

int $red
int $green
int $blue

Return Value

int

at line 91
int colorResolveAlpha(int $red, int $green, int $blue, int $alpha)

No description

Parameters

int $red
int $green
int $blue
int $alpha

Return Value

int

at line 91
void colorSet(int $index, int $red, int $green, int $blue, int $alpha)

No description

Parameters

int $index
int $red
int $green
int $blue
int $alpha

Return Value

void

at line 91
array colorsForIndex(int $color)

No description

Parameters

int $color

Return Value

array

at line 91
int colorsTotal()

No description

Return Value

int

at line 91
int colorTransparent(?int $color = null)

No description

Parameters

?int $color

Return Value

int

at line 91
void convolution(array $matrix, float $div, float $offset)

No description

Parameters

array $matrix
float $div
float $offset

Return Value

void

at line 91
void copy(Image $src, int $dstX, int $dstY, int $srcX, int $srcY, int $srcW, int $srcH)

No description

Parameters

Image $src
int $dstX
int $dstY
int $srcX
int $srcY
int $srcW
int $srcH

Return Value

void

at line 91
void copyMerge(Image $src, int $dstX, int $dstY, int $srcX, int $srcY, int $srcW, int $srcH, int $pct)

No description

Parameters

Image $src
int $dstX
int $dstY
int $srcX
int $srcY
int $srcW
int $srcH
int $pct

Return Value

void

at line 91
void copyMergeGray(Image $src, int $dstX, int $dstY, int $srcX, int $srcY, int $srcW, int $srcH, int $pct)

No description

Parameters

Image $src
int $dstX
int $dstY
int $srcX
int $srcY
int $srcW
int $srcH
int $pct

Return Value

void

at line 91
void copyResampled(Image $src, int $dstX, int $dstY, int $srcX, int $srcY, int $dstW, int $dstH, int $srcW, int $srcH)

No description

Parameters

Image $src
int $dstX
int $dstY
int $srcX
int $srcY
int $dstW
int $dstH
int $srcW
int $srcH

Return Value

void

at line 91
void copyResized(Image $src, int $dstX, int $dstY, int $srcX, int $srcY, int $dstW, int $dstH, int $srcW, int $srcH)

No description

Parameters

Image $src
int $dstX
int $dstY
int $srcX
int $srcY
int $dstW
int $dstH
int $srcW
int $srcH

Return Value

void

at line 91
Image cropAuto(int $mode = IMG_CROP_DEFAULT, float $threshold = .5, ImageColor $color = null)

No description

Parameters

int $mode
float $threshold
ImageColor $color

Return Value

Image

at line 91
void ellipse(int $centerX, int $centerY, int $width, int $height, ImageColor $color)

No description

Parameters

int $centerX
int $centerY
int $width
int $height
ImageColor $color

Return Value

void

at line 91
void fill(int $x, int $y, ImageColor $color)

No description

Parameters

int $x
int $y
ImageColor $color

Return Value

void

at line 91
void filledArc(int $centerX, int $centerY, int $width, int $height, int $startAngle, int $endAngle, ImageColor $color, int $style)

No description

Parameters

int $centerX
int $centerY
int $width
int $height
int $startAngle
int $endAngle
ImageColor $color
int $style

Return Value

void

at line 91
void filledEllipse(int $centerX, int $centerY, int $width, int $height, ImageColor $color)

No description

Parameters

int $centerX
int $centerY
int $width
int $height
ImageColor $color

Return Value

void

at line 91
void filledPolygon(array $points, ImageColor $color)

No description

Parameters

array $points
ImageColor $color

Return Value

void

at line 91
void filledRectangle(int $x1, int $y1, int $x2, int $y2, ImageColor $color)

No description

Parameters

int $x1
int $y1
int $x2
int $y2
ImageColor $color

Return Value

void

at line 91
void fillToBorder(int $x, int $y, ImageColor $borderColor, ImageColor $color)

No description

Parameters

int $x
int $y
ImageColor $borderColor
ImageColor $color

Return Value

void

at line 91
void filter(int $filter, mixed $args)

No description

Parameters

int $filter
mixed $args

Return Value

void

at line 91
void flip(int $mode)

No description

Parameters

int $mode

Return Value

void

at line 91
array ftText(float $size, float $angle, int $x, int $y, ImageColor $color, string $fontFile, string $text, array $options = [])

No description

Parameters

float $size
float $angle
int $x
int $y
ImageColor $color
string $fontFile
string $text
array $options

Return Value

array

at line 91
void gammaCorrect(float $inputgamma, float $outputgamma)

No description

Parameters

float $inputgamma
float $outputgamma

Return Value

void

at line 91
array getClip()

No description

Return Value

array

at line 91
int getInterpolation()

No description

Return Value

int

at line 91
int interlace(?bool $enable = null)

No description

Parameters

?bool $enable

Return Value

int

at line 91
bool isTrueColor()

No description

Return Value

bool

at line 91
void layerEffect(int $effect)

No description

Parameters

int $effect

Return Value

void

at line 91
void line(int $x1, int $y1, int $x2, int $y2, ImageColor $color)

No description

Parameters

int $x1
int $y1
int $x2
int $y2
ImageColor $color

Return Value

void

at line 91
void openPolygon(array $points, ImageColor $color)

No description

Parameters

array $points
ImageColor $color

Return Value

void

at line 91
void paletteCopy(Image $source)

No description

Parameters

Image $source

Return Value

void

at line 91
void paletteToTrueColor()

No description

Return Value

void

at line 91
void polygon(array $points, ImageColor $color)

No description

Parameters

array $points
ImageColor $color

Return Value

void

at line 91
void rectangle(int $x1, int $y1, int $x2, int $y2, ImageColor $color)

No description

Parameters

int $x1
int $y1
int $x2
int $y2
ImageColor $color

Return Value

void

at line 91
mixed resolution(?int $resolutionX = null, ?int $resolutionY = null)

No description

Parameters

?int $resolutionX
?int $resolutionY

Return Value

mixed

at line 91
Image rotate(float $angle, ImageColor $backgroundColor)

No description

Parameters

float $angle
ImageColor $backgroundColor

Return Value

Image

at line 91
void saveAlpha(bool $enable)

No description

Parameters

bool $enable

Return Value

void

at line 91
Image scale(int $newWidth, int $newHeight = -1, int $mode = IMG_BILINEAR_FIXED)

No description

Parameters

int $newWidth
int $newHeight
int $mode

Return Value

Image

at line 91
void setBrush(Image $brush)

No description

Parameters

Image $brush

Return Value

void

at line 91
void setClip(int $x1, int $y1, int $x2, int $y2)

No description

Parameters

int $x1
int $y1
int $x2
int $y2

Return Value

void

at line 91
void setInterpolation(int $method = IMG_BILINEAR_FIXED)

No description

Parameters

int $method

Return Value

void

at line 91
void setPixel(int $x, int $y, ImageColor $color)

No description

Parameters

int $x
int $y
ImageColor $color

Return Value

void

at line 91
void setStyle(array $style)

No description

Parameters

array $style

Return Value

void

at line 91
void setThickness(int $thickness)

No description

Parameters

int $thickness

Return Value

void

at line 91
void setTile(Image $tile)

No description

Parameters

Image $tile

Return Value

void

at line 91
void trueColorToPalette(bool $dither, int $ncolors)

No description

Parameters

bool $dither
int $ncolors

Return Value

void

at line 91
array ttfText(float $size, float $angle, int $x, int $y, ImageColor $color, string $fontfile, string $text, array $options = [])

No description

Parameters

float $size
float $angle
int $x
int $y
ImageColor $color
string $fontfile
string $text
array $options

Return Value

array

Traits

Strict class for better experience.