Grafika\Gd\Editor::blend PHP 메소드

blend() 공개 메소드

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.
리턴 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 end X
        $loopEndX = $image2->getWidth() - 1;
        $canvasEndX = $offsetX + $image2->getWidth() - 1;
        if ($canvasEndX > $image1->getWidth() - 1) {
            $diff = $canvasEndX - ($image1->getWidth() - 1);
            $loopEndX -= $diff;
        }
        // Loop start Y
        $loopStartY = 0;
        $canvasStartY = $offsetY;
        if ($canvasStartY < 0) {
            $diff = 0 - $canvasStartY;
            $loopStartY += $diff;
        }
        // Loop end Y
        $loopEndY = $image2->getHeight() - 1;
        $canvasEndY = $offsetY + $image2->getHeight() - 1;
        if ($canvasEndY > $image1->getHeight() - 1) {
            $diff = $canvasEndY - ($image1->getHeight() - 1);
            $loopEndY -= $diff;
        }
        $w = $image1->getWidth();
        $h = $image1->getHeight();
        $gd1 = $image1->getCore();
        $gd2 = $image2->getCore();
        $canvas = imagecreatetruecolor($w, $h);
        imagecopy($canvas, $gd1, 0, 0, 0, 0, $w, $h);
        $type = strtolower($type);
        if ($type === 'normal') {
            if ($opacity !== 1) {
                $this->opacity($image2, $opacity);
            }
            imagecopy($canvas, $gd2, $loopStartX + $offsetX, $loopStartY + $offsetY, 0, 0, $image2->getWidth(), $image2->getHeight());
        } else {
            if ($type === 'multiply') {
                $this->_blendMultiply($canvas, $gd1, $gd2, $loopStartY, $loopEndY, $loopStartX, $loopEndX, $offsetX, $offsetY, $opacity);
            } else {
                if ($type === 'overlay') {
                    $this->_blendOverlay($canvas, $gd1, $gd2, $loopStartY, $loopEndY, $loopStartX, $loopEndX, $offsetX, $offsetY, $opacity);
                } else {
                    if ($type === 'screen') {
                        $this->_blendScreen($canvas, $gd1, $gd2, $loopStartY, $loopEndY, $loopStartX, $loopEndX, $offsetX, $offsetY, $opacity);
                    } else {
                        throw new \Exception(sprintf('Invalid blend type "%s".', $type));
                    }
                }
            }
        }
        imagedestroy($gd1);
        // Free resource
        $image1 = new Image($canvas, $image1->getImageFile(), $w, $h, $image1->getType());
        return $this;
    }