宝哥软件园

一个例子 使循环用户头像由php _ custom包类源代码

编辑:宝哥软件园 来源:互联网 时间:2021-09-03

思考

使用图层设计的方法,总共需要创建3个图像图层

1.底层:最后生成的图像

2.真实用户头像:作为中间层,用户上传的真实头像图片

3.圆形遮罩:作为顶层,在遮罩中画一个圆,并将其设置为透明

如图所示:

代码如下:

主要功能类别avatar.class.php

?phpclass avatar { private $ FIlename;//文件的绝对路径(或基于最终调用文件的相对路径)私人$ rgb/颜色索引(数组数组(255,255,0)或16进制值ffff00/#ffff00/ff0/#ff0)私有$ size//图像大小private $ imgInfo//图像信息/** * 初始化*在此输入描述.* @param字符串$fileName文件的绝对路径(或基于最终调用文件的相对路径)* @param mixed $rgb颜色索引(数组数组(255,255,0)或16进制值ffff 00/# ffff 00/ff0/# ff0)* @ param int $ size图像大小*/public function _ _ construct($ FIlename,$rgb,$ size){ $ this-FIlename=$ FIlename;if(is _ array($ RGB)){ $ this-RGB=$ RGB;//rgb颜色数组数组(255,255,0) }else{ //有的人喜欢带#号$rgb=trim($rgb,' # ');//处理缩写形式if(strlen($ RGB)=3){ $ _ tmp=$ RGB[0].$rgb[0].$rgb[1].$rgb[1].$rgb[2]。$ RGB[2];$ RGB=$ _ tmp } $ this-RGB=$ this-create RGb($ RGB);//16进制值ffff 00 } $ this-size=$ size;$ this-imgInfo=getimagesize($ this-fileName);if(!$ this-IMginFo){ 0引发异常('无法读取图像文件');} if(!in_array($this-imgInfo[2],array(2,3)){//仅允许使用联合图象专家组文件交换格式存储的编码图像文件扩展名和巴布亚新几内亚抛出异常(""图像格式不支持');} } /** * 显示图像*在此输入描述.*/public function show(){ header(' content-type : image/png ');$ shadow=$ this-create shadow();//遮罩图片//创建一个方形图片$ imgbk=imagecreatetrue color($ this-size,$ this-size);//目标图片switch($ this-imgInfo[2]){ case 2: $ imgfk=imagecreatefrom JPEG($ this-fileName);//原素材图片打破;case : $ img fk=imagecreatefrompng($ this-FIlename);//原素材图片默认值:返回;打破;} $真实大小=$ this-IMginfo[0]$ this-IMginfo[1]?$ this-IMginfo[0]: $ this-IMginfo[1];imagecopyresized($imgbk,$imgfk,0,0,0,$this-size,$this-size,$realSize,$ real size);imagecopymerge($imgbk,$shadow,0,0,0,$this-size,$this-size,100);//创建图像图像png($ img bk);//销毁资源图像销毁($ img bk);图像销毁($ imgfk);图像破坏(阴影);} /** * 创建一个圆形遮罩*在此输入描述.* @param数组10进制颜色数组*/私有函数create shadow(){ $ img=imagecreatetrue color($ this-size,$ this-size);图像反IAS($ img,true);//开启抗锯齿$ color _ BG=image color allocate($ img,$this-rgb[0],$this-rgb[1],$ this-RGB[2]);//背景色$ color _ fg=image color allocate($ img,0,0,0);//前景色,主要用来创建圆形imagefiledrectangle($ img,0,0,200,200,$ color _ BG);imagefiledarc($ img,100,100,200,200,0,0,$color_fg,IMG _ ARC _ PIE);imagecolortransparent($img,$ color _ fg);//将前景色转换为透明返回$ img} /** *将字符形式16进制串转为10进制*在此输入描述.* @ param $ str */private function getintfromchexstr($ str){ $ format=' 0123456789 abcdef ';$ sum=0;for($i=strlen($str)-1,$c=0,$ j=0;$ I=$ c;$i -,$j ){ $index=strpos($format,$ str[$ I]);//strpos从0计算$sum=$index * pow(16,$ j);}返回$ sum} /** *将16进制颜色转为10进制颜色值数组(RGB) *在此输入描述.* @param $str 16进制串(如:ff9900) */private函数创建RGb($ str){ $ RGb=array();if(strlen($str)!=6){ $ RGB[]=0xff;$ RGB[]=0x ff;$ RGB[]=0x ff;返回$ rgb/默认白色} $ RGB[]=$ this-GetintFromHexStr(substr($ str,0,2));$ RGB[]=$ this-GetintFromHexStr(substr($ str,2,2));$ RGB[]=$ this-GetintFromHexStr(substr($ str,4,2));返回$ rgb}}以上这篇服务器端编程语言(专业超文本预处理器的缩写)制作圆形用户头像的实例_自定义封装类源代码就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持我们。

更多资讯
游戏推荐
更多+