Skip to content

Commit

Permalink
testing
Browse files Browse the repository at this point in the history
  • Loading branch information
wuxiumu committed Apr 7, 2022
1 parent 39c3afe commit cbcce59
Show file tree
Hide file tree
Showing 7 changed files with 152 additions and 0 deletions.
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,23 @@
# laravel-avatar
测试开发laravel-composer包
# laravel-avatar
根据第一个字符或者汉字生成头像的laravel扩展包

## 基本使用
1. 先发布配置文件在config目录下面
```bash
php artisan vendor:publish
```
2. app.php 添加 providers
```php
Wqb\Avatar\AvatarProvider::class,
```
3. app.php 添加 aliases
```php
'Avatar' => Wqb\Avatar\Facades\Avatar::class
```
4 开始使用
```php
// 第一个参数姓名,第二个参数图片生成位置
Avatar::output('吴','wu.png')
```
17 changes: 17 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"name": "wuqb/laravel-avatar",
"description": "laravel avatar",
"license": "MIT",
"authors": [
{
"name": "wuqb",
"email": "824543976@qq.com"
}
],
"autoload": {
"psr-4": {
"Wqb\\Avatar\\": "src"
}
},
"require": {}
}
65 changes: 65 additions & 0 deletions src/Avatar.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

namespace Wqb\Avatar;
use Illuminate\Config\Repository;

class Avatar {

protected $config;

/**
* 构造方法
*/
public function __construct(Repository $config)
{
$this->config = $config->get('avatar');
}

/**
* 生成图像
* @return resource 图片资源
*/
private function generate($name)
{
// 创建图片资源
$img_res = imagecreate($this->config['width'], $this->config['height']);

// 背景颜色
$bg_color = imagecolorallocate($img_res, mt_rand(120, 190), mt_rand(120, 190), mt_rand(120, 190));
// 文字颜色
$font_color = imagecolorallocate($img_res, mt_rand(190, 255), mt_rand(190, 255), mt_rand(190, 255));
// 填充背景色
imagefill($img_res, 1, 1, $bg_color);
// 计算文字的宽高
$pos = imagettfbbox($this->config['size'], 0, $this->config['font_file'], mb_substr($name, 0, 1));
$font_width = $pos[2] - $pos[0] + 0.32 * $this->config['size'];
$font_height = $pos[1] - $pos[5] + -0.16 * $this->config['size'];

// 写入文字
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));

return $img_res;
}

/**
* 输出图片(默认输出到浏览器,给定输出文件位置则输出到文件)
* @param string|false $path 保存路径
*/
public function output($name, $path = false)
{
$img_res = $this->generate($name);
// 确定输出类型和生成用的方法名
$content_type = 'image/' . $this->config['type'];
$generateMethodName = 'image' . $this->config['type'];
// 确定是否输出到浏览器
if (!$path) {
header("Content-type: " . $content_type);
$generateMethodName($img_res);
} else {
$generateMethodName($img_res, $path);
}
// 释放图片内存
imagedestroy($img_res);
}

}
30 changes: 30 additions & 0 deletions src/AvatarProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php
namespace Wqb\Avatar;
use Illuminate\Support\ServiceProvider;
class AvatarProvider extends ServiceProvider
{
/**
* Bootstrap the application services.
*
* @return void
*/
public function boot()
{
// 发布配置文件
$this->publishes([
__DIR__.'/config/avatar.php' => config_path('avatar.php'),
__DIR__.'/config/WawaSC-Regular.otf' => public_path('/fonts/WawaSC-Regular.otf'),
]);
}
/**
* Register the application services.
*
* @return void
*/
public function register()
{
$this->app->singleton('avatar', function ($app) {
return new Avatar($app['config']);
});
}
}
10 changes: 10 additions & 0 deletions src/Facades/Avatar.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php
namespace Wqb\Avatar\Facades;
use Illuminate\Support\Facades\Facade;
class Avatar extends Facade
{
protected static function getFacadeAccessor()
{
return 'avatar';
}
}
Binary file added src/config/WawaSC-Regular.otf
Binary file not shown.
9 changes: 9 additions & 0 deletions src/config/avatar.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

return [
'type' => 'png', // jpeg|png|gif|bmp
'width' => '100',
'height' => '100',
'size' => '26',
'font_file' => public_path() . '/fonts/WawaSC-Regular.otf',
];

0 comments on commit cbcce59

Please sign in to comment.