PHP群:95885625 Hbuilder+MUI群:81989597 站长QQ:634381967
    您现在的位置: 首页 > 开发编程 > PHP教程 > 正文

    php自定义封装类--制作圆形用户头像

    作者:admin来源:网络浏览:时间:2020-09-30 00:07:50我要评论
    导读:使用图层的方法设计,共需要创建3个图像层1.底层:最后生成的图像2.真实用户头像:作为中间层,用户上传的真实头像图片3.圆形蒙版:作为最...
    使用图层的方法设计,共需要创建3个图像层

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

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

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

    代码如下:

    主功能类 avatar.class.php

    1. <?php 
    2. class avatar 
    3.     private $fileName//文件的绝对路径(或基于最终调用文件的相对路径) 
    4.     private $rgb//颜色索引(数组 array(255,255,0) 或 16进制值 ffff00/#ffff00/ff0/#ff0) 
    5.     private $size//图像大小 
    6.     private $imgInfo//图像信息 
    7.      
    8.     /** 
    9.      * 初始化 
    10.      * Enter description here ... 
    11.      * @param string $fileName 文件的绝对路径(或基于最终调用文件的相对路径) 
    12.      * @param mixed $rgb 颜色索引(数组 array(255,255,0) 或 16进制值 ffff00/#ffff00/ff0/#ff0) 
    13.      * @param int $size 图像大小 
    14.      */ 
    15.     public function __construct($fileName$rgb$size
    16.     { 
    17.         $this->fileName = $fileName
    18.          
    19.         if(is_array($rgb)){ 
    20.             $this->rgb = $rgb//rgb颜色数组 array(255,255,0) 
    21.         }else
    22.             //有的人喜欢带#号 
    23.             $rgb = trim($rgb'#'); 
    24.             //处理缩写形式 
    25.             if (strlen($rgb)==3){ 
    26.                 $_tmp = $rgb[0].$rgb[0].$rgb[1].$rgb[1].$rgb[2].$rgb[2]; 
    27.                 $rgb = $_tmp
    28.             } 
    29.             $this->rgb = $this->createRGB($rgb); //16进制值 ffff00 
    30.         } 
    31.          
    32.         $this->size = $size
    33.          
    34.         $this->imgInfo = getimagesize($this->fileName); 
    35.          
    36.         if(!$this->imgInfo){ 
    37.             throw Exception("无法读取图像文件"); 
    38.         } 
    39.         if(!in_array($this->imgInfo[2], array(2,3))){ 
    40.             //仅允许jpg和png 
    41.             throw Exception("图像格式不支持"); 
    42.         } 
    43.     } 
    44.      
    45.     /** 
    46.      * 显示图像 
    47.      * Enter description here ... 
    48.      */ 
    49.     public function show() 
    50.     { 
    51.         header("content-type:image/png"); 
    52.          
    53.         $shadow = $this->createshadow(); //遮罩图片 
    54.          
    55.         //创建一个方形图片 
    56.         $imgbk = imagecreatetruecolor($this->size, $this->size); //目标图片 
    57.          
    58.         switch ($this->imgInfo[2]){ 
    59.             case 2: 
    60.                 $imgfk = imagecreatefromjpeg($this->fileName); //原素材图片 
    61.                 break
    62.             case 3: 
    63.                 $imgfk = imagecreatefrompng($this->fileName); //原素材图片 
    64.             default
    65.                 return ; 
    66.                 break
    67.         } 
    68.          
    69.          
    70.         $realSize = $this->imgInfo[0]<$this->imgInfo[1]? $this->imgInfo[0] : $this->imgInfo[1]; 
    71.          
    72.         imagecopyresized($imgbk$imgfk, 0, 0, 0, 0, $this->size, $this->size, $realSize$realSize); 
    73.         imagecopymerge($imgbk$shadow, 0, 0, 0, 0, $this->size, $this->size, 100); 
    74.          
    75.         //创建图像 
    76.         imagepng($imgbk); 
    77.          
    78.         //销毁资源 
    79.         imagedestroy($imgbk); 
    80.         imagedestroy($imgfk); 
    81.         imagedestroy($shadow); 
    82.     } 
    83.      
    84.     /** 
    85.      * 创建一个圆形遮罩 
    86.      * Enter description here ... 
    87.      * @param array 10进制颜色数组 
    88.      */ 
    89.     private function createshadow() 
    90.     { 
    91.          
    92.         $img = imagecreatetruecolor($this->size, $this->size); 
    93.          
    94.         imageantialias($img, true); //开启抗锯齿 
    95.          
    96.         $color_bg = imagecolorallocate($img$this->rgb[0], $this->rgb[1], $this->rgb[2]); //背景色 
    97.         $color_fg = imagecolorallocate($img, 0, 0, 0); //前景色,主要用来创建圆形 
    98.          
    99.         imagefilledrectangle($img, 0, 0, 200, 200, $color_bg); 
    100.         imagefilledarc($img, 100, 100, 200, 200, 0, 0, $color_fg, IMG_ARC_PIE); 
    101.          
    102.         imagecolortransparent($img$color_fg); //将前景色转换为透明 
    103.          
    104.          
    105.         return $img
    106.     } 
    107.      
    108.     /** 
    109.      * 将字符形式16进制串转为10进制 
    110.      * Enter description here ... 
    111.      * @param $str 
    112.      */ 
    113.     private function getIntFromHexStr($str
    114.     { 
    115.         $format = '0123456789abcdef'
    116.          
    117.         $sum = 0; 
    118.          
    119.         for($i=strlen($str)-1, $c=0, $j=0; $i>=$c$i--,$j++){ 
    120.             $index = strpos($format$str[$i]);//strpos从0计算 
    121.             $sum+=$index * pow(16,$j); 
    122.         } 
    123.          
    124.         return $sum
    125.     } 
    126.      
    127.     /** 
    128.      * 将16进制颜色转为10进制颜色值数组(RGB) 
    129.      * Enter description here ... 
    130.      * @param $str 16进制串(如:ff9900) 
    131.      */ 
    132.     private function createRGB($str
    133.     { 
    134.         $rgb = array(); 
    135.         if(strlen($str) != 6){ 
    136.             $rgb[] = 0xff; 
    137.             $rgb[] = 0xff; 
    138.             $rgb[] = 0xff; 
    139.             return $rgb//默认白色 
    140.         } 
    141.      
    142.         $rgb[] = $this->getIntFromHexStr(substr($str, 0, 2)); 
    143.         $rgb[] = $this->getIntFromHexStr(substr($str, 2, 2)); 
    144.         $rgb[] = $this->getIntFromHexStr(substr($str, 4, 2)); 
    145.          
    146.         return $rgb
    147.          
    148.     } 

     

    转载请注明(B5教程网)原文链接:https://b5.mxunkeji.com/content-10-5975-1.html
    相关热词搜索: