Coder Social home page Coder Social logo

think-glide's Introduction

ThinkPHP8+ 图片动态裁剪缩放库

Glide 是一个可以帮助你根据指定参数动态的生成图片内容给浏览器的图片操作库,从而实现 图片动态裁剪,打水印等,本库对 Glide 进行了一些友好的包装与扩展,屏蔽了原生库的一些底层抽象从而使得 ThinkPHP 用户可以在 ThinkPHP8 项目中 更好的添加图片的动态裁剪功能。

Installation

执行下面命令安装:

$ composer require love-dj/think-glide

Usage

Quick start

  • 要求php8.0/ThinkPHP8 及以上版本使用 middleware 注册:

    打开 app/middleware.php 文件(如果不存在创建即可),注册 middleware:

    return [
        //...
    
        \think\middleware\Glide::class
    ];

配置

系统安装后会自动在 config 目录中生成 glide.php 的配置文件

参数说明

参数名 类型 说明 是否必选
source string 本地文件夹位置
cache string 缓存文件位置,默认在 runtime/glide 下面
cacheTime string 缓存时间,示例 +2 days, 缓存期间多次请求会自动响应 304
signKey string 安全签名
onException callable 异常处理handler
baseUrl string 路由前缀,匹配到该前缀时中间件开始执行,默认是 /images

source 是你本地图片文件夹的位置,假设该目录下有图片 user.jpg, 打开浏览器访问下面链接:

https://youdomain.com/images/user.jpg?w=100&h=100

即可得到缩小后的图片。

安全签名

不开启安全签名的情况下用户可以调整query里面的参数自行对图片进行裁剪,如果你不打算这么做的话,你可以通过 signKey 进行校验,

这种情况下用户自行调整参数将会无效;生成安全的URL:

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

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

异常处理

如果用户访问了一张不存在的图片或者没有进行安全校验,系统会抛出异常,你可以通过修改glide.php配置文件中 onException 进行替换默认行为:

return [
    //...

    'onException' => function(\Exception $exception, $request, $server){
    
        if ($exception instanceof \League\Glide\Signatures\SignatureException) {
            $response = response('签名错误', 403);
        } else {
            $response = response(sprintf('你访问的资源 "%s" 不存在', $request->path()), 404);
        }
        
        return $response;
    }
])

注意该闭包必须返回一个 think\Response 实例;

Quick reference

不止支持裁剪,glide还支持其它操作,只要传递对应参数即可,参考这里查看支持的参数:

http://glide.thephpleague.com/2.0/api/quick-reference/

License

See MIT.

think-glide's People

Contributors

love-dj avatar

Stargazers

 avatar

Watchers

 avatar

think-glide's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.