├── .DS_Store ├── README.md ├── composer.json └── src ├── .DS_Store ├── Avatar.php ├── AvatarProvider.php ├── Facades └── Avatar.php └── config ├── .DS_Store └── avatar.php /.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cxp1539/laravel-avatar/dfd03213c59942a88e1a847f4a4b7df49d20093d/.DS_Store -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # laravel-avatar 2 | 根据第一个字符或者汉字生成头像的laravel扩展包 3 | 4 | ## 基本使用 5 | 1. 先发布配置文件在config目录下面 6 | ```bash 7 | php artisan vendor:publish 8 | ``` 9 | 2. app.php 添加 providers 10 | ```php 11 | Cxp\Avatar\AvatarProvider::class, 12 | ``` 13 | 3. app.php 添加 aliases 14 | ```php 15 | 'Avatar' => Cxp\Avatar\Facades\Avatar::class 16 | ``` 17 | 4 开始使用 18 | ```php 19 | // 第一个参数姓名,第二个参数图片生成位置 20 | Avatar::output('赵','zhao.png') 21 | ``` 22 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "cxp/laravel-avatar", 3 | "description": "laravel avatar", 4 | "license": "MIT", 5 | "authors": [ 6 | { 7 | "name": "cxp1539", 8 | "email": "457714145@qq.com" 9 | } 10 | ], 11 | "autoload": { 12 | "psr-4": { 13 | "Cxp\\Avatar\\": "src" 14 | } 15 | }, 16 | "require": {} 17 | } 18 | -------------------------------------------------------------------------------- /src/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cxp1539/laravel-avatar/dfd03213c59942a88e1a847f4a4b7df49d20093d/src/.DS_Store -------------------------------------------------------------------------------- /src/Avatar.php: -------------------------------------------------------------------------------- 1 | config = $config->get('avatar'); 22 | } 23 | 24 | /** 25 | * 生成图像 26 | * @return resource 图片资源 27 | */ 28 | private function generate($name) 29 | { 30 | // 创建图片资源 31 | $img_res = imagecreate($this->config['width'], $this->config['height']); 32 | 33 | // 背景颜色 34 | $bg_color = imagecolorallocate($img_res, mt_rand(120, 190), mt_rand(120, 190), mt_rand(120, 190)); 35 | // 文字颜色 36 | $font_color = imagecolorallocate($img_res, mt_rand(190, 255), mt_rand(190, 255), mt_rand(190, 255)); 37 | // 填充背景色 38 | imagefill($img_res, 1, 1, $bg_color); 39 | // 计算文字的宽高 40 | $pos = imagettfbbox($this->config['size'], 0, $this->config['font_file'], mb_substr($name, 0, 1)); 41 | $font_width = $pos[2] - $pos[0] + 0.32 * $this->config['size']; 42 | $font_height = $pos[1] - $pos[5] + -0.16 * $this->config['size']; 43 | 44 | // 写入文字 45 | imagettftext($img_res, $this->config['size'], 0, ($this->config['width'] - $font_width) / 2, ($this->config['height'] - $font_height) / 2 + $font_height, $font_color, $this->config['font_file'], mb_substr($name, 0, 1)); 46 | 47 | return $img_res; 48 | } 49 | 50 | /** 51 | * 输出图片(默认输出到浏览器,给定输出文件位置则输出到文件) 52 | * @param string|false $path 保存路径 53 | */ 54 | public function output($name, $path = false) 55 | { 56 | $img_res = $this->generate($name); 57 | // 确定输出类型和生成用的方法名 58 | $content_type = 'image/' . $this->config['type']; 59 | $generateMethodName = 'image' . $this->config['type']; 60 | // 确定是否输出到浏览器 61 | if (!$path) { 62 | header("Content-type: " . $content_type); 63 | $generateMethodName($img_res); 64 | } else { 65 | $generateMethodName($img_res, $path); 66 | } 67 | // 释放图片内存 68 | imagedestroy($img_res); 69 | } 70 | 71 | } -------------------------------------------------------------------------------- /src/AvatarProvider.php: -------------------------------------------------------------------------------- 1 | publishes([ 17 | __DIR__.'/config/avatar.php' => config_path('avatar.php'), 18 | ]); 19 | } 20 | 21 | /** 22 | * Register the application services. 23 | * 24 | * @return void 25 | */ 26 | public function register() 27 | { 28 | $this->app->singleton('avatar', function ($app) { 29 | return new Avatar($app['config']); 30 | }); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/Facades/Avatar.php: -------------------------------------------------------------------------------- 1 | 'png', // jpeg|png|gif|bmp 11 | 'width' => '100', 12 | 'height' => '100', 13 | 'size' => '26', 14 | 'font_file' => public_path() . '/fonts/WawaSC-Regular.otf', 15 | ]; --------------------------------------------------------------------------------