服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|JavaScript|易语言|

服务器之家 - 编程语言 - PHP教程 - PHP 图片合成、仿微信群头像的方法示例

PHP 图片合成、仿微信群头像的方法示例

2021-09-14 16:03学知无涯 PHP教程

这篇文章主要介绍了PHP 图片合成、仿微信群头像的方法,涉及php针对图片的转换、生成等相关操作技巧,需要的朋友可以参考下

本文实例讲述了PHP 图片合成、仿微信群头像的方法。分享给大家供大家参考,具体如下:

参考文章:

作者:凯歌~,php图片合成方法(多张图片合成一张)http://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

延伸 · 阅读

精彩推荐