Image
class Image implements ImageInterface (View source)
Defines an image object to represent an image file.
Properties
| protected string | $source | Path of the image file. |
|
| protected ImageToolkitInterface | $toolkit | An image toolkit object. |
|
| protected int | $fileSize | File size in bytes. |
Methods
Constructs a new Image object.
Checks if the image is valid.
Returns the height of the image.
Returns the width of the image.
Returns the size of the image file.
Returns the MIME type of the image file.
Retrieves the source path of the image file.
Returns the ID of the image toolkit used for this image file.
Returns the image toolkit used for this image file.
Closes the image and saves the changes to a file.
Applies a toolkit operation to the image.
Prepares a new image, without loading it from a file.
Instructs the toolkit to save the image in the format specified by the extension.
Crops an image to a rectangle specified by the given dimensions.
Converts an image to grayscale.
Resizes an image to the given dimensions (ignoring aspect ratio).
Rotates an image by the given number of degrees.
Scales an image to the exact width and height given.
Scales an image while maintaining aspect ratio.
Details
__construct(ImageToolkitInterface $toolkit, string|null $source = NULL)
Constructs a new Image object.
bool
isValid()
Checks if the image is valid.
int|null
getHeight()
Returns the height of the image.
int|null
getWidth()
Returns the width of the image.
int|null
getFileSize()
Returns the size of the image file.
string
getMimeType()
Returns the MIME type of the image file.
string
getSource()
Retrieves the source path of the image file.
string
getToolkitId()
Returns the ID of the image toolkit used for this image file.
ImageToolkitInterface
getToolkit()
Returns the image toolkit used for this image file.
bool
save(string|null $destination = NULL)
Closes the image and saves the changes to a file.
bool
apply(string $operation, array $arguments = [])
Applies a toolkit operation to the image.
The operation is deferred to the active toolkit.
bool
createNew(int $width, int $height, string $extension = 'png', string $transparent_color = '#ffffff')
Prepares a new image, without loading it from a file.
For a working example, see \Drupal\system\Plugin\ImageToolkit\Operation\gd\CreateNew.
bool
convert(string $extension)
Instructs the toolkit to save the image in the format specified by the extension.
bool
crop(int $x, int $y, int $width, int $height = NULL)
Crops an image to a rectangle specified by the given dimensions.
bool
desaturate()
Converts an image to grayscale.
bool
resize(int $width, int $height)
Resizes an image to the given dimensions (ignoring aspect ratio).
bool
rotate(float $degrees, string|null $background = NULL)
Rotates an image by the given number of degrees.
bool
scaleAndCrop(int $width, int $height)
Scales an image to the exact width and height given.
This function achieves the target aspect ratio by cropping the original image equally on both sides, or equally on the top and bottom. This function is useful to create uniform sized avatars from larger images.
The resulting image always has the exact target dimensions.
bool
scale(int|null $width, int|null $height = NULL, bool $upscale = FALSE)
Scales an image while maintaining aspect ratio.
The resulting image can be smaller for one or both target dimensions.