dotblue/nette-webimages

为您的 Nette 应用程序动态生成网页图像

2.0.0 2015-02-25 23:13 UTC

This package is not auto-updated.

Last update: 2024-09-24 01:36:01 UTC


README

要求

安装

  1. 从 Github 拷贝源代码或使用 Composer
$ composer require dotblue/nette-webimages
  1. 注册为 Configurator 的扩展
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