如何開發 Laravel 擴展包併發布到 Composer

如何開發 Laravel 擴展包併發布到 Composer

咱們來作一個根據第一個字符或者漢字生成頭像的laravel擴展包。其實原理就是咱們本身去寫一個服務提供者,把服務提供者配置到app/providers數組中。php

1.第一步如今本身項目中跟目錄建立packages/cxp/avatar/srchtml

2.修改 composer.jsonlaravel

"psr-4": {
    "App\\": "app/",
    "Cxp\\Avatar\\": "packages/cxp/avatar/src/"
}
  1. 執行composer dumpautoloadgit

  2. src 目錄建立 Avatar.php 具體代碼github

/**
 * 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);
    }
}
  1. 再src下建立config 目錄來存取咱們的配置參數文件如config/avatar.php
/**
 * 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',
];
  1. 在src建立AvatarProvider.php即服務提供者。供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']);
        });
    }
}
  1. 若是想使用門臉,能夠在src目錄下建立Facades目錄,提供門臉
namespace Cxp\Avatar\Facades;
use Illuminate\Support\Facades\Facade;
class Avatar extends Facade
{
    protected static function getFacadeAccessor()
    {
        return 'avatar';
    }
}

使用擴展包

到此咱們開發就完成了,那改怎麼使用了。json

  1. 先發布配置文件在config目錄下面
php artisan vendor:publish
  1. 在app/config目錄註冊咱們的服務提供者和門臉類
'providers' => [
    Cxp\Avatar\AvatarProvider::class,
]

'aliases' => [
    'Avatar' => Cxp\Avatar\Facades\Avatar::class,
]
  1. 程序中使用
Avatar::output('趙','zhao.png');

發佈擴展包

1.在avatar目錄執行composer init,生成composer.json數組

{
    "name": "cxp/laravel-avatar",
    "description": "laravel avatar",
    "license": "MIT",
    "authors": [
        {
            "name": "cxp1539",
            "email": "457714145@qq.com"
        }
    ],
    "autoload": {
      "psr-4": {
        "Cxp\\Avatar\\": "src"
      }
    },
    "require": {}
}

2.在github建立個項目,將avatar目錄的代碼推送到github上。瀏覽器

3.打開https://packagist.org/ 註冊個帳號,提交git的地址就能夠了。bash

示例代碼下載連接併發

相關文章
相關標籤/搜索