class Image (View source)

The Image object represents a resized (and cropped) copy of a given image.

Properties

$file The filename of generated image.
$height The height of the generated image.
$originalHeight The height of the source image.
$originalWidth The width of the source image.
$width The width of the generated image.
private $crop Cropping parameter.
private $cropX The pixels to crop the image on the X-axis on both sides.
private $cropY The pixels to crop the image on the Y-axis on both sides.
private $fileFullPath The full file system path to the generated image.
private $originalFile The filename of the source image
private $requestedHeight The desired height of the new image. (May not be the resulting width, depending on cropping or original image size)
private $requestedWidth The disired width of the new image. (May not be the resulting width, depending on cropping or original image size)
private $type The given image type.

Methods

__construct(string $originalFile, float|null $requestedWidth = null, float|null $requestedHeight = null, bool $crop = false)

The constructor defines the main object properties from the given parameters and initiates the main methods.

calculateSize()

Calculate the size and pixels to crop for the generated image.

createImage()

Create a new (resized and cropped) image from the source image and save that image in AM_DIR_CACHE_IMAGES.

string
getImageCacheFilePath()

Determine the corresponding image file to a source file based on a md5 hash.

verifyCachedImage()

To verify, if the requested image is up to date, only the existence has to be tested, since any changes in the source image will be reflected in the filename's hash.

Details

__construct(string $originalFile, float|null $requestedWidth = null, float|null $requestedHeight = null, bool $crop = false)

The constructor defines the main object properties from the given parameters and initiates the main methods.

Parameters

string $originalFile
float|null $requestedWidth
float|null $requestedHeight
bool $crop

private calculateSize()

Calculate the size and pixels to crop for the generated image.

private createImage()

Create a new (resized and cropped) image from the source image and save that image in AM_DIR_CACHE_IMAGES.

private string getImageCacheFilePath()

Determine the corresponding image file to a source file based on a md5 hash.

That hash is based on the source image's path, mtime, the new width and height and the cropping parameter. If one parameter changes, the hash will be different, which will result in recreating an image. Since the mtime is part of the hash, also any modification to the source image will be reflected in a different name. For each size and cropping setting, a unique filename will be returned, to clearly identify that setting.

Return Value

string The matching filename for the requested source image, based on its parameters

private verifyCachedImage()

To verify, if the requested image is up to date, only the existence has to be tested, since any changes in the source image will be reflected in the filename's hash.