SimpleImage::overlay PHP Method

overlay() public method

Overlay an image on top of another, works with 24-bit PNG alpha-transparency
public overlay ( string $overlay, string $position = 'center', float | integer $opacity = 1, integer $x_offset, integer $y_offset ) : SimpleImage
$overlay string An image filename or a SimpleImage object
$position string center|top|left|bottom|right|top left|top right|bottom left|bottom right
$opacity float | integer Overlay opacity 0-1
$x_offset integer Horizontal offset in pixels
$y_offset integer Vertical offset in pixels
return SimpleImage
    function overlay($overlay, $position = 'center', $opacity = 1, $x_offset = 0, $y_offset = 0)
    {
        // Load overlay image
        if (!$overlay instanceof SimpleImage) {
            $overlay = new SimpleImage($overlay);
        }
        // Convert opacity
        $opacity = $opacity * 100;
        // Determine position
        switch (strtolower($position)) {
            case 'top left':
                $x = 0 + $x_offset;
                $y = 0 + $y_offset;
                break;
            case 'top right':
                $x = $this->width - $overlay->width + $x_offset;
                $y = 0 + $y_offset;
                break;
            case 'top':
                $x = $this->width / 2 - $overlay->width / 2 + $x_offset;
                $y = 0 + $y_offset;
                break;
            case 'bottom left':
                $x = 0 + $x_offset;
                $y = $this->height - $overlay->height + $y_offset;
                break;
            case 'bottom right':
                $x = $this->width - $overlay->width + $x_offset;
                $y = $this->height - $overlay->height + $y_offset;
                break;
            case 'bottom':
                $x = $this->width / 2 - $overlay->width / 2 + $x_offset;
                $y = $this->height - $overlay->height + $y_offset;
                break;
            case 'left':
                $x = 0 + $x_offset;
                $y = $this->height / 2 - $overlay->height / 2 + $y_offset;
                break;
            case 'right':
                $x = $this->width - $overlay->width + $x_offset;
                $y = $this->height / 2 - $overlay->height / 2 + $y_offset;
                break;
            case 'center':
            default:
                $x = $this->width / 2 - $overlay->width / 2 + $x_offset;
                $y = $this->height / 2 - $overlay->height / 2 + $y_offset;
                break;
        }
        // Perform the overlay
        $this->imagecopymerge_alpha($this->image, $overlay->image, $x, $y, 0, 0, $overlay->width, $overlay->height, $opacity);
        return $this;
    }