您正在使用 IPV4 [3.142.171.180] 访问本站,您本次已经查看了 1 页
用户名: 密 码: 验证码:     用QQ登录本站
首页 软件 编程 笑话 知识 公告 台风 日历 计算器
[公益]保护绿色环境,构建和谐社会       悟空收录网      

【腾讯云】 爆款2核2G3M云服务器首年 61元,叠加红包再享折上折      
[公益] 地球是我家,绿化靠大家      
2024年 劳动节 013
2024年 端午节 053
2025年 元 旦 258
2025年 春 节 286
 
您现在的位置:首页 >> PHP >> 内容
本类新增
本类热门
PHP 图片合成、仿微信群头像的方法示例
内容摘要: /***合成图片*@paramarray$pic_list[图片列表数组]*@paramboolean$is_save[是否保存,true保存,false输出到浏览器]*@paramstring$save_path[保存路径]*@returnboolean|string*/functiongetGroupAvatar($pic_list=array(),$i......
/**

*合成图片

*@paramarray$pic_list[图片列表数组]

*@paramboolean$is_save[是否保存,true保存,false输出到浏览器]

*@paramstring$save_path[保存路径]

*@returnboolean|string

*/

functiongetGroupAvatar($pic_list=array(),$is_save=false,$save_path=''){

//验证参数

if(emptyempty($pic_list)||emptyempty($save_path)){

returnfalse;

}

if($is_save){

//如果需要保存,需要传保存地址

if(emptyempty($save_path)){

returnfalse;

}

}

//只操作前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){

case1://正中间

$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;

case2://中间位置并排

$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;

case3:

$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;

case4:

$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;

case5:

$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;

case6:

$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;

case7:

$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;

case8:

$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;

case9:

$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_listas$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&&!emptyempty($image_mime_info['mime'])){

$mime_arr=explode('/',$image_mime_info['mime']);

if(is_array($mime_arr)&&$mime_arr[0]=='image'&&!emptyempty($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_hcopy后的高度和宽度

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){

returnfalse;//没有创建成功

}

}

$res=imagejpeg($background,$save_path);

imagedestroy($background);

if($res){

returntrue;

}else{

returnfalse;

}

}else{

//直接输出

header("Content-type:image/jpg");

imagejpeg($background);

imagedestroy($background);

}

}

调用示例:

$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实现批量修改文件名的方法示例

 

下一篇:使PHP自定义函数返回多个值

发布日期:2022/3/4
手机扫二维码直达本页
发布时间:13:47:39
点  击:2
录  入:壹家怡园
相关文章
Baidu
YiJiaCMS 7.3.8 build231228(MSSQL) 闽ICP备05000814号-1
本空间由腾讯云(轻量应用服务器)提供,Cloudflare提供加速防护
运行时间载入中.....