laravel 擴展包事件使用

https://github.com/codingyu/laravel-ueditor為例
--------------------------下面原文檔說明---------------------------------

上傳完成事件
Codingyu\LaravelUEditor\Events\Uploaded

它有兩個屬性:

$event->file 與 Uploading 一樣,上傳的文件

$event->result 上傳結(jié)構(gòu),數(shù)組,包含以下信息:

'state' => 'SUCCESS',
'url' => 'http://xxxxxx.qiniucdn.com/xxx/xxx.jpg',
'title' => '文件名.jpg',
'original' => '上傳時的源文件名.jpg',
'type' => 'jpg',
'size' => 17283,
你可以監(jiān)聽此事件用于一些后續(xù)處理任務(wù),比如記錄到數(shù)據(jù)庫。

--------------------------下面具體調(diào)用處理---------------------------------

打開App\Providers\EventServiceProvider.php,添加該事件處理

    protected $listen = [
        Registered::class => [
            SendEmailVerificationNotification::class,
        ],
        'Codingyu\LaravelUEditor\Events\Uploaded'=>[
            'App\Listeners\UploadedListener',
        ]
    ];

在App\Listeners\UploadedListener.php添加具體處理,下面為圖片添加水印處理

<?php

namespace App\Listeners;

use Codingyu\LaravelUEditor\Events\Uploaded;
use Log;
use Intervention\Image\ImageManagerStatic as Image;

class UploadedListener
{
    /**
     * Create the event listener.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Handle the event.
     *
     * @param  \App\Events\OrderShipped  $event
     * @return void
     */
    public function handle(Uploaded $event)
    {
        Log::info(json_encode($event));
        $result=$event->result;
        // Access the order using $event->order...
        $imgPath = storage_path() .'/app/public/'. $result['title'];
        //$spaceFilter=str_replace(" ", "\\ ",$imgPath);
        //list($imgWidth, $imgHeight, $type, $attr) = getimagesize($imgPath);
        $img = Image::make($imgPath);


        $img->insert(storage_path() . '/app/watermark.png', 'bottom-right', 0, 0);

        $img->save();
    }
}

這樣完成事件調(diào)用使用

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容