Glide is an image manipulation library that can help you dynamically generate image content for the browser based on specified parameters, thereby achieving
For dynamic image cropping, watermarking, etc., this library provides some friendly packaging and extensions to Glide, shielding some of the underlying abstractions of the native library so that ThinkPHP users can better add dynamic cropping functions for images in ThinkPHP projects. . Installation
Execute the following command to install:
$ composer require slince/think-glide
Copy after login
- application/middleware.php
- file (if Just create it if it does not exist), register middleware:
This method is relatively simple and recommended;
return [ //... \Slince\Glide\GlideMiddleware::factory([ 'source' => __DIR__ . '/../img', ]) ];
Copy after login
ThinkPHP 5.1.0 or above 5.1.6 or below : does not support middleware, so the activation process is a bit more complicated. We compromise in the following way:
// 在 /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'); }); }
Copy after login
source
is your local image file folder location, assuming there is a picturehttp://youdomain.com/images/user.jpg?w=100&h=100
Copy after login
to get the reduced picture.
Parameter description
Parameter name
Is it required? | source | ||
---|---|---|---|
is | cache | string | |
runtime/glide | No |
| cacheTimestring |
2 days | , multiple requests during the cache period will automatically respond with 304No |
| signKeystring |
No | onException | callable | |
No | baseUrl | string | |
/images | No |
| ##Security signatureDoes not enable security signature In this case, the user can adjust the parameters in the query to crop the image by himself. If you do not intend to do this, you can verify it through |
\Slince\Glide\GlideMiddleware::factory([
'source' => __DIR__ . '/../img',
'signKey' => 'v-LK4WCdhcfcc%jt*VC2cj%nVpu+xQKvLUA%H86kRVk_4bgG8&CWM#k*'
])
Copy after login
in this case It will be invalid if the user adjusts the parameters by himself; generate a safe URL:
\Slince\Glide\GlideMiddleware::factory([ 'source' => __DIR__ . '/../img', 'signKey' => 'v-LK4WCdhcfcc%jt*VC2cj%nVpu+xQKvLUA%H86kRVk_4bgG8&CWM#k*' ])
Copy after login
echo app('glide.url_builder')->getUrl('user.jpg', ['w' => 100, 'h' => 100]); //你会得到如下链接:/images/user.jpg?w=100&h=100&s=af3dc18fc6bfb2afb521e587c348b904
Copy after login
Exception handling
If the user accesses a non-existent picture or does not perform security verification, the system will throw Exception, you can replace the default behavior by onException:
\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; } ])
Copy after login
Note that the closure must return a
think\Response instance; Quick reference
Not only supports cropping, glide also supports other operations. Just pass the corresponding parameters. Refer here to view the supported parameters: http://glide.thephpleague.com/1.0/api/ quick-reference/
The latest 10 thinkphp video tutorials