Image_GD::_do_watermark PHP Method

_do_watermark() protected method

Execute a watermarking.
protected _do_watermark ( $watermark, integer $offset_x, integer $offset_y, integer $opacity ) : void
$offset_x integer offset from the left
$offset_y integer offset from the top
$opacity integer opacity of watermark
return void
    protected function _do_watermark($watermark, $offset_x, $offset_y, $opacity)
    {
        if (!function_exists('imagelayereffect')) {
            throw new CException('This method requires imagelayereffect, which is only available in the bundled version of GD');
        }
        // Loads image if not yet loaded
        $this->_load_image();
        // Create the watermark image resource
        $overlay = imagecreatefromstring($watermark->render());
        imagesavealpha($overlay, TRUE);
        // Get the width and height of the watermark
        $width = imagesx($overlay);
        $height = imagesy($overlay);
        if ($opacity < 100) {
            // Convert an opacity range of 0-100 to 127-0
            $opacity = round(abs($opacity * 127 / 100 - 127));
            // Allocate transparent gray
            $color = imagecolorallocatealpha($overlay, 127, 127, 127, $opacity);
            // The transparent image will overlay the watermark
            imagelayereffect($overlay, IMG_EFFECT_OVERLAY);
            // Fill the background with the transparent color
            imagefilledrectangle($overlay, 0, 0, $width, $height, $color);
        }
        // Alpha blending must be enabled on the background!
        imagealphablending($this->_image, TRUE);
        if (imagecopy($this->_image, $overlay, $offset_x, $offset_y, 0, 0, $width, $height)) {
            // Destroy the overlay image
            imagedestroy($overlay);
        }
    }