PHP 打水印功能

时间:2019-11-25
本文章向大家介绍PHP 打水印功能,主要包括PHP 打水印功能使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
    /**
     * @param $str  需要打水印的文字
     * @param int $size 文字大小
     * @param int $red  文字的颜色 rgb r
     * @param int $gree 文字的颜色 rgb g
     * @param int $blue 文字的颜色 rgb b
     * @param string $path 生成后图片的路径
     * @author 宁佳兵
     */
    function watermark( $str, $size=48, $red=150, $gree=150, $blue=150, $path='/Uploads/FS/photo_stamp.png' ){
        $im = imagecreatefrompng(__STATIC__.'/images/lucency.png'); //引入透明图片
        $font= __STATIC__."/fonts/msyh.ttf";//字体文件引入
        // 从 GD 手动创建水印图像
        $stamp = imagecreatetruecolor(2480, 3508);
        //设置字体背景
        imagefilledrectangle($stamp, 0, 0, 2480, 3508, 0xFFFFFF);
        $color = imagecolorallocate($stamp, $red, $gree, $blue);
        //打满全屏
        for ($i=1;$i<25;$i++){
            imagefttext($stamp, $size,50,0,150 * $i, $color, $font, $str);

            for ($j=1;$j<15;$j++){
                imagefttext($stamp, $size,50,200 * $j,150 * $i, $color, $font, $str);
            }
        }

        // 以 50% 的透明度合并水印和图像
        imagecopymerge($im, $stamp, 0, 0, 0, 0, imagesx($stamp), imagesy($stamp), 20);
        // 将图像保存到文件,并释放内存 水印
        imagepng($im, $path);
        imagedestroy($im);
    }

  

原文地址:https://www.cnblogs.com/ningjiabing/p/11926800.html