Class yii\imagine\BaseImage

Inheritanceyii\imagine\BaseImage
Subclassesyii\imagine\Image
Available since version2.0
Source Code https://github.com/yiisoft/yii2-imagine/blob/master/BaseImage.php

BaseImage provides concrete implementation for yii\imagine\Image.

Do not use BaseImage. Use yii\imagine\Image instead.

Public Properties

Hide inherited properties

PropertyTypeDescriptionDefined By
$driver array|string The driver to use. yii\imagine\BaseImage
$thumbnailBackgroundAlpha string Background alpha (transparency) to use when creating thumbnails in ImageInterface::THUMBNAIL_INSET mode with both width and height specified. yii\imagine\BaseImage
$thumbnailBackgroundColor string Background color to use when creating thumbnails in ImageInterface::THUMBNAIL_INSET mode with both width and height specified. yii\imagine\BaseImage

Public Methods

Hide inherited methods

MethodDescriptionDefined By
autorotate() Rotates an image automatically based on EXIF information. yii\imagine\BaseImage
crop() Crops an image. yii\imagine\BaseImage
frame() Adds a frame around of the image. Please note that the image size will increase by $margin x 2. yii\imagine\BaseImage
getImagine() Returns the Imagine object that supports various image manipulations. yii\imagine\BaseImage
resize() Resizes an image. yii\imagine\BaseImage
setImagine() yii\imagine\BaseImage
text() Draws a text string on an existing image. yii\imagine\BaseImage
thumbnail() Creates a thumbnail image. yii\imagine\BaseImage
watermark() Adds a watermark to an existing image. yii\imagine\BaseImage

Protected Methods

Hide inherited methods

MethodDescriptionDefined By
createImagine() Creates an Imagine object based on the specified $driver. yii\imagine\BaseImage
ensureImageInterfaceInstance() Takes either file path or ImageInterface. In case of file path, creates an instance of ImageInterface from it. yii\imagine\BaseImage
getBox() Returns box for an image to be created. yii\imagine\BaseImage
getThumbnailBox() 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. yii\imagine\BaseImage
isUpscaling() Checks if upscaling is going to happen yii\imagine\BaseImage

Constants

Hide inherited constants

ConstantValueDescriptionDefined By
DRIVER_GD2 'gd2' GD2 driver definition for Imagine implementation using the GD library. yii\imagine\BaseImage
DRIVER_GMAGICK 'gmagick' Gmagick driver definition. yii\imagine\BaseImage
DRIVER_IMAGICK 'imagick' Imagick driver definition. yii\imagine\BaseImage

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 array|string $driver = [self::DRIVER_GMAGICKself::DRIVER_IMAGICKself::DRIVER_GD2]
$thumbnailBackgroundAlpha public static property (available since version 2.0.4)

Background alpha (transparency) to use when creating thumbnails in ImageInterface::THUMBNAIL_INSET mode with both width and height specified. Default is solid.

public static string $thumbnailBackgroundAlpha 100
$thumbnailBackgroundColor public static property (available since version 2.0.4)

Background color to use when creating thumbnails in ImageInterface::THUMBNAIL_INSET mode with both width and height specified. Default is white.

public static string $thumbnailBackgroundColor 'FFF'

Method Details

autorotate() public static method (available since version 2.1.0)

Rotates an image automatically based on EXIF information.

public static \Imagine\Image\ImageInterface autorotate ( $image, $color '000000' )
$image string|resource|\Imagine\Image\ImageInterface

Either ImageInterface, resource or a string containing file path

$color string
createImagine() protected static method

Creates an Imagine object based on the specified $driver.

protected static \Imagine\Image\ImagineInterface createImagine ( )
return \Imagine\Image\ImagineInterface

The new Imagine object

throws yii\base\InvalidConfigException

if $driver is unknown or the system doesn't support any $driver.

crop() public static method

Crops an image.

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 \Imagine\Image\ImageInterface crop ( $image, $width, $height, array $start = [00] )
$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.

throws yii\base\InvalidParamException

if the $start parameter is invalid

ensureImageInterfaceInstance() protected static method (available since version 2.1.0)

Takes either file path or ImageInterface. In case of file path, creates an instance of ImageInterface from it.

protected static \Imagine\Image\ImageInterface ensureImageInterfaceInstance ( $image )
$image string|resource|\Imagine\Image\ImageInterface
throws yii\base\InvalidParamException
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 \Imagine\Image\ImageInterface frame ( $image, $margin 20, $color '666', $alpha 100 )
$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.

getBox() protected static method (available since version 2.1.1)

Returns box for an image 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.

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.

protected static \Imagine\Image\BoxInterface getBox ( \Imagine\Image\BoxInterface $sourceBox, $width, $height, $keepAspectRatio true )
$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 \Imagine\Image\ImagineInterface getImagine ( )
return \Imagine\Image\ImagineInterface

The Imagine object

getThumbnailBox() protected static method (available since version 2.0.4)

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.

protected static \Imagine\Image\BoxInterface getThumbnailBox ( \Imagine\Image\BoxInterface $sourceBox, $width, $height )
$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 boolean isUpscaling ( \Imagine\Image\BoxInterface $sourceBox, \Imagine\Image\BoxInterface $destinationBox )
$sourceBox \Imagine\Image\BoxInterface
$destinationBox \Imagine\Image\BoxInterface
resize() public static method (available since version 2.1.1)

Resizes an image.

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.

public static \Imagine\Image\ImageInterface resize ( $image, $width, $height, $keepAspectRatio true, $allowUpscaling false )
$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

setImagine() public static method

public static void setImagine ( $imagine )
$imagine \Imagine\Image\ImagineInterface

The Imagine object.

text() public static method

Draws a text string on an existing image.

public static \Imagine\Image\ImageInterface text ( $image, $text, $fontFile, array $start = [00], array $fontOptions = [] )
$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.
throws yii\base\InvalidParamException

if $fontOptions is invalid

thumbnail() public static method

Creates a thumbnail image.

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 defined via settings parameter.

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 yii\imagine\Image::$thumbnailBackgroundColor and yii\imagine\Image::$thumbnailBackgroundAlpha.

public static \Imagine\Image\ImageInterface thumbnail ( $image, $width, $height, $settings = \Imagine\Image\ManipulatorInterface::THUMBNAIL_OUTBOUND )
$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

$settings integer

Settings for resizing original image, one or more of the ManipulatorInterface::THUMBNAIL_ flags (joined with |)

watermark() public static method

Adds a watermark to an existing image.

public static \Imagine\Image\ImageInterface watermark ( $image, $watermarkImage, array $start = [00] )
$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.

throws yii\base\InvalidParamException

if $start is invalid