public function createMosaicGroupAvatar($pic_list = array(), $bg_w = 396, $bg_h = 396) {

        if (!$pic_list) {

            $result = new \stdClass();

            $result->img_id = 0;

            $result->url = '';

            return $result;
} $pic_list = array_slice($pic_list, 0, 9); // 只操作前9个图片 $background = imagecreatetruecolor($bg_w, $bg_h); // 背景图片 $color = imagecolorallocate($background, 216, 216, 216); // 为真彩色画布创建白色背景,再设置为透明
imagefill($background, 0, 0, $color); //区域填充
imageColorTransparent($background, $color); // 将某个颜色定义为透明色 $pic_count = count($pic_list);
$lineArr = array(); // 需要换行的位置
$space_x = 3;
$space_y = 3;
$line_x = $pic_h = $start_y = 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 = 124; // 开始位置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 = 85.5; // 开始位置X
$start_y = 85.5; // 开始位置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 = 85.5; // 开始位置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 = 166.5; // 开始位置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 = 80.5; // 开始位置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;
}
$image_info = getimagesize($pic_path);
$image_suffix = end(explode('/', end($image_info)));
switch ($image_suffix) {
case 'png':
$resource = imagecreatefrompng($pic_path);
break;
default:
$resource = imagecreatefromjpeg($pic_path); } $image_width = imagesx($resource);
$image_height = imagesy($resource); // 图片圆角处理
$radius = 0; //20 40-貌似不错 50也不错
// lt(左上角)
$lt_corner = $this->get_lt_rounder_corner($radius);
imagecopymerge($resource, $lt_corner, 0, 0, 0, 0, $radius, $radius, 100); // lb(左下角)
$lb_corner = imagerotate($lt_corner, 90, 0);
imagecopymerge($resource, $lb_corner, 0, $image_height - $radius, 0, 0, $radius, $radius, 100); // rb(右上角)
$rb_corner = imagerotate($lt_corner, 180, 0);
imagecopymerge($resource, $rb_corner, $image_width - $radius, $image_height - $radius, 0, 0, $radius, $radius, 100);
// rt(右下角)
$rt_corner = imagerotate($lt_corner, 270, 0);
imagecopymerge($resource, $rt_corner, $image_width - $radius, 0, 0, 0, $radius, $radius, 100); // $start_x,$start_y copy图片在背景中的位置
// 0,0 被copy图片的位置 $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;
} $fname = 'xshop' . date('YmdHis').rand(100, 999); $tmp_img_path = '/tmp/'.$fname.'.jpg'; imagejpeg($background, $tmp_img_path); /*
      上传图片
      */unlink($tmp_img_path); // 释放内存
imagedestroy($background); return $wx_img_res;
} /**
* [产生一个弧角图片]
* @param [type] $radius [弧度]
* @return [type] [description]
*/
public function get_lt_rounder_corner($radius) {
// $radius:弧角图片的大小
$img = imagecreatetruecolor($radius, $radius);
$bgcolor = imagecolorallocate($img, 223, 223, 223);
// $bgcolor = imagecolorallocate($img, 216, 216, 216);
$fgcolor = imagecolorallocate($img, 0, 0, 0);
imagefill($img, 0, 0, $bgcolor);
// $radius,$radius:以图像的右下角开始画弧
// $radius*2, $radius*2:已宽度、高度画弧
// 180, 270:指定了角度的起始和结束点
// fgcolor:指定颜色
imagefilledarc($img, $radius, $radius, $radius * 2, $radius * 2, 180, 270, $fgcolor, IMG_ARC_PIE);
// 设置颜色为透明
imagecolortransparent($img, $fgcolor);
return $img;
}

效果:

PHP GD库---之头像合成九宫格的更多相关文章

  1. PHP 使用GD库合成带二维码和圆形头像的海报步骤以及源码实现

    PHP 使用GD库合成带二维码和圆形头像的海报步骤以及源码实现 之前记录过一篇文章,不过那只是简单将二维码合成到海报中去,这次还要合成头像,而且是圆形.所需要素材就不一一列举,直接代码吧 1.先获取用 ...

  2. PHP 使用GD库合成带二维码的海报步骤以及源码实现

    PHP 使用GD库合成带二维码的海报步骤以及源码实现 在做微信项目开发过程中,经常会遇到图片合成的问题,比如将用户的二维码合成到宣传海报中,那么,遇到这种情况,利用PHP的GD库也是很容易实现的,实现 ...

  3. PHP->利用GD库新建图像

    1.确认php中GD库是否开启 在PHP配置文件php.ini中查找extension=php_gd2.dll,去掉前边的(分号) ';' 即可,一般php是默认开启的 2.绘画步骤 创建一个画布(画 ...

  4. GD库使用小结---2

    接着上一篇.GD库可以折腾很多用法出来,当然得跟画图相关,除了前面的验证码.水印外,还可以进行图片的缩放,裁剪.旋转等操作,这在很多应用中可以见到. 1. 加水印 前面已经知道,我们可以使用image ...

  5. GD库使用小结---1

    因为一开始,“大家”都说一般任务中,用php操作图片不常见,像我们这种基本业务型的,就更用不到了,所以先别看,偶就没有看.现在有机会了自然要来玩一把. 以前学过C#的GDI+,交了课程设计后忘得一干二 ...

  6. GD库 图片缩略图 图片水印

    /** * GD库 图片缩略图 *//*$image = imagecreatefromjpeg("1.jpg");var_dump($image);exit;$width = i ...

  7. PHP面向对象——GD库实现图片水印和缩略图

    今天的实现目标就是使用GD库完成对图片加水印和图 片缩略图两个功能 动身前逻辑准备 属性: 路径 功能: 构造方法 生成水印的方法 获取 图片信息 获取位置信息(123 456 789) 创建图片资源 ...

  8. PHP中GD库的使用

    1.基本步骤 <?php /** * Created by PhpStorm. * User: jiqing * Date: 18-4-9 * Time: 上午9:34 * 熟悉步骤 */ // ...

  9. PHP的GD库

    GD库 PHP通过GD库,可以对JPG.PNG.GIF.SWF等图片进行处理.GD库常用在图片加水印,验证码生成等方面. 绘制线条 要对图形进行操作,首先要新建一个画布,通过imagecreatetr ...

随机推荐

  1. JDBC连接中Class.forName("")到底干了什么?

    思考了一个问题,Class.forName("***");到底干了什么? 我们知道Class.forName( )静态方法的目的是为了动态加载类,但是一般来说,一个类forName ...

  2. Unity注入

    [此文引用别人,作为随笔自己看.]今天写<WCF技术剖析(卷2)>关于<WCF扩展>一章,举了“如何通过WCF扩展实现与IoC框架(以Unity为例)集成”(<通过自定义 ...

  3. 阿里云服务器Centos7安装FastDFS(一)

    安装步骤一 安装FastDFS需要安装:gcc.libevent.libfastcommon.FastDFS(包括tracker和storage) 安装gcc 判断是否安装了gcc gcc -V 如果 ...

  4. css常用操作

    对齐操作 1.使用margin属性进行水平对齐     margin-left:auto;    margin-right:auto; 2.使用position属性进行左右对齐      3.使用fl ...

  5. 关于Linux系统启动时出现UVD not responding, Trying to reset the vcpu问题的解决

    本人的老古董笔记本!不知道什么时候显卡烧坏了 每次启动Linux的时候就会出现错误,信息如下: UVD not responding, trying to reset the VCPU! 讲道理,显卡 ...

  6. windows下安装pm2

    安装pm2 npm install pm2 -g 添加系统环境变量 PM2_HOME=C:\Users\PCONE\.pm2 打开新的cmd命令行窗口,执行以下命令来安装服务 pm2-service- ...

  7. github的pull Request使用

    场景: teamA要一起做一个项目,选择用github管理自己的代码仓库,这时userA在github上新建了一个远程仓库,其他人需要通过pull request来实现提交.那么,问题来了,pull ...

  8. 11gR2 新特性: Rebootless Restart

    众所周知,当集群出现问题时,例如某个节点丢失网络心跳,或者不能够访问表决盘,或者节点出现了严重的性能问题等,CRS会选择将某个节点的OS 重启,以便保证集群的一致性.当然,大部分的重启都是由CRS的核 ...

  9. redis的一些问题总结,转载自infoq

    Redis是时下比较流行的Nosql技术.在优酷我们使用Redis Cluster构建了一套内存存储系统,项目代号蓝鲸.到目前为止集群有700+节点,即将达到作者推荐的最大集群规模1000节点.集群从 ...

  10. 01_11_Strtus2简单数据验证

    01_11_Strtus2简单数据验证 1. 引入struts标签 <%@taglib uri="/struts-tags" prefix="s" %&g ...