PHP Class Contao\Image

The class resizes images and stores them in the assets/images folder. Usage: $imageObj = new Image(new File('example.jpg')); $src = $imageObj->setTargetWidth(640) ->setTargetHeight(480) ->setResizeMode('center_center') ->executeResize() ->getResizedPath();
Show file Open project: contao/core-bundle Class Usage Examples

Protected Properties

Property Type Description
$fileObj contao\File The File instance of the original image
$forceOverride boolean Override an existing target
$importantPart array Important part settings
$resizeMode string The resize mode (defaults to crop for BC)
$resizedPath string The resized image path
$targetHeight integer The target height
$targetPath string The target path
$targetWidth integer The target width
$zoomLevel integer Zoom level (between 0 and 100)

Public Methods

Method Description
__construct ( contao\File $file ) Create a new object to handle an image
computeResize ( ) : array Calculate the resize coordinates
create ( string | contao\File $image, array | integer $size = null ) : static Create an image instance from the given image path and size
executeResize ( ) Resize the image
get ( string $image, integer $width, integer $height, string $mode = '', string $target = null, boolean $force = false ) : string | null Resize an image and store the resized version in the image target folder
getCacheName ( ) : string Get the cache name
getForceOverride ( ) : boolean Get force override setting
getHtml ( string $src, string $alt = '', string $attributes = '' ) : string Generate an image tag and return it as string
getImportantPart ( ) : array Get the important part settings
getOriginalPath ( ) : string Get the path of the original image
getPath ( string $src ) : string Get the relative path to an image
getPixelValue ( string $size ) : integer Convert sizes like 2em, 10cm or 12pt to pixels
getResizeMode ( ) : string Get the resize mode
getResizedPath ( ) : string Get the path of the resized image
getTargetHeight ( ) : integer Get the target height
getTargetPath ( ) : string Get the target path
getTargetWidth ( ) : integer Get the target width
getZoomLevel ( ) : integer Get the zoom level
resize ( string $image, integer $width, integer $height, string $mode = '' ) : boolean Resize or crop an image and replace the original with the resized version
setForceOverride ( boolean $forceOverride ) Override the target image
setImportantPart ( array $importantPart = null ) Set the important part settings
setResizeMode ( string $resizeMode ) Set the resize mode
setTargetHeight ( integer $targetHeight ) Set the target height
setTargetPath ( string $targetPath ) Set the target path
setTargetWidth ( integer $targetWidth ) Set the target width
setZoomLevel ( integer $zoomLevel ) Set the zoom level

Protected Methods

Method Description
prepareImage ( ) : Contao\Image\Image Prepare image object.
prepareImportantPart ( ) : Contao\Image\ImportantPart Prepare important part object.
prepareResizeConfig ( ) : Contao\Image\ResizeConfiguration Prepare resize configuration object.

Method Details

__construct() public method

Create a new object to handle an image
Deprecation: Deprecated since Contao 4.3, to be removed in Contao 5.0. Use the contao.image.image_factory service instead.
public __construct ( contao\File $file )
$file contao\File A file instance of the original image

computeResize() public method

Calculate the resize coordinates
public computeResize ( ) : array
return array The resize coordinates (width, height, target_x, target_y, target_width, target_height)

create() public static method

Create an image instance from the given image path and size
Deprecation: Deprecated since Contao 4.3, to be removed in Contao 5.0. Use the contao.image.image_factory service instead.
public static create ( string | contao\File $image, array | integer $size = null ) : static
$image string | contao\File The image path or File instance
$size array | integer The image size as array (width, height, resize mode) or an tl_image_size ID
return static The created image instance

executeResize() public method

Resize the image
public executeResize ( )

get() public static method

Resize an image and store the resized version in the image target folder
Deprecation: Deprecated since Contao 4.3, to be removed in Contao 5.0. Use the contao.image.image_factory service instead.
public static get ( string $image, integer $width, integer $height, string $mode = '', string $target = null, boolean $force = false ) : string | null
$image string The image path
$width integer The target width
$height integer The target height
$mode string The resize mode
$target string An optional target path
$force boolean Override existing target images
return string | null The path of the resized image or null

getCacheName() public method

Get the cache name
public getCacheName ( ) : string
return string The cache name

getForceOverride() public method

Get force override setting
public getForceOverride ( ) : boolean
return boolean True if the target image will be overridden

getHtml() public static method

Generate an image tag and return it as string
public static getHtml ( string $src, string $alt = '', string $attributes = '' ) : string
$src string The image path
$alt string An optional alt attribute
$attributes string A string of other attributes
return string The image HTML tag

getImportantPart() public method

Get the important part settings
public getImportantPart ( ) : array
return array The settings array

getOriginalPath() public method

Get the path of the original image
public getOriginalPath ( ) : string
return string The path of the original image

getPath() public static method

Get the relative path to an image
public static getPath ( string $src ) : string
$src string The image name or path
return string The relative path

getPixelValue() public static method

Convert sizes like 2em, 10cm or 12pt to pixels
Deprecation: Deprecated since Contao 4.3, to be removed in Contao 5.0. Use the contao.image.image_factory service instead.
public static getPixelValue ( string $size ) : integer
$size string The size string
return integer The pixel value

getResizeMode() public method

Get the resize mode
public getResizeMode ( ) : string
return string The resize mode

getResizedPath() public method

Get the path of the resized image
public getResizedPath ( ) : string
return string The path of the resized image

getTargetHeight() public method

Get the target height
public getTargetHeight ( ) : integer
return integer The target height

getTargetPath() public method

Get the target path
public getTargetPath ( ) : string
return string The target path

getTargetWidth() public method

Get the target width
public getTargetWidth ( ) : integer
return integer The target width

getZoomLevel() public method

Get the zoom level
public getZoomLevel ( ) : integer
return integer The zoom level

prepareImage() protected method

Prepare image object.
protected prepareImage ( ) : Contao\Image\Image
return Contao\Image\Image

prepareImportantPart() protected method

Prepare important part object.
protected prepareImportantPart ( ) : Contao\Image\ImportantPart
return Contao\Image\ImportantPart

prepareResizeConfig() protected method

Prepare resize configuration object.
protected prepareResizeConfig ( ) : Contao\Image\ResizeConfiguration
return Contao\Image\ResizeConfiguration

resize() public static method

Resize or crop an image and replace the original with the resized version
Deprecation: Deprecated since Contao 4.3, to be removed in Contao 5.0. Use the contao.image.image_factory service instead.
public static resize ( string $image, integer $width, integer $height, string $mode = '' ) : boolean
$image string The image path
$width integer The target width
$height integer The target height
$mode string The resize mode
return boolean True if the image could be resized successfully

setForceOverride() public method

Override the target image
public setForceOverride ( boolean $forceOverride )
$forceOverride boolean True to override the target image

setImportantPart() public method

Set the important part settings
public setImportantPart ( array $importantPart = null )
$importantPart array The settings array

setResizeMode() public method

Set the resize mode
public setResizeMode ( string $resizeMode )
$resizeMode string The resize mode

setTargetHeight() public method

Set the target height
public setTargetHeight ( integer $targetHeight )
$targetHeight integer The target height

setTargetPath() public method

Set the target path
public setTargetPath ( string $targetPath )
$targetPath string The target path

setTargetWidth() public method

Set the target width
public setTargetWidth ( integer $targetWidth )
$targetWidth integer The target width

setZoomLevel() public method

Set the zoom level
public setZoomLevel ( integer $zoomLevel )
$zoomLevel integer The zoom level

Property Details

$fileObj protected property

The File instance of the original image
protected File,contao $fileObj
return contao\File

$forceOverride protected property

Override an existing target
protected bool $forceOverride
return boolean

$importantPart protected property

Important part settings
protected array $importantPart
return array

$resizeMode protected property

The resize mode (defaults to crop for BC)
protected string $resizeMode
return string

$resizedPath protected property

The resized image path
protected string $resizedPath
return string

$targetHeight protected property

The target height
protected int $targetHeight
return integer

$targetPath protected property

The target path
protected string $targetPath
return string

$targetWidth protected property

The target width
protected int $targetWidth
return integer

$zoomLevel protected property

Zoom level (between 0 and 100)
protected int $zoomLevel
return integer