diarmuidie / imagerack
简单的PHP图像处理和服务器。
Requires
- php: >=5.6
- diarmuidie/imagerack-kernel: ^0.4
Suggests
- league/flysystem-aws-s3-v3: Allows using S3 as the source and cache locations
README
ImageRack PHP 库的项目仓库 ImageRack PHP lib。简单的PHP图像处理和服务器。
由 Diarmuid 开发。
特性
- 根据预定义的用户模板动态调整图像大小。
- 使用 Intervention 图像库,通过 GD 或 Imagick 实现简单的图像处理。
- 通过使用 PHP League Flysystem 文件系统抽象库,与本地或远程(Amazon S3、SFTP、Azure 等)图像存储进行集成。
- 能够缓存处理过的图像,以便后续请求快速提供服务。
- SEO 友好且易于使用的 URL,例如
example.com/<template>/path/to/source/image.png
。 - 支持 PNG/JPEG/GIF 图像。
- 在响应中添加
Cache-Control
、ETag
和Last_Modified
头部,以实现更好的浏览器和 CDN 缓存。
用法
示例 Heroku 配置指南和代码: diarmuid.ie/blog/setting-up-a-php-image-server-on-heroku。
要开始一个新项目,请使用 composer create-project
命令在命名文件夹中安装 ImageRack 包
composer create-project diarmuidie/imagerack <folder-name>
安装包后,您可以编辑
public/index.php
,这是所有请求处理的主文件。bootstrap/dependencies.local.sample.php
,在此处配置 Flysystem 和 Intervention Image 依赖项。templates/
,在此处存储您调整媒体大小的模板。
服务器配置
Apache
确保 public/
目录已设置为您的公开访问 DocumentRoot。提供的 .htaccess
文件将重定向所有请求到 ImageRack 服务器。
Nginx
Nginx 配置文件应包括以下信息(除其他任何设置外)
root /some/folder/public;
index index.php;
location ~ \.php$ {
...
try_files $uri $uri/ /index.php?$args;
...
}
单服务器配置
单服务器部署是最直接的配置。源图像必须存储在 storage/source
文件夹中。调整大小的图像将缓存在 storage/cache
中。
多服务器配置
要在多服务器环境中(即多个位于负载均衡器后面的 Web 服务器)使用 ImageRack 服务器,您必须将源图像和缓存图像存储在分布式文件系统中。
ImageRack 随附了一个用于使用 AWS S3 的示例配置。要使用此配置,您必须确保已安装 Flysystem S3 适配器
composer require league/flysystem-aws-s3-v3
然后,您可以编辑 public/index.php
以加载示例 S3 依赖项
$dependencies = require_once __DIR__.'/../bootstrap/dependencies.s3.sample.php';
您还必须更改 bootstrap/dependencies.s3.sample.php
文件,以使用您的 S3 秘密和密钥,并更改存储桶名称。
配置
ImageRack 允许您以多种方式配置服务器
HTTP 缓存最大年龄
您可以覆盖默认的 http 缓存最大年龄头值(30天)
$server->setHttpCacheMaxAge(86000); // 86000 seconds = 1 day
将缓存持续时间设置为零将通过设置 Cache-Control:no-cache
头来禁用缓存。
模板
模板是定义图像如何被处理的对象。您可以创建多个模板以不同方式处理图像。模板必须实现 Diarmuidie\ImageRack\Image\TemplateInterface 接口。ImageRack 提供了一个 示例模板,用于将图像调整大小到 320×240 像素。
创建新的模板后,必须在服务器中注册
$server->setTemplate( 'large', function () { return new Templates\Large(); } );
模板名称(本例中的 large
)必须是 URL 安全的,因为它用于通过此模板访问图像,即 example.com/large/path/to/image.jpg
。
未找到响应
您可以设置可选的“未找到”响应。默认情况下,将发送带有正文“文件未找到”的 404 头。但是,可以使用 setNotFound()
方法进行修改。
$server->setNotFound(function ($response) { // Edit the response as required. // For example here we cahnge the content $response->setContent('Image not found.'); // Return the modified response return $response; });
$response
是 Symfony\Component\HttpFoundation\Response
的一个实例。有关如何使用响应的更多信息,请参阅 Symfony HTTP-Foundation 文档。
错误响应
您可以设置可选的“错误”响应。默认情况下,将发送带有正文“处理此请求时出现问题”的 500 头。但是,可以使用 setError()
方法进行修改。
$server->setError(function ($response, $exception) { // Edit the response as required. // For example here we add the exception message to the content $response->setContent('An internal error occurred. ' . $exception->getMessage()); // Return the modified response return $response; });
$exception
是捕获到的异常的实例。
$response
是 Symfony\Component\HttpFoundation\Response
的一个实例。有关如何使用响应的更多信息,请参阅 Symfony HTTP-Foundation 文档。
变更日志
版本 0.1.0(2015年3月29日)
- 初始提交。
版本 0.1.1(2015年8月5日)
- 要求 ImageRack 0.2.0 版本。
版本 0.2.0(2015年8月9日)
- 默认为 GD 图像库。
版本 0.2.1(2015年8月11日)
- 整理文档并改进示例。
版本 0.3.0(2017年3月5日)
- 要求 ImageRack 0.4.0 版本(PHP >= 5.6)。
作者
许可证
MIT 许可证(MIT)
版权所有(c)2017 Diarmuid
在此特此授予任何人免费获得本软件及其相关文档文件(统称为“软件”)的副本的权利,以便在不作限制的情况下处理软件,包括但不限于使用、复制、修改、合并、发布、分发、再许可和/或销售软件副本,并允许获得软件的人进行此类操作,但须遵守以下条件
上述版权声明和本许可声明应包含在软件的所有副本或实质性部分中。
软件按“原样”提供,不提供任何形式的保证,无论是明示的、暗示的,还是关于适销性、特定用途适用性或非侵权的保证。在任何情况下,作者或版权所有者均不对任何索赔、损害或其他责任承担责任,无论其是基于合同、侵权或其他法律行为,以及与软件或软件的使用或其他处理相关的任何事件。