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

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

        //创建水印图像资源
        $water = new Imagick(realpath($source));
        $info  = [$water->getImageWidth(), $water->getImageHeight()];

        /* 设定水印位置 */
        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('不支持的水印位置类型');
                }
        }

        //创建绘图资源
        $draw = new ImagickDraw();
        $draw->composite($water->getImageCompose(), $x, $y, $info[0], $info[1], $water);

        if ('gif' == $this->info['type']) {
            $img = $this->im->coalesceImages();
            $this->im->destroy(); //销毁原图

            do {
                //添加水印
                $img->drawImage($draw);
            } while ($img->nextImage());

            //压缩图片
            $this->im = $img->deconstructImages();
            $img->destroy(); //销毁零时图片

        } else {
            //添加水印
            $this->im->drawImage($draw);
        }

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

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

        //创建水印图像资源
        $water = new Imagick(realpath($source));
        $info  = [$water->getImageWidth(), $water->getImageHeight()];

        /* 设定水印位置 */
        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('不支持的水印位置类型');
                }
        }

        //创建绘图资源
        $draw = new ImagickDraw();
        $draw->composite($water->getImageCompose(), $x, $y, $info[0], $info[1], $water);

        if ('gif' == $this->info['type']) {
            $img = $this->im->coalesceImages();
            $this->im->destroy(); //销毁原图

            do {
                //添加水印
                $img->drawImage($draw);
            } while ($img->nextImage());

            //压缩图片
            $this->im = $img->deconstructImages();
            $img->destroy(); //销毁零时图片

        } else {
            //添加水印
            $this->im->drawImage($draw);
        }

        //销毁水印资源
        $draw->destroy();
        $water->destroy();
    }
public function water ( $ source , $ locate = THINKIMAGE_WATER_SOUTHEAST ) { //资源检测 if ( empty ( $ this -> im ) ) { throw new \ Exception ( '没有可以被添加水印的图像资源'); } if ( ! is_file ( $ source ) ) { throw new \ Exception ( '水印图像不存在'); } //创建水印图像资源 $ water = new Imagick ( realpath ( $ source ) ) ; $ info = [ $ water -> getImageWidth ( ) , $ water -> getImageHeight ( ) ] ; /* 设定水印位置 */ 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 ( '不支持的水印位置类型'); } } //创建绘图资源 $ draw = new ImagickDraw ( ) ; $ draw -> composite ( $ water -> getImageCompose ( ) , $ x , $ y , $ info [ 0 ] , $ info [ 1 ] , $ water ) ; if ( 'gif' == $ this -> info [ 'type' ] ) { $ img = $ this -> im -> coalesceImages ( ) ; $ this -> im -> destroy ( ) ; //销毁原图 do { //添加水印 $ img -> drawImage ( $ draw ) ; } while ( $ img -> nextImage ( ) ) ; //压缩图片 $ this -> im = $ img -> deconstructImages ( ) ; $ img -> destroy ( ) ; //销毁零时图片 } else { //添加水印 $ this -> im -> drawImage ( $ draw ) ; } //销毁水印资源 $ draw -> destroy ( ) ; $ water -> destroy ( ) ; }
添加水印

@param  string  $source 水印图片路径
@param  integer $locate 水印位置
@param  integer $alpha  水印透明度
添加水印