PHP Class yii\imagine\BaseImage

Do not use BaseImage. Use [[Image]] instead.
Since: 2.0
Author: Antonio Ramirez ([email protected])
Author: Qiang Xue ([email protected])
Show file Open project: yiisoft/yii2-imagine Class Usage Examples

Public Properties

Property Type Description
$driver the driver to use. This can be either a single driver name or an array of driver names. If the latter, the first available driver will be used.
$thumbnailBackgroundAlpha background alpha (transparency) to use when creating thumbnails in ImageInterface::THUMBNAIL_INSET mode with both width and height specified. Default is solid.
$thumbnailBackgroundColor background color to use when creating thumbnails in ImageInterface::THUMBNAIL_INSET mode with both width and height specified. Default is white.

Public Methods

Method Description
autorotate ( string | resource | Imagine\Image\ImageInterface $image, string $color = '000000' ) : Imagine\Image\ImageInterface Rotates an image automatically based on EXIF information.
crop ( string | resource | Imagine\Image\ImageInterface $image, integer $width, integer $height, array $start = [0, 0] ) : Imagine\Image\ImageInterface Crops an image.
frame ( string | resource | Imagine\Image\ImageInterface $image, integer $margin = 20, string $color = '666', integer $alpha = 100 ) : Imagine\Image\ImageInterface Adds a frame around of the image. Please note that the image size will increase by $margin x 2.
getImagine ( ) : Imagine\Image\ImagineInterface Returns the Imagine object that supports various image manipulations.
resize ( string | resource | Imagine\Image\ImageInterface $image, integer $width, integer $height, boolean $keepAspectRatio = true, boolean $allowUpscaling = false ) : Imagine\Image\ImageInterface Resizes an image.
setImagine ( Imagine\Image\ImagineInterface $imagine )
text ( string | resource | Imagine\Image\ImageInterface $image, string $text, string $fontFile, array $start = [0, 0], array $fontOptions = [] ) : Imagine\Image\ImageInterface Draws a text string on an existing image.
thumbnail ( string | resource | Imagine\Image\ImageInterface $image, integer $width, integer $height, string $mode = ManipulatorInterface::THUMBNAIL_OUTBOUND ) : Imagine\Image\ImageInterface Creates a thumbnail image.
watermark ( string | resource | Imagine\Image\ImageInterface $image, string | resource | Imagine\Image\ImageInterface $watermarkImage, array $start = [0, 0] ) : Imagine\Image\ImageInterface Adds a watermark to an existing image.

Protected Methods

Method Description
createImagine ( ) : Imagine\Image\ImagineInterface Creates an Imagine object based on the specified [[driver]].
ensureImageInterfaceInstance ( string | resource | Imagine\Image\ImageInterface $image ) : Imagine\Image\ImageInterface Takes either file path or ImageInterface. In case of file path, creates an instance of ImageInterface from it.
getBox ( Imagine\Image\BoxInterface $sourceBox, integer $width, integer $height, boolean $keepAspectRatio = true ) : Imagine\Image\BoxInterface Returns box for an image to be created.
getThumbnailBox ( Imagine\Image\BoxInterface $sourceBox, integer $width, integer $height ) : Imagine\Image\BoxInterface Returns box for a thumbnail to be created. If one of the dimensions is set to null, another one is calculated automatically based on width to height ratio of original image box.
isUpscaling ( Imagine\Image\BoxInterface $sourceBox, Imagine\Image\BoxInterface $destinationBox ) : boolean Checks if upscaling is going to happen

Method Details

autorotate() public static method

Rotates an image automatically based on EXIF information.
Since: 2.1.0
public static autorotate ( string | resource | Imagine\Image\ImageInterface $image, string $color = '000000' ) : Imagine\Image\ImageInterface
$image string | resource | Imagine\Image\ImageInterface either ImageInterface, resource or a string containing file path
$color string
return Imagine\Image\ImageInterface

createImagine() protected static method

Creates an Imagine object based on the specified [[driver]].
protected static createImagine ( ) : Imagine\Image\ImagineInterface
return Imagine\Image\ImagineInterface the new `Imagine` object

crop() public static method

For example, ~~~ $obj->crop('path\to\image.jpg', 200, 200, [5, 5]); $point = new \Imagine\Image\Point(5, 5); $obj->crop('path\to\image.jpg', 200, 200, $point); ~~~
public static crop ( string | resource | Imagine\Image\ImageInterface $image, integer $width, integer $height, array $start = [0, 0] ) : Imagine\Image\ImageInterface
$image string | resource | Imagine\Image\ImageInterface either ImageInterface, resource or a string containing file path
$width integer the crop width
$height integer the crop height
$start array the starting point. This must be an array with two elements representing `x` and `y` coordinates.
return Imagine\Image\ImageInterface

ensureImageInterfaceInstance() protected static method

Takes either file path or ImageInterface. In case of file path, creates an instance of ImageInterface from it.
Since: 2.1.0
protected static ensureImageInterfaceInstance ( string | resource | Imagine\Image\ImageInterface $image ) : Imagine\Image\ImageInterface
$image string | resource | Imagine\Image\ImageInterface
return Imagine\Image\ImageInterface

frame() public static method

Adds a frame around of the image. Please note that the image size will increase by $margin x 2.
public static frame ( string | resource | Imagine\Image\ImageInterface $image, integer $margin = 20, string $color = '666', integer $alpha = 100 ) : Imagine\Image\ImageInterface
$image string | resource | Imagine\Image\ImageInterface either ImageInterface, resource or a string containing file path
$margin integer the frame size to add around the image
$color string the frame color
$alpha integer the alpha value of the frame.
return Imagine\Image\ImageInterface

getBox() protected static method

If one of the dimensions is set to null, another one is calculated automatically based on width to height ratio of original image box. If both of the dimensions are set then new dimensions are calculated so that image keeps aspect ratio. You can set $keepAspectRatio to false if you want to force fixed width and height.
Since: 2.1.1
protected static getBox ( Imagine\Image\BoxInterface $sourceBox, integer $width, integer $height, boolean $keepAspectRatio = true ) : Imagine\Image\BoxInterface
$sourceBox Imagine\Image\BoxInterface original image box
$width integer new image width
$height integer new image height
$keepAspectRatio boolean should we keep aspect ratio even if both with and height are set
return Imagine\Image\BoxInterface new image box

getImagine() public static method

Returns the Imagine object that supports various image manipulations.
public static getImagine ( ) : Imagine\Image\ImagineInterface
return Imagine\Image\ImagineInterface the `Imagine` object

getThumbnailBox() protected static method

Returns box for a thumbnail to be created. If one of the dimensions is set to null, another one is calculated automatically based on width to height ratio of original image box.
Since: 2.0.4
protected static getThumbnailBox ( Imagine\Image\BoxInterface $sourceBox, integer $width, integer $height ) : Imagine\Image\BoxInterface
$sourceBox Imagine\Image\BoxInterface original image box
$width integer thumbnail width
$height integer thumbnail height
return Imagine\Image\BoxInterface thumbnail box

isUpscaling() protected static method

Checks if upscaling is going to happen
protected static isUpscaling ( Imagine\Image\BoxInterface $sourceBox, Imagine\Image\BoxInterface $destinationBox ) : boolean
$sourceBox Imagine\Image\BoxInterface
$destinationBox Imagine\Image\BoxInterface
return boolean

resize() public static method

If one of the dimensions is set to null, another one is calculated automatically based on aspect ratio of original image. If both of the dimensions are set then new dimensions are calculated so that image keeps aspect ratio. You can set $keepAspectRatio to false if you want to force fixed width and height.
Since: 2.1.1
public static resize ( string | resource | Imagine\Image\ImageInterface $image, integer $width, integer $height, boolean $keepAspectRatio = true, boolean $allowUpscaling = false ) : Imagine\Image\ImageInterface
$image string | resource | Imagine\Image\ImageInterface either ImageInterface, resource or a string containing file path
$width integer the width in pixels
$height integer the height in pixels
$keepAspectRatio boolean should the image keep aspect ratio
$allowUpscaling boolean should the image be upscaled if needed
return Imagine\Image\ImageInterface

setImagine() public static method

public static setImagine ( Imagine\Image\ImagineInterface $imagine )
$imagine Imagine\Image\ImagineInterface the `Imagine` object.

text() public static method

Draws a text string on an existing image.
public static text ( string | resource | Imagine\Image\ImageInterface $image, string $text, string $fontFile, array $start = [0, 0], array $fontOptions = [] ) : Imagine\Image\ImageInterface
$image string | resource | Imagine\Image\ImageInterface either ImageInterface, resource or a string containing file path
$text string the text to write to the image
$fontFile string the file path or path alias
$start array the starting position of the text. This must be an array with two elements representing `x` and `y` coordinates.
$fontOptions array the font options. The following options may be specified: - color: The font color. Defaults to "fff". - size: The font size. Defaults to 12. - angle: The angle to use to write the text. Defaults to 0.
return Imagine\Image\ImageInterface

thumbnail() public static method

If one of thumbnail dimensions is set to null, another one is calculated automatically based on aspect ratio of original image. Note that calculated thumbnail dimension may vary depending on the source image in this case. If both dimensions are specified, resulting thumbnail would be exactly the width and height specified. How it's achieved depends on the mode. If ImageInterface::THUMBNAIL_OUTBOUND mode is used, which is default, then the thumbnail is scaled so that its smallest side equals the length of the corresponding side in the original image. Any excess outside of the scaled thumbnail’s area will be cropped, and the returned thumbnail will have the exact width and height specified. If thumbnail mode is ImageInterface::THUMBNAIL_INSET, the original image is scaled down so it is fully contained within the thumbnail dimensions. The rest is filled with background that could be configured via [[Image::$thumbnailBackgroundColor]] and [[Image::$thumbnailBackgroundAlpha]].
public static thumbnail ( string | resource | Imagine\Image\ImageInterface $image, integer $width, integer $height, string $mode = ManipulatorInterface::THUMBNAIL_OUTBOUND ) : Imagine\Image\ImageInterface
$image string | resource | Imagine\Image\ImageInterface either ImageInterface, resource or a string containing file path
$width integer the width in pixels to create the thumbnail
$height integer the height in pixels to create the thumbnail
$mode string mode of resizing original image to use in case both width and height specified
return Imagine\Image\ImageInterface

watermark() public static method

Adds a watermark to an existing image.
public static watermark ( string | resource | Imagine\Image\ImageInterface $image, string | resource | Imagine\Image\ImageInterface $watermarkImage, array $start = [0, 0] ) : Imagine\Image\ImageInterface
$image string | resource | Imagine\Image\ImageInterface either ImageInterface, resource or a string containing file path
$watermarkImage string | resource | Imagine\Image\ImageInterface either ImageInterface, resource or a string containing watermark file path
$start array the starting point. This must be an array with two elements representing `x` and `y` coordinates.
return Imagine\Image\ImageInterface

Property Details

$driver public static property

the driver to use. This can be either a single driver name or an array of driver names. If the latter, the first available driver will be used.
public static $driver

$thumbnailBackgroundAlpha public static property

background alpha (transparency) to use when creating thumbnails in ImageInterface::THUMBNAIL_INSET mode with both width and height specified. Default is solid.
Since: 2.0.4
public static $thumbnailBackgroundAlpha

$thumbnailBackgroundColor public static property

background color to use when creating thumbnails in ImageInterface::THUMBNAIL_INSET mode with both width and height specified. Default is white.
Since: 2.0.4
public static $thumbnailBackgroundColor