darkling / nette-webimages
为您的 Nette 应用程序动态生成网络图像
2.2
2020-10-01 15:30 UTC
Requires
- php: >=7.2
- latte/latte: ^2.4
- nette/application: ^2.4
- nette/di: ^2.4
- nette/http: ^2.4
- nette/utils: ^2.4
README
要求
- PHP 5.4+
- nette/application >= 2.2
- nette/di >= 2.2
- nette/http >= 2.2
- latte/latte >= 2.2
- nette/utils >= 2.2
安装
- 从 Github 复制源代码或使用 Composer
$ composer require dotblue/nette-webimages
- 注册为配置器的扩展
extensions:
webimages: DotBlue\WebImages\Extension
概念
此插件为您提供了在应用程序中自动生成不同尺寸图像版本的能力。当浏览器请求新的图像版本时,应用程序将生成它并将其保存到请求的目的地,这样在下一个 HTTP 请求中,您的服务器只需提供现有的文件即可。
要启用此功能,请修改您的 .htaccess
# front controller
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule !\.(pdf|js|ico|gif|css|rar|zip|tar\.gz)$ index.php [L]
用法
首先,您必须定义您的 DotBlue\WebImages\IProvider
实现。其职责是使用 Nette\Image
生成图像的新版本。查看 示例 以获得灵感 - 唯一必需的方法 getImage
应返回查询图像的 Nette\Image
实例。
当您拥有它时,在配置中注册它
webimages:
providers:
- <name of your class>
其次,您必须指定图像可用的路由。路由的中央点是 id
参数,它应唯一标识您的图像。让我们设置一个简单的路由
webimages:
routes:
- images/<id>-<width>x<height>.jpg
默认情况下,所有这些路由都将添加到您的其他路由之前 - 假设您使用
Nette\Application\Routers\RouteList
作为您的根路由器。您可以通过设置prependRoutesToRouter: false
来禁用此功能。然后,您负责将 webimages 路由器(服务webimages.router
)连接到您的路由实现。
插件为您提供新的宏 n:src
。现在您可以开始使用了。
<img n:src="foo, 200, 150">
这将生成以下 HTML
<img src="/images/foo-200x150.jpg">
此文件的创建将处理您的 DotBlue\WebImages\IProvider
实现。
HTTPS
要切换安全路由标志,请在您的配置中使用以下语法
webimages:
routes:
- mask: images/<id>-<width>x<height>.jpg
secured: true