图片上添加多行文字水印

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/**
 * Text Watermark Point:
 *   #1      #2    #3
 *   #4      #5    #6
 *   #7      #8    #9
 */
 
/**
 * 给图片添加文字水印 可控制位置,旋转,多行文字    **有效字体未验证**
 * @param string $imgurl  图片地址
 * @param array $text   水印文字(多行以'|'分割)
 * @param int $fontSize 字体大小
 * @param type $color 字体颜色  如: 255,255,255
 * @param int $point 水印位置
 * @param type $font 字体
 * @param int $angle 旋转角度  允许值:  0-90   270-360 不含
 * @param string $newimgurl  新图片地址 默认使用后缀命名图片
 * @return boolean
 */
function createWordsWatermark($imgurl, $text, $fontSize = '14', $color = '0,0,0', $point = '1', $font = 'simhei.ttf', $angle = 0, $newimgurl = '') {
 
    $imageCreateFunArr = array('image/jpeg' => 'imagecreatefromjpeg', 'image/png' => 'imagecreatefrompng', 'image/gif' => 'imagecreatefromgif');
    $imageOutputFunArr = array('image/jpeg' => 'imagejpeg', 'image/png' => 'imagepng', 'image/gif' => 'imagegif');
 
//获取图片的mime类型
    $imgsize = getimagesize($imgurl);
 
    if (empty($imgsize)) {
        return false; //not image
    }
 
    $imgWidth = $imgsize[0];
    $imgHeight = $imgsize[1];
    $imgMime = $imgsize['mime'];
 
    if (!isset($imageCreateFunArr[$imgMime])) {
        return false; //do not have create img function
    }
    if (!isset($imageOutputFunArr[$imgMime])) {
        return false; //do not have output img function
    }
 
    $imageCreateFun = $imageCreateFunArr[$imgMime];
    $imageOutputFun = $imageOutputFunArr[$imgMime];
 
    $im = $imageCreateFun($imgurl);
 
    /*
     * 参数判断
     */
    $color = explode(',', $color);
    $text_color = imagecolorallocate($im, intval($color[0]), intval($color[1]), intval($color[2])); //文字水印颜色
    $point = intval($point) > 0 && intval($point) < 10 ? intval($point) : 1; //文字水印所在的位置
    $fontSize = intval($fontSize) > 0 ? intval($fontSize) : 14;
    $angle = ($angle >= 0 && $angle < 90 || $angle > 270 && $angle < 360) ? $angle : 0; //判断输入的angle值有效性
    $fontUrl = "C:\phpstudy_pro\WWW\lecent_prison_fund_sys\upload\simhei.ttf"; //有效字体未验证
    $text = explode('|', $text);
    $newimgurl = $newimgurl ? $newimgurl : $imgurl . '_WordsWatermark.jpg'; //新图片地址 统一图片后缀
 
    /**
     *  根据文字所在图片的位置方向,计算文字的坐标
     * 首先获取文字的宽,高, 写一行文字,超出图片后是不显示的
     */
    $textLength = count($text) - 1;
    $maxtext = 0;
    foreach ($text as $val) {
        $maxtext = strlen($val) > strlen($maxtext) ? $val : $maxtext;
    }
 
    $textSize = imagettfbbox($fontSize, 0, $fontUrl, $maxtext);
    $textWidth = $textSize[2] - $textSize[1]; //文字的最大宽度
    $textHeight = $textSize[1] - $textSize[7]; //文字的高度
    $lineHeight = $textHeight + 100; //文字的行高
//是否可以添加文字水印 只有图片的可以容纳文字水印时才添加
    if ($textWidth + 40 > $imgWidth || $lineHeight * $textLength + 40 > $imgHeight) {
        return false; //图片太小了,无法添加文字水印
    }
 
    if ($point == 1) { //左上角
        $porintLeft = 20;
        $pointTop = 20;
    } elseif ($point == 2) { //上中部
        $porintLeft = floor(($imgWidth - $textWidth) / 2);
        $pointTop = 20;
    } elseif ($point == 3) { //右上部
        $porintLeft = $imgWidth - $textWidth - 20;
        $pointTop = 20;
    } elseif ($point == 4) { //左中部
        $porintLeft = 20;
        $pointTop = floor(($imgHeight - $textLength * $lineHeight) / 2);
    } elseif ($point == 5) { //正中部
        $porintLeft = $imgWidth;
        $pointTop = floor(($imgHeight - $textLength * $lineHeight) / 2);
    } elseif ($point == 6) { //右中部
        $porintLeft = $imgWidth - $textWidth - 20;
        $pointTop = floor(($imgHeight - $textLength * $lineHeight) / 2);
    } elseif ($point == 7) { //左下部
        $porintLeft = 20;
        $pointTop = $imgHeight - $textLength * $lineHeight - 20;
    } elseif ($point == 8) { //中下部
        $porintLeft = floor(($imgWidth - $textWidth) / 2);
        $pointTop = $imgHeight - $textLength * $lineHeight - 20;
    } elseif ($point == 9) { //右下部
        $porintLeft = $imgWidth - $textWidth - 20;
        $pointTop = $imgHeight - $textLength * $lineHeight - 20;
    }
 
//如果有angle旋转角度,则重新设置 top ,left 坐标值
    if ($angle != 0) {
        if ($angle < 90) {
            $diffTop = ceil(sin($angle * M_PI / 180) * $textWidth);
 
            if (in_array($point, array(1, 2, 3))) {// 上部 top 值增加
                $pointTop += $diffTop;
            } elseif (in_array($point, array(4, 5, 6))) {// 中部 top 值根据图片总高判断
                if ($textWidth > ceil($imgHeight / 2)) {
                    $pointTop += ceil(($textWidth - $imgHeight / 2) / 2);
                }
            }
        } elseif ($angle > 270) {
            $diffTop = ceil(sin((360 - $angle) * M_PI / 180) * $textWidth);
 
            if (in_array($point, array(7, 8, 9))) {// 上部 top 值增加
                $pointTop -= $diffTop;
            } elseif (in_array($point, array(4, 5, 6))) {// 中部 top 值根据图片总高判断
                if ($textWidth > ceil($imgHeight / 2)) {
                    $pointTop = ceil(($imgHeight - $diffTop) / 2);
                }
            }
        }
    }
 
 
 
    foreach ($text as $key => $val) {
        imagettftext($im, $fontSize, 30, random_int(0,$porintLeft), ($pointTop + $key * $lineHeight), $text_color, $fontUrl, $val);
    }
 
// 输出图像
// JPEG图像生成的图像的质量的是一个范围从0(最低质量,最小的文件大小)到100(最高质量,最大文件大小)
// PNG生成图像的质量范围从0到9的
    $imageOutputFun($im, $newimgurl, 80);
 
// 释放内存
    imagedestroy($im);
    return $newimgurl;
}
 
 
 
 
//$imgurl, $text, $fontSize='14', $color='0,0,0', $point='1', $font = 'simhei.ttf', $angle=0, $newimgurl=''
$img = createWordsWatermark('../upload/bg.jpeg', '资金管理系统|资金管理系统|资金管理系统|资金管理系统|资金管理系统|资金管理系统', '14', '150,150,150', '5', '24', '30', '../upload/newpic.jpg');
 
$imfile = '../upload/newpic.jpg';//原图
 
$origim = imagecreatefromjpeg($imfile);//从 JPEG 文件或 URL 新建一图像
 
$w=imagesx($origim);//原图宽度
 
$h=imagesy($origim);//原图高度
 
$newimg = imagecreatetruecolor($w, $h);//返回一个图像标识符,代表了一幅大小为 x_size 和 y_size 的黑色图像。imagecreatetruecolor//
 
$color=imagecolorallocatealpha($newimg,0,0,0,0);//为一幅图像分配颜色 + alpha; 和 imagecolorallocate() 相同,但多了一个额外的透明度参数 alpha,其值从 0 到 127。0 表示完全不透明,127 表示完全透明。
 
imagecolortransparent($newimg,$color);//将某个颜色定义为透明色
 
imagefill($newimg,0,0,$color);//区域填充;resource $image , int $x , int $y , int $color
 
imagecopy($origim,$newimg, 0,0, 0, 0,$w, $h);//拷贝图像的一部分;将 src_im 图像中坐标从 src_x,src_y 开始,宽度为 src_w,高度为 src_h 的一部分拷贝到 dst_im 图像中坐标为 dst_x 和 dst_y 的位置上。
 
imagejpeg($origim, '../upload/newpic.jpg');//输出图象到浏览器或文件。;resource $image [, string $filename [, int $quality ]]
 
echo '<img src="' . $img . '" />';

 

posted @   程序员小艺  阅读(145)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 25岁的心里话
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
历史上的今天:
2020-07-18 phpstudy配置nginx虚拟主机
点击右上角即可分享
微信分享提示