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 相关文章推荐
PHP+javascript模拟Matrix画面
Oct 09 PHP
PHP完整的日历类(CLASS)
Nov 27 PHP
什么是MVC,好东西啊
May 03 PHP
php实现utf-8转unicode函数分享
Jan 06 PHP
Symfony2之session与cookie用法小结
Mar 18 PHP
ThinkPHP使用Ueditor的方法详解
May 20 PHP
php中让人头疼的浮点数运算分析
Oct 10 PHP
PHP基于Redis消息队列实现发布微博的方法
May 03 PHP
PHP实现的防止跨站和xss攻击代码【来自阿里云】
Jan 29 PHP
php面向对象程序设计中self与static的区别分析
May 21 PHP
laravel框架数据库操作、查询构建器、Eloquent ORM操作实例分析
Dec 20 PHP
php的instanceof和判断闭包Closure操作示例
Jan 26 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
有道搜索和IP138的IP的API接口(PHP应用)
2012/11/29 PHP
php生成txt文件标题及内容的方法
2014/01/16 PHP
CodeIgniter中使用cookie的三种方式详解
2014/07/18 PHP
LINUX下PHP程序实现WORD文件转化为PDF文件的方法
2016/05/13 PHP
PHP配合fiddler抓包抓取微信指数小程序数据的实现方法分析
2020/01/02 PHP
JQuery Tab选项卡效果代码改进版
2010/04/01 Javascript
jquery控制select的text/value值为选中状态
2014/06/03 Javascript
深入探秘jquery瀑布流的实现
2016/01/30 Javascript
BootStrap实现手机端轮播图左右滑动事件
2016/10/13 Javascript
JavaScript中数组的各种操作的总结(必看篇)
2017/02/13 Javascript
javascript 中Cookie读、写与删除操作
2017/03/29 Javascript
JavaScript通过filereader接口读取文件
2017/05/10 Javascript
jQuery.Form实现Ajax上传文件同时设置headers的方法
2017/06/26 jQuery
基于webpack.config.js 参数详解
2018/03/20 Javascript
echarts同一页面中四个图表切换的js数据交互方法示例
2018/07/03 Javascript
js动态获取时间的方法分析
2019/08/02 Javascript
用Python中的wxPython实现最基本的浏览器功能
2015/04/14 Python
python定时检查某个进程是否已经关闭的方法
2015/05/20 Python
python 读入多行数据的实例
2018/04/19 Python
python简易远程控制单线程版
2018/06/20 Python
对python指数、幂数拟合curve_fit详解
2018/12/29 Python
利用Python校准本地时间的方法教程
2019/10/31 Python
解决Pycharm 导入其他文件夹源码的2种方法
2020/02/12 Python
Tensorflow中的dropout的使用方法
2020/03/13 Python
使用keras内置的模型进行图片预测实例
2020/06/17 Python
Pycharm Available Package无法显示/安装包的问题Error Loading Package List解决
2020/09/18 Python
html5指南-6.如何创建离线web应用程序实现离线访问
2013/01/07 HTML / CSS
中学生学习生活的自我评价
2013/10/26 职场文书
中文师范生自荐信
2014/01/30 职场文书
安全负责人任命书
2014/06/06 职场文书
校本课程教学计划
2015/01/19 职场文书
圣诞节开幕词
2015/01/29 职场文书
火烧圆明园的观后感
2015/06/03 职场文书
2015小学教育教学工作总结
2015/07/21 职场文书
PHP 对接美团大众点评团购券(门票)的开发步骤
2021/04/03 PHP
pytorch 中autograd.grad()函数的用法说明
2021/05/12 Python