tp5 通过imagefttext 写入文字水印

$bigImgPath = 'http://' . $_SERVER['SERVER_NAME'].'/'.$filename;  //获得底图的路径
        $img = imagecreatefromstring(file_get_contents($bigImgPath));
        putenv('GDFONTPATH=' . realpath('.'));
        $font = "MSYH";//字体,字体文件需保存到相应文件夹下   //liunx默认没有字体需要下载
        $black = imagecolorallocate($img, 0, 0, 0);//字体颜色 RGB

        $fontSize = 20;   //字体大小
        $circleSize = 0; //旋转角度
        $left = 10;      //左边距
        $top = 30;       //顶边距

        imagefttext($img, $fontSize, $circleSize, $left, $top, $black, $font, '测试一下');
        /*如果putenv()报错putenv() has been disabled for security reasons  是因为tp5.1没有调用putenv  可以将$font 改为
         $font = realpath('MSYH.TTF'); //根目录下的路径*/


        list($bgWidth, $bgHight, $bgType) = getimagesize($bigImgPath);
//        return $img;
        if($bgType == 3){
            header("Content-type: image/png");
            $filename = 'uploads/qrcode/' . rand(10000, 99999) . time() . '.png';
            imagepng($img,$filename);  //在 images 目录下就会生成一个 circle.png 文件,上面也可设置相应的保存目录及文件名。
        }
//        print_r($filename);
        return $filename;
        imagedestroy($img);

注意:我这的图片默认的都是png格式  所以我直接写的$bgType == 3

如果是其他图片可以写成:、

list($bgWidth, $bgHight, $bgType) = getimagesize($bigImgPath);
switch ($bgType) {
    case 1: //gif
        header('Content-Type:image/gif');
        imagegif($img);
        break;
    case 2: //jpg
        header('Content-Type:image/jpg');
        imagejpeg($img);
        break;
    case 3: //png
        header('Content-Type:image/png');
        imagepng($img,"images/circle.png");  //在 images 目录下就会生成一个 circle.png 文件,上面也可设置相应的保存目录及文件名。
        break;
    default:
        break;
}
imagedestroy($img);
将生成好的文件上传到服务器上用  imagepng  方法(jpg和gif未尝试)

liunx服务器下默认没有  MSYH.TTF 字体需要下载  下载后放在根目录下  tp5  也就是public目录下

这个字体不能直接引用  需写成:

putenv('GDFONTPATH=' . realpath('.'));
$font = "MSYH";//字体,字体文件需保存到相应文件夹下
亲测可用

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值