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();
Afficher le fichier Open project: contao/core-bundle Class Usage Examples

Protected Properties

Свойство 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)

Méthodes publiques

Méthode 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

Méthodes protégées

Méthode 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 méthode

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 méthode

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

create() public static méthode

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
Résultat static The created image instance

executeResize() public méthode

Resize the image
public executeResize ( )

get() public static méthode

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
Résultat string | null The path of the resized image or null

getCacheName() public méthode

Get the cache name
public getCacheName ( ) : string
Résultat string The cache name

getForceOverride() public méthode

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

getHtml() public static méthode

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
Résultat string The image HTML tag

getImportantPart() public méthode

Get the important part settings
public getImportantPart ( ) : array
Résultat array The settings array

getOriginalPath() public méthode

Get the path of the original image
public getOriginalPath ( ) : string
Résultat string The path of the original image

getPath() public static méthode

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

getPixelValue() public static méthode

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
Résultat integer The pixel value

getResizeMode() public méthode

Get the resize mode
public getResizeMode ( ) : string
Résultat string The resize mode

getResizedPath() public méthode

Get the path of the resized image
public getResizedPath ( ) : string
Résultat string The path of the resized image

getTargetHeight() public méthode

Get the target height
public getTargetHeight ( ) : integer
Résultat integer The target height

getTargetPath() public méthode

Get the target path
public getTargetPath ( ) : string
Résultat string The target path

getTargetWidth() public méthode

Get the target width
public getTargetWidth ( ) : integer
Résultat integer The target width

getZoomLevel() public méthode

Get the zoom level
public getZoomLevel ( ) : integer
Résultat integer The zoom level

prepareImage() protected méthode

Prepare image object.
protected prepareImage ( ) : Contao\Image\Image
Résultat Contao\Image\Image

prepareImportantPart() protected méthode

Prepare important part object.
protected prepareImportantPart ( ) : Contao\Image\ImportantPart
Résultat Contao\Image\ImportantPart

prepareResizeConfig() protected méthode

Prepare resize configuration object.
protected prepareResizeConfig ( ) : Contao\Image\ResizeConfiguration
Résultat Contao\Image\ResizeConfiguration

resize() public static méthode

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
Résultat boolean True if the image could be resized successfully

setForceOverride() public méthode

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

setImportantPart() public méthode

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

setResizeMode() public méthode

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

setTargetHeight() public méthode

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

setTargetPath() public méthode

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

setTargetWidth() public méthode

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

setZoomLevel() public méthode

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

Property Details

$fileObj protected_oe property

The File instance of the original image
protected File,contao $fileObj
Résultat contao\File

$forceOverride protected_oe property

Override an existing target
protected bool $forceOverride
Résultat boolean

$importantPart protected_oe property

Important part settings
protected array $importantPart
Résultat array

$resizeMode protected_oe property

The resize mode (defaults to crop for BC)
protected string $resizeMode
Résultat string

$resizedPath protected_oe property

The resized image path
protected string $resizedPath
Résultat string

$targetHeight protected_oe property

The target height
protected int $targetHeight
Résultat integer

$targetPath protected_oe property

The target path
protected string $targetPath
Résultat string

$targetWidth protected_oe property

The target width
protected int $targetWidth
Résultat integer

$zoomLevel protected_oe property

Zoom level (between 0 and 100)
protected int $zoomLevel
Résultat integer