如何用php製作圓形帳戶圖片

來源:互聯網
上載者:User
思路

使用圖層的方法設計,共需要建立3個映像層

1.底層:最後產生的映像

2.真實帳戶圖片:作為中介層,使用者上傳的真實頭像圖片

3.圓形蒙版:作為最上層,在蒙版中繪製圓形,並設定為透明

代碼如下:

主功能類 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;}}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.