作曲にLaravelの拡張パックを開発し、公開する方法
私たちは、アバターlaravel生成された最初の文字または文字に応じて拡張パックを行います。実際には、原則としては、自分自身のアプリ/プロバイダのアレイを設定するには、サービスプロバイダがサービスプロバイダを書くことです。
1.最初のステップは、今でパッケージ/ CXP /アバター/ srcディレクトリを作成するために、彼自身のプロジェクトを持っています
2. [変更composer.json
"psr-4": {
"App\\": "app/",
"Cxp\\Avatar\\": "packages/cxp/avatar/src/"
}
-
実行作曲dumpautoload
-
srcディレクトリはAvatar.php固有のコードを作成します
/**
* Created by PhpStorm.
* User: mac
* Date: 2019-01-10
* Time: 14:06
*/
namespace Cxp\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);
}
}
- そのような設定/ avatar.phpとして、当社のコンフィギュレーションファイルのパラメータにアクセスするには、configディレクトリsrcを再作成
/**
* Created by PhpStorm.
* User: mac
* Date: 2019-01-10
* Time: 14:28
*/
return [
'type' => 'png', // jpeg|png|gif|bmp
'width' => '100',
'height' => '100',
'size' => '26',
'font_file' => public_path() . '/fonts/WawaSC-Regular.otf',
];
- AvatarProvider.phpはSRCで、サービスプロバイダを作成します。IOCコンテナにサインアップ
namespace Cxp\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'),
]);
}
/**
* Register the application services.
*
* @return void
*/
public function register()
{
$this->app->singleton('avatar', function ($app) {
return new Avatar($app['config']);
});
}
}
- あなたがMenlianを使用する場合は、srcディレクトリ下のファサードディレクトリを作成することができMenlianを提供しています
namespace Cxp\Avatar\Facades;
use Illuminate\Support\Facades\Facade;
class Avatar extends Facade
{
protected static function getFacadeAccessor()
{
return 'avatar';
}
}
拡張パックを使用します
我々は、それが使用する方法を変更し、これを完全に開発しました。
- まず、次の構成ファイルのconfigディレクトリにリリース
php artisan vendor:publish
- アプリ/ configディレクトリに当社のサービスプロバイダとMenlianクラスにサインアップ
'providers' => [
Cxp\Avatar\AvatarProvider::class,
]
'aliases' => [
'Avatar' => Cxp\Avatar\Facades\Avatar::class,
]
- プログラムで使用
Avatar::output('赵','zhao.png');
拡張パックがリリース
アバターディレクトリ作曲の初期化を実行するには1.、composer.jsonを生成します
{
"name": "cxp/laravel-avatar",
"description": "laravel avatar",
"license": "MIT",
"authors": [
{
"name": "cxp1539",
"email": "[email protected]"
}
],
"autoload": {
"psr-4": {
"Cxp\\Avatar\\": "src"
}
},
"require": {}
}
2. githubのでプロジェクトを作成し、コードのアバターディレクトリには、githubの上にプッシュ。
3.オープンhttps://packagist.org/は、アカウントを登録し、その上にアドレスGitリポジトリ。