src/thinkextend/image/driver/Gd.php
public function water($source, $locate = THINKIMAGE_WATER_SOUTHEAST)
    {
        //资源检测
        if (empty($this->im)) {
            throw new Exception('没有可以被添加水印的图像资源');
        }

        if (!is_file($source)) {
            throw new Exception('水印图像不存在');
        }

        //获取水印图像信息
        $info = getimagesize($source);
        if (false === $info || (IMAGETYPE_GIF === $info[2] && empty($info['bits']))) {
            throw new Exception('非法水印文件');
        }

        //创建水印图像资源
        $fun   = 'imagecreatefrom' . image_type_to_extension($info[2], false);
        $water = $fun($source);

        //设定水印图像的混色模式
        imagealphablending($water, true);

        /* 设定水印位置 */
        switch ($locate) {
            /* 右下角水印 */
            case THINKIMAGE_WATER_SOUTHEAST:
                $x = $this->info['width'] - $info[0];
                $y = $this->info['height'] - $info[1];
                break;

            /* 左下角水印 */
            case THINKIMAGE_WATER_SOUTHWEST:
                $x = 0;
                $y = $this->info['height'] - $info[1];
                break;

            /* 左上角水印 */
            case THINKIMAGE_WATER_NORTHWEST:
                $x = $y = 0;
                break;

            /* 右上角水印 */
            case THINKIMAGE_WATER_NORTHEAST:
                $x = $this->info['width'] - $info[0];
                $y = 0;
                break;

            /* 居中水印 */
            case THINKIMAGE_WATER_CENTER:
                $x = ($this->info['width'] - $info[0]) / 2;
                $y = ($this->info['height'] - $info[1]) / 2;
                break;

            /* 下居中水印 */
            case THINKIMAGE_WATER_SOUTH:
                $x = ($this->info['width'] - $info[0]) / 2;
                $y = $this->info['height'] - $info[1];
                break;

            /* 右居中水印 */
            case THINKIMAGE_WATER_EAST:
                $x = $this->info['width'] - $info[0];
                $y = ($this->info['height'] - $info[1]) / 2;
                break;

            /* 上居中水印 */
            case THINKIMAGE_WATER_NORTH:
                $x = ($this->info['width'] - $info[0]) / 2;
                $y = 0;
                break;

            /* 左居中水印 */
            case THINKIMAGE_WATER_WEST:
                $x = 0;
                $y = ($this->info['height'] - $info[1]) / 2;
                break;

            default:
                /* 自定义水印坐标 */
                if (is_array($locate)) {
                    list($x, $y) = $locate;
                } else {
                    throw new Exception('不支持的水印位置类型');
                }
        }

        do {
            //添加水印
            $src = imagecreatetruecolor($info[0], $info[1]);
            // 调整默认颜色
            $color = imagecolorallocate($src, 255, 255, 255);
            imagefill($src, 0, 0, $color);

            imagecopy($src, $this->im, 0, 0, $x, $y, $info[0], $info[1]);
            imagecopy($src, $water, 0, 0, 0, 0, $info[0], $info[1]);
            imagecopymerge($this->im, $src, $x, $y, 0, 0, $info[0], $info[1], 100);

            //销毁零时图片资源
            imagedestroy($src);
        } while (!empty($this->gif) && $this->gifNext());

        //销毁水印资源
        imagedestroy($water);
        return $this;
    }
public function water($source, $locate = THINKIMAGE_WATER_SOUTHEAST)
    {
        //资源检测
        if (empty($this->im)) {
            throw new Exception('没有可以被添加水印的图像资源');
        }

        if (!is_file($source)) {
            throw new Exception('水印图像不存在');
        }

        //获取水印图像信息
        $info = getimagesize($source);
        if (false === $info || (IMAGETYPE_GIF === $info[2] && empty($info['bits']))) {
            throw new Exception('非法水印文件');
        }

        //创建水印图像资源
        $fun   = 'imagecreatefrom' . image_type_to_extension($info[2], false);
        $water = $fun($source);

        //设定水印图像的混色模式
        imagealphablending($water, true);

        /* 设定水印位置 */
        switch ($locate) {
            /* 右下角水印 */
            case THINKIMAGE_WATER_SOUTHEAST:
                $x = $this->info['width'] - $info[0];
                $y = $this->info['height'] - $info[1];
                break;

            /* 左下角水印 */
            case THINKIMAGE_WATER_SOUTHWEST:
                $x = 0;
                $y = $this->info['height'] - $info[1];
                break;

            /* 左上角水印 */
            case THINKIMAGE_WATER_NORTHWEST:
                $x = $y = 0;
                break;

            /* 右上角水印 */
            case THINKIMAGE_WATER_NORTHEAST:
                $x = $this->info['width'] - $info[0];
                $y = 0;
                break;

            /* 居中水印 */
            case THINKIMAGE_WATER_CENTER:
                $x = ($this->info['width'] - $info[0]) / 2;
                $y = ($this->info['height'] - $info[1]) / 2;
                break;

            /* 下居中水印 */
            case THINKIMAGE_WATER_SOUTH:
                $x = ($this->info['width'] - $info[0]) / 2;
                $y = $this->info['height'] - $info[1];
                break;

            /* 右居中水印 */
            case THINKIMAGE_WATER_EAST:
                $x = $this->info['width'] - $info[0];
                $y = ($this->info['height'] - $info[1]) / 2;
                break;

            /* 上居中水印 */
            case THINKIMAGE_WATER_NORTH:
                $x = ($this->info['width'] - $info[0]) / 2;
                $y = 0;
                break;

            /* 左居中水印 */
            case THINKIMAGE_WATER_WEST:
                $x = 0;
                $y = ($this->info['height'] - $info[1]) / 2;
                break;

            default:
                /* 自定义水印坐标 */
                if (is_array($locate)) {
                    list($x, $y) = $locate;
                } else {
                    throw new Exception('不支持的水印位置类型');
                }
        }

        do {
            //添加水印
            $src = imagecreatetruecolor($info[0], $info[1]);
            // 调整默认颜色
            $color = imagecolorallocate($src, 255, 255, 255);
            imagefill($src, 0, 0, $color);

            imagecopy($src, $this->im, 0, 0, $x, $y, $info[0], $info[1]);
            imagecopy($src, $water, 0, 0, 0, 0, $info[0], $info[1]);
            imagecopymerge($this->im, $src, $x, $y, 0, 0, $info[0], $info[1], 100);

            //销毁零时图片资源
            imagedestroy($src);
        } while (!empty($this->gif) && $this->gifNext());

        //销毁水印资源
        imagedestroy($water);
        return $this;
    }
public function water ( $ source , $ locate = THINKIMAGE_WATER_SOUTHEAST ) { //资源检测 if ( empty ( $ this -> im ) ) { throw new Exception ( '没有可以被添加水印的图像资源'); } if ( ! is_file ( $ source ) ) { throw new Exception ( '水印图像不存在'); } //获取水印图像信息 $ info = getimagesize ( $ source ) ; if ( false === $ info || ( IMAGETYPE_GIF === $ info [ 2 ] && empty ( $ info [ 'bits' ] ) ) ) { throw new Exception ( '非法水印文件'); } //创建水印图像资源 $ fun = 'imagecreatefrom' . image_type_to_extension ( $ info [ 2 ] , false ) ; $ water = $ fun ( $ source ) ; //设定水印图像的混色模式 imagealphablending ( $ water , true ) ; /* 设定水印位置 */ switch ( $ locate ) { /* 右下角水印 */ case THINKIMAGE_WATER_SOUTHEAST : $ x = $ this -> info [ 'width' ] - $ info [ 0 ] ; $ y = $ this -> info [ 'height' ] - $ info [ 1 ] ; break ; /* 左下角水印 */ case THINKIMAGE_WATER_SOUTHWEST : $ x = 0 ; $ y = $ this -> info [ 'height' ] - $ info [ 1 ] ; break ; /* 左上角水印 */ case THINKIMAGE_WATER_NORTHWEST : $ x = $ y = 0 ; break ; /* 右上角水印 */ case THINKIMAGE_WATER_NORTHEAST : $ x = $ this -> info [ 'width' ] - $ info [ 0 ] ; $ y = 0 ; break ; /* 居中水印 */ case THINKIMAGE_WATER_CENTER : $ x = ( $ this -> info [ 'width' ] - $ info [ 0 ] ) / 2 ; $ y = ( $ this -> info [ 'height' ] - $ info [ 1 ] ) / 2 ; break ; /* 下居中水印 */ case THINKIMAGE_WATER_SOUTH : $ x = ( $ this -> info [ 'width' ] - $ info [ 0 ] ) / 2 ; $ y = $ this -> info [ 'height' ] - $ info [ 1 ] ; break ; /* 右居中水印 */ case THINKIMAGE_WATER_EAST : $ x = $ this -> info [ 'width' ] - $ info [ 0 ] ; $ y = ( $ this -> info [ 'height' ] - $ info [ 1 ] ) / 2 ; break ; /* 上居中水印 */ case THINKIMAGE_WATER_NORTH : $ x = ( $ this -> info [ 'width' ] - $ info [ 0 ] ) / 2 ; $ y = 0 ; break ; /* 左居中水印 */ case THINKIMAGE_WATER_WEST : $ x = 0 ; $ y = ( $ this -> info [ 'height' ] - $ info [ 1 ] ) / 2 ; break ; default : /* 自定义水印坐标 */ if ( is_array ( $ locate ) ) { list ( $ x , $ y ) = $ locate ; } else { throw new Exception ( '不支持的水印位置类型'); } } do { //添加水印 $ src = imagecreatetruecolor ( $ info [ 0 ] , $ info [ 1 ] ) ; // 调整默认颜色 $ color = imagecolorallocate ( $ src , 255 , 255 , 255 ) ; imagefill ( $ src , 0 , 0 , $ color ) ; imagecopy ( $ src , $ this -> im , 0 , 0 , $ x , $ y , $ info [ 0 ] , $ info [ 1 ] ) ; imagecopy ( $ src , $ water , 0 , 0 , 0 , 0 , $ info [ 0 ] , $ info [ 1 ] ) ; imagecopymerge ( $ this -> im , $ src , $ x , $ y , 0 , 0 , $ info [ 0 ] , $ info [ 1 ] , 100 ) ; //销毁零时图片资源 imagedestroy ( $ src ) ; } while ( ! empty ( $ this -> gif ) && $ this -> gifNext ( ) ) ; //销毁水印资源 imagedestroy ( $ water ) ; return $ this ; }
添加水印

@param  string $source 水印图片路径
@param int     $locate 水印位置

@return $this
@throws Exception
@internal param int $alpha 水印透明度
添加水印