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 相关文章推荐
论坛头像随机变换代码
Oct 09 PHP
为查询结果建立向后/向前按钮
Oct 09 PHP
PHP分页显示制作详细讲解
Dec 05 PHP
php5 apache 2.2 webservice 创建与配置(java)
Jan 27 PHP
php htmlspecialchars()与shtmlspecialchars()函数的深入分析
Jun 05 PHP
Laravel 5框架学习之环境与配置
Apr 08 PHP
PHP页面间传递值和保持值的方法
Aug 24 PHP
IIS 7.5 asp Session超时时间设置方法
Apr 17 PHP
PHP实现登陆并抓取微信列表中最新一组微信消息的方法
Jul 10 PHP
php + WebUploader实现图片批量上传功能
May 06 PHP
PHP实现关键字搜索后描红功能示例
Jul 03 PHP
laravel Model 执行事务的实现
Oct 10 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中的CMS的涵义
2007/03/11 PHP
php操作mongoDB实例分析
2014/12/29 PHP
PHP中的魔术方法总结和使用实例
2015/05/11 PHP
php格式化json函数示例代码
2016/05/12 PHP
Thinkphp5框架实现获取数据库数据到视图的方法
2019/08/14 PHP
javascript与CSS复习(《精通javascript》)
2010/06/29 Javascript
JavaScript基础语法让人疑惑的地方小结
2012/05/23 Javascript
jquery高效反选具体实现
2013/05/05 Javascript
window.location.href的用法(动态输出跳转)
2014/08/09 Javascript
js判断滚动条是否已到页面最底部或顶部实例
2014/11/20 Javascript
jQuery中[attribute!=value]选择器用法实例
2014/12/31 Javascript
javascript基于prototype实现类似OOP继承的方法
2015/12/16 Javascript
易被忽视的js事件问题总结
2016/05/14 Javascript
Vue.js每天必学之表单控件绑定
2016/09/05 Javascript
移动端脚本框架Hammer.js
2016/12/15 Javascript
Ajax验证用户名或昵称是否已被注册
2017/04/05 Javascript
jQuery事件_动力节点Java学院整理
2017/07/05 jQuery
JS解决position:sticky的兼容性问题的方法
2017/10/17 Javascript
vue中实现在外部调用methods的方法(推荐)
2018/02/08 Javascript
Node.js使用MongoDB的ObjectId作为查询条件的方法
2019/09/10 Javascript
Python复制文件操作实例详解
2015/11/10 Python
Python for Informatics 第11章之正则表达式(四)
2016/04/21 Python
利用python3随机生成中文字符的实现方法
2017/11/24 Python
Python+pandas计算数据相关系数的实例
2018/07/03 Python
Python numpy线性代数用法实例解析
2019/11/15 Python
提升python处理速度原理及方法实例
2019/12/25 Python
快速解决jupyter notebook启动需要密码的问题
2020/04/21 Python
Python离线安装各种库及pip的方法
2020/11/28 Python
30年同学聚会邀请函
2014/01/25 职场文书
教堂婚礼主持词
2014/03/14 职场文书
卖车协议书
2014/04/21 职场文书
初中学生期末评语
2014/04/24 职场文书
班委竞选演讲稿
2014/04/28 职场文书
工商管理专业毕业生求职信
2014/05/26 职场文书
2014年爱国卫生工作总结
2014/11/22 职场文书
浅谈Python协程asyncio
2021/06/20 Python