php制作圆形用户头像的实例_自定义封装类源代码


Posted in PHP onSeptember 18, 2017

思路

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

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

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

3.圆形蒙版:作为最上层,在蒙版中绘制圆形,并设置为透明

如图:

php制作圆形用户头像的实例_自定义封装类源代码

代码如下:

主功能类 avatar.class.php

<?php
class avatar
{
 private $fileName; //文件的绝对路径(或基于最终调用文件的相对路径)
 private $rgb; //颜色索引(数组 array(255,255,0) 或 16进制值 ffff00/#ffff00/ff0/#ff0)
 private $size; //图像大小
 private $imgInfo; //图像信息
 
 /**
  * 初始化
  * Enter description here ...
  * @param string $fileName 文件的绝对路径(或基于最终调用文件的相对路径)
  * @param mixed $rgb 颜色索引(数组 array(255,255,0) 或 16进制值 ffff00/#ffff00/ff0/#ff0)
  * @param int $size 图像大小
  */
 public function __construct($fileName, $rgb, $size)
 {
  $this->fileName = $fileName;
  
  if(is_array($rgb)){
   $this->rgb = $rgb; //rgb颜色数组 array(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->createRGB($rgb); //16进制值 ffff00
  }
  
  $this->size = $size;
  
  $this->imgInfo = getimagesize($this->fileName);
  
  if(!$this->imgInfo){
   throw Exception("无法读取图像文件");
  }
  if(!in_array($this->imgInfo[2], array(2,3))){
   //仅允许jpg和png
   throw Exception("图像格式不支持");
  }
 }
 
 /**
  * 显示图像
  * Enter description here ...
  */
 public function show()
 {
  header("content-type:image/png");
  
  $shadow = $this->createshadow(); //遮罩图片
  
  //创建一个方形图片
  $imgbk = imagecreatetruecolor($this->size, $this->size); //目标图片
  
  switch ($this->imgInfo[2]){
   case 2:
    $imgfk = imagecreatefromjpeg($this->fileName); //原素材图片
    break;
   case 3:
    $imgfk = imagecreatefrompng($this->fileName); //原素材图片
   default:
    return ;
    break;
  }
  
  
  $realSize = $this->imgInfo[0]<$this->imgInfo[1]? $this->imgInfo[0] : $this->imgInfo[1];
  
  imagecopyresized($imgbk, $imgfk, 0, 0, 0, 0, $this->size, $this->size, $realSize, $realSize);
  imagecopymerge($imgbk, $shadow, 0, 0, 0, 0, $this->size, $this->size, 100);
  
  //创建图像
  imagepng($imgbk);
  
  //销毁资源
  imagedestroy($imgbk);
  imagedestroy($imgfk);
  imagedestroy($shadow);
 }
 
 /**
  * 创建一个圆形遮罩
  * Enter description here ...
  * @param array 10进制颜色数组
  */
 private function createshadow()
 {
  
  $img = imagecreatetruecolor($this->size, $this->size);
  
  imageantialias($img, true); //开启抗锯齿
  
  $color_bg = imagecolorallocate($img, $this->rgb[0], $this->rgb[1], $this->rgb[2]); //背景色
  $color_fg = imagecolorallocate($img, 0, 0, 0); //前景色,主要用来创建圆形
  
  imagefilledrectangle($img, 0, 0, 200, 200, $color_bg);
  imagefilledarc($img, 100, 100, 200, 200, 0, 0, $color_fg, IMG_ARC_PIE);
  
  imagecolortransparent($img, $color_fg); //将前景色转换为透明
  
  
  return $img;
 }
 
 /**
  * 将字符形式16进制串转为10进制
  * Enter description here ...
  * @param $str
  */
 private function getIntFromHexStr($str)
 {
  $format = '0123456789abcdef';
  
  $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);
  }
  
  return $sum;
 }
 
 /**
  * 将16进制颜色转为10进制颜色值数组(RGB)
  * Enter description here ...
  * @param $str 16进制串(如:ff9900)
  */
 private function createRGB($str)
 {
  $rgb = array();
  if(strlen($str) != 6){
   $rgb[] = 0xff;
   $rgb[] = 0xff;
   $rgb[] = 0xff;
   return $rgb; //默认白色
  }
 
  $rgb[] = $this->getIntFromHexStr(substr($str, 0, 2));
  $rgb[] = $this->getIntFromHexStr(substr($str, 2, 2));
  $rgb[] = $this->getIntFromHexStr(substr($str, 4, 2));
  
  return $rgb;
  
 }
}

以上这篇php制作圆形用户头像的实例_自定义封装类源代码就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

PHP 相关文章推荐
对Session和Cookie的区分与解释
Mar 16 PHP
pw的一个放后门的方法分析
Oct 08 PHP
PHP得到某段时间区间的时间戳 php定时任务
Apr 12 PHP
php利用腾讯ip分享计划获取地理位置示例分享
Jan 20 PHP
smarty实现多级分类的方法
Dec 05 PHP
windows中为php安装mongodb与memcache
Jan 06 PHP
PHP5.3新特性小结
Feb 14 PHP
完美解决Thinkphp3.2中插入相同数据的问题
Aug 01 PHP
php处理多图上传压缩代码功能
Jun 13 PHP
PHP PDOStatement::setAttribute讲解
Feb 01 PHP
php-fpm中max_children的配置
Mar 15 PHP
6个常见的 PHP 安全性攻击实例和阻止方法
Dec 16 PHP
PHP中使用jQuery+Ajax实现分页查询多功能操作(示例讲解)
Sep 17 #PHP
PHP实现深度优先搜索算法(DFS,Depth First Search)详解
Sep 16 #PHP
PHP实现广度优先搜索算法(BFS,Broad First Search)详解
Sep 16 #PHP
PHP实现的迪科斯彻(Dijkstra)最短路径算法实例
Sep 16 #PHP
PHP环形链表实现方法示例
Sep 15 #PHP
PHP实现的链式队列结构示例
Sep 15 #PHP
PHP基于堆栈实现的高级计算器功能示例
Sep 15 #PHP
You might like
PHP+Mysql+jQuery实现动态展示信息
2011/10/08 PHP
php构造函数实例讲解
2013/11/13 PHP
推荐几个开源的微信开发项目
2014/12/28 PHP
symfony表单与页面实现技巧
2015/01/26 PHP
Discuz!X中SESSION机制实例详解
2015/09/23 PHP
PHP5.3新特性小结
2016/02/14 PHP
php 5.4 全新的代码复用Trait详解
2017/01/05 PHP
简介JavaScript中的push()方法的使用
2015/06/09 Javascript
javaScript日期工具类DateUtils详解
2017/12/08 Javascript
Node.js模块全局安装路径配置方法
2018/05/17 Javascript
解决vue-cli单页面手机应用input点击手机端虚拟键盘弹出盖住input问题
2018/08/25 Javascript
p5.js实现故宫橘猫赏秋图动画
2019/10/23 Javascript
小程序中设置缓存过期的实现方法
2020/01/14 Javascript
vue路由权限校验功能的实现代码
2020/06/07 Javascript
[01:02:46]VGJ.S vs NB 2018国际邀请赛小组赛BO2 第二场 8.18
2018/08/19 DOTA
python 运算符 供重载参考
2009/06/11 Python
python下setuptools的安装详解及No module named setuptools的解决方法
2017/07/06 Python
Python错误处理操作示例
2018/07/18 Python
python使用pygame模块实现坦克大战游戏
2020/03/25 Python
Django 路由控制的实现
2019/07/17 Python
对Django 中request.get和request.post的区别详解
2019/08/12 Python
使用Python将字符串转换为格式化的日期时间字符串
2019/09/01 Python
Django 创建后台,配置sqlite3教程
2019/11/18 Python
Python对wav文件的重采样实例
2020/02/25 Python
openCV提取图像中的矩形区域
2020/07/21 Python
CSS3动画之利用requestAnimationFrame触发重新播放功能
2019/09/11 HTML / CSS
比利时网上药店: Drogisterij.net
2017/03/17 全球购物
南非最大的花卉和送礼服务:NetFlorist
2017/09/13 全球购物
松本清官方海外旗舰店:日本最大的药妆连锁店
2017/11/21 全球购物
莫斯科隐形眼镜网上商店:Linzi
2019/07/22 全球购物
软件工程专业推荐信
2013/10/28 职场文书
酒店总经理岗位职责
2014/03/17 职场文书
文案策划专业自荐信
2014/07/07 职场文书
毕业设计答辩开场白
2015/05/29 职场文书
教师节领导致辞
2015/07/29 职场文书
驾驶员管理制度范本
2015/08/06 职场文书