Image class

Framework\Image\Image

Class Image.

Implements: JsonSerializable, Stringable

Defined in libraries/image/src/Image.php

Properties #

$filename
Path to the image file.
$type
Image type. One of IMAGETYPE_* constants.
$mime
MIME type.
$instance
GdImage instance.
$quality
The image quality/compression level.

$filename

protected string $filename

Path to the image file.

$type

protected int $type

Image type. One of IMAGETYPE_* constants.

$mime

protected string $mime

MIME type.

$instance

protected GdImage $instance

GdImage instance.

$quality

protected ?int $quality = NULL

The image quality/compression level.

Methods #

__construct()
Image constructor.
__toString()
-
crop()
Crops the image.
destroy()
Destroys the GdImage instance.
filter()
Applies a filter to the image.
flatten()
Flattens the image.
flip()
Flips the image.
getDataUrl()
Allow embed the image contents in a document.
getExtension()
Gets the file extension for image type.
getHeight()
Gets the image height.
getInstance()
Gets the GdImage instance.
getMime()
Gets the image MIME type.
getQuality()
Gets the image quality/compression level.
getResolution()
Gets the image resolution.
getWidth()
Gets the image width.
isAcceptable()
Indicates if a given filename has an acceptable image type.
jsonSerialize()
-
opacity()
Sets the image opacity level.
render()
Renders the image contents.
rotate()
Rotates the image with a given angle.
save()
Saves the image contents to a given filename.
scale()
Scales the image.
send()
Sends the image contents to the output buffer.
setInstance()
Sets the GdImage instance.
setQuality()
Sets the image quality/compression level.
setResolution()
Sets the image resolution.
watermark()
Adds a watermark to the image.

__construct()

public function __construct(string $filename)

Image constructor.

Parameters
  • string $filename - Path to the image file. Acceptable types are: GIF, JPEG and PNG
throws
InvalidArgumentException for invalid file
throws
RuntimeException for unsupported image type of could not get image info

__toString()

public function __toString(): string
Returns string

crop()

public function crop(int $width, int $height, int $marginLeft = 0, int $marginTop = 0): static

Crops the image.

Parameters
  • int $width - Width in pixels
  • int $height - Height in pixels
  • int $marginLeft - Margin left in pixels
  • int $marginTop - Margin top in pixels
Returns static
throws
RuntimeException for image could not to crop

destroy()

public function destroy(): bool

Destroys the GdImage instance.

Returns bool

filter()

public function filter(int $type, int ...$arguments): static

Applies a filter to the image.

Parameters
  • int $type - IMG_FILTER_* constants
  • int $arguments
Returns static
throws
RuntimeException for image could not apply the filter

flatten()

public function flatten(int $red = 255, int $green = 255, int $blue = 255): static

Flattens the image.

Replaces transparency with an RGB color.

Parameters
  • int $red
  • int $green
  • int $blue
Returns static
throws
RuntimeException for could not create a true color image, could not allocate a color or image could not to flatten

flip()

public function flip(string $direction = 'horizontal'): static

Flips the image.

Parameters
  • string $direction - Allowed values are: h or horizontal. v or vertical. b or both.
Returns static
throws
InvalidArgumentException for invalid image flip direction
throws
RuntimeException for image could not to flip

getDataUrl()

public function getDataUrl(): string

Allow embed the image contents in a document.

Returns string - The image "data" URL

getExtension()

public function getExtension(): string|false

Gets the file extension for image type.

Returns string|false - a string with the extension corresponding to the given image type or false on fail

getHeight()

public function getHeight(): int

Gets the image height.

Returns int

getInstance()

public function getInstance(): GdImage

Gets the GdImage instance.

Returns GdImage - GdImage instance

getMime()

public function getMime(): string

Gets the image MIME type.

Returns string

getQuality()

public function getQuality(): ?int

Gets the image quality/compression level.

Returns ?int - An integer for PNG and JPEG types or null for GIF

getResolution()

public function getResolution(): array

Gets the image resolution.

Returns array - Returns an array containing two keys, horizontal and vertical, with integers as values
throws
RuntimeException for image could not get resolution

getWidth()

public function getWidth(): int

Gets the image width.

Returns int

isAcceptable()

public static function isAcceptable(string $filename): bool

Indicates if a given filename has an acceptable image type.

Parameters
  • string $filename
Returns bool

jsonSerialize()

public function jsonSerialize(): string
Returns string

opacity()

public function opacity(int $opacity = 100): static

Sets the image opacity level.

Parameters
  • int $opacity - Opacity percentage: from 0 to 100
Returns static

render()

public function render(): string

Renders the image contents.

Returns string - The image contents
throws
RuntimeException for image could not be rendered

rotate()

public function rotate(float $angle): static

Rotates the image with a given angle.

Parameters
  • float $angle - Rotation angle, in degrees. Clockwise direction.
Returns static
throws
RuntimeException for image could not allocate a color or could not rotate

save()

public function save(?string $filename = NULL): bool

Saves the image contents to a given filename.

Parameters
  • ?string $filename - Optional filename or null to use the original
Returns bool

scale()

public function scale(int $width, int $height = -1): static

Scales the image.

Parameters
  • int $width - Width in pixels
  • int $height - Height in pixels. Use -1 to use a proportional height based on the width.
Returns static
throws
RuntimeException for image could not to scale

send()

public function send(): bool

Sends the image contents to the output buffer.

Returns bool

setInstance()

public function setInstance(GdImage $instance): static

Sets the GdImage instance.

Parameters
  • GdImage $instance - GdImage instance
Returns static

setQuality()

public function setQuality(int $quality): static

Sets the image quality/compression level.

Parameters
  • int $quality - The quality/compression level
Returns static
throws
LogicException when trying to set a quality value for a GIF image
throws
InvalidArgumentException if the image type is PNG and the value is not between 0 and 9 or if the image type is JPEG and the value is not between 0 and 100

setResolution()

public function setResolution(int $horizontal = 96, int $vertical = 96): static

Sets the image resolution.

Parameters
  • int $horizontal - The horizontal resolution in DPI
  • int $vertical - The vertical resolution in DPI
Returns static
throws
RuntimeException for image could not to set resolution

watermark()

public function watermark(\Framework\Image\Image $watermark, int $horizontalPosition = 0, int $verticalPosition = 0): static

Adds a watermark to the image.

Parameters
  • \Framework\Image\Image $watermark - The image to use as watermark
  • int $horizontalPosition - Horizontal position
  • int $verticalPosition - Vertical position
Returns static
throws
RuntimeException for image could not to create watermark