Grafika\Imagick\Editor::blend PHP Method

blend() public method

Blend two images together with the first image as the base and the second image on top. Supports several blend modes.
public blend ( Image &$image1, Image $image2, string $type = 'normal', float $opacity = 1, string $position = 'top-left', integer $offsetX, integer $offsetY ) : Editor
$image1 Image The base image.
$image2 Image The image placed on top of the base image.
$type string The blend mode. Can be: normal, multiply, overlay or screen.
$opacity float The opacity of $image2. Possible values 0.0 to 1.0 where 0.0 is fully transparent and 1.0 is fully opaque. Defaults to 1.0.
$position string The position of $image2 on $image1. Possible values top-left, top-center, top-right, center-left, center, center-right, bottom-left, bottom-center, bottom-right and smart. Defaults to top-left.
$offsetX integer Number of pixels to add to the X position of $image2.
$offsetY integer Number of pixels to add to the Y position of $image2.
return Editor
    public function blend(&$image1, $image2, $type = 'normal', $opacity = 1.0, $position = 'top-left', $offsetX = 0, $offsetY = 0)
    {
        // Turn into position object
        $position = new Position($position, $offsetX, $offsetY);
        // Position is for $image2. $image1 is canvas.
        list($offsetX, $offsetY) = $position->getXY($image1->getWidth(), $image1->getHeight(), $image2->getWidth(), $image2->getHeight());
        // Check if it overlaps
        if ($offsetX >= $image1->getWidth() or $offsetX + $image2->getWidth() <= 0 or $offsetY >= $image1->getHeight() or $offsetY + $image2->getHeight() <= 0) {
            throw new \Exception('Invalid blending. Image 2 is outside the canvas.');
        }
        // Loop start X
        $loopStartX = 0;
        $canvasStartX = $offsetX;
        if ($canvasStartX < 0) {
            $diff = 0 - $canvasStartX;
            $loopStartX += $diff;
        }
        // Loop start Y
        $loopStartY = 0;
        $canvasStartY = $offsetY;
        if ($canvasStartY < 0) {
            $diff = 0 - $canvasStartY;
            $loopStartY += $diff;
        }
        if ($opacity !== 1) {
            $this->opacity($image2, $opacity);
        }
        $type = strtolower($type);
        if ($type === 'normal') {
            $image1->getCore()->compositeImage($image2->getCore(), \Imagick::COMPOSITE_OVER, $loopStartX + $offsetX, $loopStartY + $offsetY);
        } else {
            if ($type === 'multiply') {
                $image1->getCore()->compositeImage($image2->getCore(), \Imagick::COMPOSITE_MULTIPLY, $loopStartX + $offsetX, $loopStartY + $offsetY);
            } else {
                if ($type === 'overlay') {
                    $image1->getCore()->compositeImage($image2->getCore(), \Imagick::COMPOSITE_OVERLAY, $loopStartX + $offsetX, $loopStartY + $offsetY);
                } else {
                    if ($type === 'screen') {
                        $image1->getCore()->compositeImage($image2->getCore(), \Imagick::COMPOSITE_SCREEN, $loopStartX + $offsetX, $loopStartY + $offsetY);
                    } else {
                        throw new \Exception(sprintf('Invalid blend type "%s".', $type));
                    }
                }
            }
        }
        return $this;
    }