{"id":28429433,"url":"https://github.com/top-think/think-glide","last_synced_at":"2025-07-04T18:31:12.811Z","repository":{"id":57053339,"uuid":"127733149","full_name":"top-think/think-glide","owner":"top-think","description":"ThinkPHP adapter for using Glide image manipulation library","archived":false,"fork":false,"pushed_at":"2019-04-16T10:52:55.000Z","size":28,"stargazers_count":16,"open_issues_count":3,"forks_count":5,"subscribers_count":5,"default_branch":"master","last_synced_at":"2025-06-13T17:32:06.849Z","etag":null,"topics":["glide","image-manipulation","think-glide","thinkphp"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/top-think.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-04-02T09:14:02.000Z","updated_at":"2025-05-17T11:11:02.000Z","dependencies_parsed_at":"2022-08-24T04:11:42.655Z","dependency_job_id":null,"html_url":"https://github.com/top-think/think-glide","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"purl":"pkg:github/top-think/think-glide","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/top-think%2Fthink-glide","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/top-think%2Fthink-glide/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/top-think%2Fthink-glide/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/top-think%2Fthink-glide/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/top-think","download_url":"https://codeload.github.com/top-think/think-glide/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/top-think%2Fthink-glide/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":261047284,"owners_count":23102411,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["glide","image-manipulation","think-glide","thinkphp"],"created_at":"2025-06-05T13:38:03.034Z","updated_at":"2025-07-04T18:31:12.795Z","avatar_url":"https://github.com/top-think.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# ThinkPHP 图片动态裁剪缩放库\n\n[![Build Status](https://img.shields.io/travis/top-think/think-glide/master.svg?style=flat-square)](https://travis-ci.org/top-think/think-glide)\n[![Coverage Status](https://img.shields.io/codecov/c/github/top-think/think-glide.svg?style=flat-square)](https://codecov.io/github/top-think/think-glide)\n[![Latest Stable Version](https://img.shields.io/packagist/v/slince/think-glide.svg?style=flat-square\u0026label=stable)](https://packagist.org/packages/slince/think-glide)\n[![Scrutinizer](https://img.shields.io/scrutinizer/g/top-think/think-glide.svg?style=flat-square)](https://scrutinizer-ci.com/g/top-think/think-glide/?branch=master)\n\n[Glide](https://github.com/thephpleague/glide) 是一个可以帮助你根据指定参数动态的生成图片内容给浏览器的图片操作库，从而实现\n图片动态裁剪，打水印等，本库对 Glide 进行了一些友好的包装与扩展，屏蔽了原生库的一些底层抽象从而使得 ThinkPHP 用户可以在 ThinkPHP 项目中\n更好的添加图片的动态裁剪功能。\n\n## Installation\n\n执行下面命令安装:\n\n```bash\n$ composer require slince/think-glide\n```\n\n## Usage\n\n### Quick start\n\n由于从 ThinkPHP 5.1.6 开始添加了中间件的功能，所以在\n \n- ThinkPHP 5.1.6 及以上版本使用 middleware 注册：\n\n    打开 `application/middleware.php` 文件（如果不存在创建即可），注册 middleware：\n    \n    ```php\n    return [\n        //...\n    \n        \\Slince\\Glide\\GlideMiddleware::factory([\n            'source' =\u003e __DIR__ . '/../img',\n        ])\n    ];\n    ```\n    \n    这种方式比较简单，也是推荐的方式；\n\n- ThinkPHP 5.1.0 以上 5.1.6 以下版本：\n\n    不支持middleware，所以启用过程要复杂一点，我们用下面方式来妥协：\n    \n    ```php\n    // 在 /route/route.php 注册下面路由\n    Route::get('images/:file', 'index/handleImageRequest');\n    \n    //在控制器 index 里创建action\n    public function handleImageRequest()\n    {\n        $middleware = \\Slince\\Glide\\GlideMiddleware::factory([\n            'source' =\u003e App::getRootPath() . '/img',\n        ]);\n        \n        return $middleware(app('request'), function(){\n            return app('response');\n        });\n    }\n    ```\n\n`source` 是你本地图片文件夹的位置，假设该目录下有图片 `user.jpg`, 打开浏览器访问下面链接：\n \n```\nhttp://youdomain.com/images/user.jpg?w=100\u0026h=100\n```\n即可得到缩小后的图片。\n\n### 参数说明\n\n| 参数名 | 类型 | 说明 | 是否必选 |\n| --- | --- | --- | --- |\n| source | string | 本地文件夹位置 | 是 |\n| cache| string | 缓存文件位置，默认在 `runtime/glide` 下面| 否 |\n| cacheTime| string | 缓存时间，示例 `+2 days`, 缓存期间多次请求会自动响应 304| 否 |\n| signKey | string | 安全签名 | 否 | \n| onException | callable | 异常处理handler | 否 | \n| baseUrl | string | 路由前缀，匹配到该前缀时中间件开始执行，默认是 `/images` | 否 | \n\n### 安全签名\n\n不开启安全签名的情况下用户可以调整query里面的参数自行对图片进行裁剪，如果你不打算这么做的话，你可以通过\n`signKey` 进行校验，\n\n```php\n\\Slince\\Glide\\GlideMiddleware::factory([\n    'source' =\u003e __DIR__ . '/../img',\n    'signKey' =\u003e 'v-LK4WCdhcfcc%jt*VC2cj%nVpu+xQKvLUA%H86kRVk_4bgG8\u0026CWM#k*'\n])\n```\n\n这种情况下用户自行调整参数将会无效；生成安全的URL:\n\n```php\necho app('glide.url_builder')-\u003egetUrl('user.jpg', ['w' =\u003e 100, 'h' =\u003e 100]);\n\n//你会得到如下链接：/images/user.jpg?w=100\u0026h=100\u0026s=af3dc18fc6bfb2afb521e587c348b904\n```\n\n### 异常处理\n\n如果用户访问了一张不存在的图片或者没有进行安全校验，系统会抛出异常，你可以通过 `onException` 进行替换默认行为：\n\n```php\n\\Slince\\Glide\\GlideMiddleware::factory([\n    'source' =\u003e __DIR__ . '/../img',\n    'signKey' =\u003e 'v-LK4WCdhcfcc%jt*VC2cj%nVpu+xQKvLUA%H86kRVk_4bgG8\u0026CWM#k*'，\n    'onException' =\u003e function(\\Exception $exception, $request, $server){\n    \n        if ($exception instanceof \\League\\Glide\\Signatures\\SignatureException) {\n            $response = new Response('签名错误', 403);\n        } else {\n            $response = new Response(sprintf('你访问的资源 \"%s\" 不存在', $request-\u003epath()), 404);\n        }\n        \n        return $response;\n    }\n])\n```\n\n注意该闭包必须返回一个 `think\\Response` 实例；\n\n### Quick reference\n\n不止支持裁剪，glide还支持其它操作，只要传递对应参数即可，参考这里查看支持的参数：\n\n[http://glide.thephpleague.com/1.0/api/quick-reference/](http://glide.thephpleague.com/1.0/api/quick-reference/)  \n\n## Changelog\n\nSee [CHANGELOG.md](./CHANGELOG.md)\n\n## License\n\nSee [MIT](https://opensource.org/licenses/MIT).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftop-think%2Fthink-glide","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftop-think%2Fthink-glide","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftop-think%2Fthink-glide/lists"}