PHP code example of slince / think-glide

1. Go to this page and download the library: Download slince/think-glide library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

slince / think-glide example snippets


    return [
        //...
    
        \Slince\Glide\GlideMiddleware::factory([
            'source' => __DIR__ . '/../img',
        ])
    ];
    

    // 在 /route/route.php 注册下面路由
    Route::get('images/:file', 'index/handleImageRequest');
    
    //在控制器 index 里创建action
    public function handleImageRequest()
    {
        $middleware = \Slince\Glide\GlideMiddleware::factory([
            'source' => App::getRootPath() . '/img',
        ]);
        
        return $middleware(app('request'), function(){
            return app('response');
        });
    }
    

\Slince\Glide\GlideMiddleware::factory([
    'source' => __DIR__ . '/../img',
    'signKey' => 'v-LK4WCdhcfcc%jt*VC2cj%nVpu+xQKvLUA%H86kRVk_4bgG8&CWM#k*'
])

echo app('glide.url_builder')->getUrl('user.jpg', ['w' => 100, 'h' => 100]);

//你会得到如下链接:/images/user.jpg?w=100&h=100&s=af3dc18fc6bfb2afb521e587c348b904

\Slince\Glide\GlideMiddleware::factory([
    'source' => __DIR__ . '/../img',
    'signKey' => 'v-LK4WCdhcfcc%jt*VC2cj%nVpu+xQKvLUA%H86kRVk_4bgG8&CWM#k*',
    'onException' => function(\Exception $exception, $request, $server){
    
        if ($exception instanceof \League\Glide\Signatures\SignatureException) {
            $response = new Response('签名错误', 403);
        } else {
            $response = new Response(sprintf('你访问的资源 "%s" 不存在', $request->path()), 404);
        }
        
        return $response;
    }
])