久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

php制作圓形用戶頭像的實例_自定義封裝類源代碼

下面小編就為大家帶來一篇php制作圓形用戶頭像的實例_自定義封裝類源代碼。小編覺得挺不錯的,現在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

思路

使用圖層的方法設計,共需要創建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制作圓形用戶頭像的實例_自定義封裝類源代碼就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持。

【網站聲明】本站除付費源碼經過測試外,其他素材未做測試,不保證完整性,網站上部分源碼僅限學習交流,請勿用于商業用途。如損害你的權益請聯系客服QQ:2655101040 給予處理,謝謝支持。

相關文檔推薦

主站蜘蛛池模板: 视频一区二区在线观看 | 久久在线| 亚洲激精日韩激精欧美精品 | 免费观看一级视频 | 欧美a区 | 91久久久久久久久 | 中文字幕国产精品 | 黄色片免费在线观看 | 免费观看成人鲁鲁鲁鲁鲁视频 | 精品国产精品三级精品av网址 | 99精品欧美一区二区蜜桃免费 | 免费视频二区 | 国产一区免费 | 午夜视频在线视频 | 性做久久久久久免费观看欧美 | 有码在线| 日韩精品一区二区三区中文在线 | 天天干天天想 | 亚洲精品免费视频 | 亚洲精品视频免费观看 | 国产特一级黄色片 | 91精品国产色综合久久 | 国产精品久久久久久福利一牛影视 | 久久久新视频 | 日韩综合在线 | 日韩成人在线视频 | 好好的日在线视频 | 亚洲三区在线 | 成人av电影免费在线观看 | 91在线电影 | 亚洲午夜精品 | 美女在线国产 | 精品二区视频 | 激情网站在线观看 | 在线中文字幕第一页 | 99久久精品免费看国产免费软件 | 91精品国产综合久久久久久丝袜 | 亚洲天堂影院 | 日韩激情在线 | 黄色在线观看网址 | 成人深夜小视频 |