本文实例讲述了PHP 图片合成、仿微信群头像的方法。分享给大家供大家参考,具体如下:
参考文章:
作者:凯歌~,php图片合成方法(多张图片合成一张)https://www.zzvips.com/article/177268.html。
经过测试,略作调整和注释,感谢分享。
欢迎提出改善优化意见!
示例代码:
?
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
|
/**
* 合成图片
* @param array $pic_list [图片列表数组]
* @param boolean $is_save [是否保存,true保存,false输出到浏览器]
* @param string $save_path [保存路径]
* @return boolean|string
*/
function getGroupAvatar( $pic_list = array (), $is_save =false, $save_path = '' ){
//验证参数
if ( empty ( $pic_list ) || empty ( $save_path )){
return false;
}
if ( $is_save ){
//如果需要保存,需要传保存地址
if ( empty ( $save_path )){
return false;
}
}
// 只操作前9个图片
$pic_list = array_slice ( $pic_list , 0, 9);
//设置背景图片宽高
$bg_w = 150; // 背景图片宽度
$bg_h = 150; // 背景图片高度
//新建一个真彩色图像作为背景
$background = imagecreatetruecolor( $bg_w , $bg_h );
//为真彩色画布创建白灰色背景,再设置为透明
$color = imagecolorallocate( $background , 202, 201, 201);
imagefill( $background , 0, 0, $color );
imageColorTransparent( $background , $color );
//根据图片个数设置图片位置
$pic_count = count ( $pic_list );
$lineArr = array (); //需要换行的位置
$space_x = 3;
$space_y = 3;
$line_x = 0;
switch ( $pic_count ) {
case 1: // 正中间
$start_x = intval ( $bg_w /4); // 开始位置X
$start_y = intval ( $bg_h /4); // 开始位置Y
$pic_w = intval ( $bg_w /2); // 宽度
$pic_h = intval ( $bg_h /2); // 高度
break ;
case 2: // 中间位置并排
$start_x = 2;
$start_y = intval ( $bg_h /4) + 3;
$pic_w = intval ( $bg_w /2) - 5;
$pic_h = intval ( $bg_h /2) - 5;
$space_x = 5;
break ;
case 3:
$start_x = 40; // 开始位置X
$start_y = 5; // 开始位置Y
$pic_w = intval ( $bg_w /2) - 5; // 宽度
$pic_h = intval ( $bg_h /2) - 5; // 高度
$lineArr = array (2);
$line_x = 4;
break ;
case 4:
$start_x = 4; // 开始位置X
$start_y = 5; // 开始位置Y
$pic_w = intval ( $bg_w /2) - 5; // 宽度
$pic_h = intval ( $bg_h /2) - 5; // 高度
$lineArr = array (3);
$line_x = 4;
break ;
case 5:
$start_x = 30; // 开始位置X
$start_y = 30; // 开始位置Y
$pic_w = intval ( $bg_w /3) - 5; // 宽度
$pic_h = intval ( $bg_h /3) - 5; // 高度
$lineArr = array (3);
$line_x = 5;
break ;
case 6:
$start_x = 5; // 开始位置X
$start_y = 30; // 开始位置Y
$pic_w = intval ( $bg_w /3) - 5; // 宽度
$pic_h = intval ( $bg_h /3) - 5; // 高度
$lineArr = array (4);
$line_x = 5;
break ;
case 7:
$start_x = 53; // 开始位置X
$start_y = 5; // 开始位置Y
$pic_w = intval ( $bg_w /3) - 5; // 宽度
$pic_h = intval ( $bg_h /3) - 5; // 高度
$lineArr = array (2,5);
$line_x = 5;
break ;
case 8:
$start_x = 30; // 开始位置X
$start_y = 5; // 开始位置Y
$pic_w = intval ( $bg_w /3) - 5; // 宽度
$pic_h = intval ( $bg_h /3) - 5; // 高度
$lineArr = array (3,6);
$line_x = 5;
break ;
case 9:
$start_x = 5; // 开始位置X
$start_y = 5; // 开始位置Y
$pic_w = intval ( $bg_w /3) - 5; // 宽度
$pic_h = intval ( $bg_h /3) - 5; // 高度
$lineArr = array (4,7);
$line_x = 5;
break ;
}
foreach ( $pic_list as $k => $pic_path ) {
$kk = $k + 1;
if ( in_array( $kk , $lineArr ) ) {
$start_x = $line_x ;
$start_y = $start_y + $pic_h + $space_y ;
}
//获取图片文件扩展类型和mime类型,判断是否是正常图片文件
//非正常图片文件,相应位置空着,跳过处理
$image_mime_info = @ getimagesize ( $pic_path );
if ( $image_mime_info && ! empty ( $image_mime_info [ 'mime' ])){
$mime_arr = explode ( '/' , $image_mime_info [ 'mime' ]);
if ( is_array ( $mime_arr ) && $mime_arr [0] == 'image' && ! empty ( $mime_arr [1])){
switch ( $mime_arr [1]) {
case 'jpg' :
case 'jpeg' :
$imagecreatefromjpeg = 'imagecreatefromjpeg' ;
break ;
case 'png' :
$imagecreatefromjpeg = 'imagecreatefrompng' ;
break ;
case 'gif' :
default :
$imagecreatefromjpeg = 'imagecreatefromstring' ;
$pic_path = file_get_contents ( $pic_path );
break ;
}
//创建一个新图像
$resource = $imagecreatefromjpeg ( $pic_path );
//将图像中的一块矩形区域拷贝到另一个背景图像中
// $start_x,$start_y 放置在背景中的起始位置
// 0,0 裁剪的源头像的起点位置
// $pic_w,$pic_h copy后的高度和宽度
imagecopyresized( $background , $resource , $start_x , $start_y ,0,0, $pic_w , $pic_h ,imagesx( $resource ),imagesy( $resource ));
}
}
// 最后两个参数为原始图片宽度和高度,倒数两个参数为copy时的图片宽度和高度
$start_x = $start_x + $pic_w + $space_x ;
}
if ( $is_save ){
$dir = pathinfo ( $save_path ,PATHINFO_DIRNAME);
if (! is_dir ( $dir )){
$file_create_res = mkdir ( $dir ,0777,true);
if (! $file_create_res ){
return false; //没有创建成功
}
}
$res = imagejpeg( $background , $save_path );
imagedestroy( $background );
if ( $res ){
return true;
} else {
return false;
}
} else {
//直接输出
header( "Content-type: image/jpg" );
imagejpeg( $background );
imagedestroy( $background );
}
}
|
调用示例:
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
$img = array (
'http://localhost/1.png' ,
'http://localhost/2.png' ,
'http://localhost/3.png' ,
'http://localhost/4.png' ,
'http://localhost/5.png' ,
'http://localhost/6.png' ,
'http://localhost/7.png' ,
'http://localhost/8.png' ,
'http://localhost/9.png' ,
'http://localhost/10.png' ,
);
$a = getGroupAvatar( $img ,1, './img/123.jpg' );
var_dump( $a );
|
希望本文所述对大家PHP程序设计有所帮助。
原文链接:https://www.cnblogs.com/gyfluck/p/11365343.html
相关文章
猜你喜欢
- 个人服务器网站搭建:如何选择合适的服务器提供商? 2025-06-10
- ASP.NET自助建站系统中如何实现多语言支持? 2025-06-10
- 64M VPS建站:如何选择最适合的网站建设平台? 2025-06-10
- ASP.NET本地开发时常见的配置错误及解决方法? 2025-06-10
- ASP.NET自助建站系统的数据库备份与恢复操作指南 2025-06-10
TA的动态
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
您的支持,是我们最大的动力!
热门文章
-
2025-05-29 74
-
2025-05-29 30
-
2025-05-27 53
-
2025-05-29 83
-
2025-05-25 17
热门评论