Php creates a circular user avatar-custom encapsulation class source code and a circular user avatar

Source: Internet
Author: User

Php creates a circular user avatar-custom encapsulation class source code and a circular user avatar

Ideas

Three image layers must be created for the layer design.

1. Bottom Layer: The final generated image

2. Real user profile pictures: real profile pictures uploaded by users as intermediate layers

3. Circular mask: as the top layer, draw a circular area in the mask and set it to transparent.

The Code is as follows:

Main function class avatar. class. php

 

<? Phpclass avatar {private $ fileName; // absolute path of the file (or relative path of the final called file) private $ rgb; // color index (array (255,255, 0) or hexadecimal value ffff00/# ffff00/ff0/# ff0) private $ size; // image size private $ imgInfo; // image information/*** initialize * Enter description here... * @ param string $ absolute path of the fileName file (or relative path of the final called file) * @ param mixed $ rgb color index (array (255,255, 0) or the hexadecimal value ffff00/# ffff00/ff0/# ff0) * @ param int $ size image size */public function _ constr Uct ($ fileName, $ rgb, $ size) {$ this-> fileName = $ fileName; if (is_array ($ rgb) {$ this-> rgb = $ rgb; // rgb color array (255,255, 0)} else {// some people like to carry # $ rgb = trim ($ rgb ,'#'); // processing abbreviation form if (strlen ($ rgb) = 3) {$ _ tmp = $ rgb [0]. $ rgb [0]. $ rgb [1]. $ rgb [1]. $ rgb [2]. $ rgb [2]; $ rgb =$ _ tmp;} $ this-> rgb = $ this-> createRGB ($ rgb ); // hexadecimal value ffff00} $ this-> size = $ size; $ this-> imgInfo = getimagesize ($ this-> fileName); if (! $ This-> imgInfo) {throw Exception ("unable to read image files");} if (! In_array ($ this-> imgInfo [2], array (2, 3) {// only jpg and png throw Exception ("image format not supported ");}} /*** display image ** Enter description here... */public function show () {header ("content-type: image/png"); $ shadow = $ this-> createshadow (); // mask image // create a square image $ imgbk = imagecreatetruecolor ($ this-> size, $ this-> size ); // switch the target image ($ this-> imgInfo [2]) {case 2: $ imgfk = imagecreatefromjpeg ($ this-> fileName); // the original image 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); // create an image imagepng ($ imgbk); // destroy the resource imagedestroy ($ imgbk); imagedestroy ($ imgfk ); imagedestroy ($ shadow);}/*** create a circular mask * Enter description here... * @ param array 10 hexadecimal color array */private function cr Eateshadow () {$ img = imagecreatetruecolor ($ this-> size, $ this-> size); imageantialias ($ img, true ); // enable anti-aliasing $ color_bg = imagecolorallocate ($ img, $ this-> rgb [0], $ this-> rgb [1], $ this-> rgb [2]); // background color $ color_fg = imagecolorallocate ($ img, 0, 0); // foreground color, mainly used to create a circular imagefilledrectangle ($ img, 0, 0,200,200, $ color_bg ); imagefilledarc ($ img, 100,100,200,200, 0, 0, $ color_fg, IMG_ARC_PIE); imagecolortransp Arent ($ img, $ color_fg); // convert the foreground color to transparent return $ img ;} /*** convert the character form into a hexadecimal string * 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 is calculated from 0 $ sum + = $ index * pow (16, $ j);} return $ sum ;} /*** convert the hexadecimal color into a 10-hexadecimal color value array (RGB) * Enter description h Ere... * @ param $ str hexadecimal string (for example, ff9900) */private function createRGB ($ str) {$ rgb = array (); if (strlen ($ str )! = 6) {$ rgb [] = 0xff; $ rgb [] = 0xff; $ rgb [] = 0xff; return $ rgb; // default white} $ rgb [] = $ this-> getIntFromHexStr (substr ($ str, 0, 2 )); $ rgb [] = $ this-> getIntFromHexStr (substr ($ str, 2, 2); $ rgb [] = $ this-> getIntFromHexStr (substr ($ str, 4, 2); return $ rgb ;}}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.