function Image::getKeywordOffset

Returns the offset in pixels from the anchor.

Parameters

string $anchor: The anchor ('top', 'left', 'bottom', 'right', 'center').

int $current_size: The current size, in pixels.

int $new_size: The new size, in pixels.

Return value

int The offset from the anchor, in pixels.

Throws

\InvalidArgumentException When the $anchor argument is not valid.

4 calls to Image::getKeywordOffset()
CropImageEffect::applyEffect in core/modules/image/src/Plugin/ImageEffect/CropImageEffect.php
Applies an image effect to the image object.
ImageTest::testGetKeywordOffset in core/tests/Drupal/Tests/Component/Utility/ImageTest.php
@covers ::getKeywordOffset
ImageTest::testInvalidGetKeywordOffset in core/tests/Drupal/Tests/Component/Utility/ImageTest.php
@covers ::getKeywordOffset
ScaleAndCropImageEffect::applyEffect in core/modules/image/src/Plugin/ImageEffect/ScaleAndCropImageEffect.php
Applies an image effect to the image object.

File

core/lib/Drupal/Component/Utility/Image.php, line 75

Class

Image
Provides helpers to operate on images.

Namespace

Drupal\Component\Utility

Code

public static function getKeywordOffset(string $anchor, int $current_size, int $new_size) : int {
    return match ($anchor) {    'bottom', 'right' => $current_size - $new_size,
        'center' => (int) round($current_size / 2 - $new_size / 2),
        'top', 'left' => 0,
        default => throw new \InvalidArgumentException("Invalid anchor '{$anchor}' provided to getKeywordOffset()"),
    
    };
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.