harmim / images
用于处理图像的工具。
Requires
- php: >=8.2
- ext-fileinfo: *
- ext-gd: *
- ext-intl: *
- latte/latte: ^3.0
- nette/di: ^3.1
- nette/http: ^3.2
- nette/utils: ^4.0
Requires (Dev)
- nette/application: ^3.1
- nette/tester: ^2.5
- phpstan/phpstan-deprecation-rules: ~1.1.3
- phpstan/phpstan-nette: ~1.2.9
- phpstan/phpstan-strict-rules: ~1.5.1
Suggests
- nette/application: To use the 'Harmim\Images\TImageStorage' trait.
README
关于
用于处理图像的工具。它可以作为 Nette 框架 的扩展使用。
提供了 图像存储 用于轻松存储和/或从存储中删除图像。还有多种方法可以调整和/或处理图像。然后,您可以直接获取存储图像的路径,或者可以使用预准备的 Latte 宏来生成 HTML 标签。请参阅 用法。
需要 PHP 版本 8.2 或更高版本,以及 PHP 扩展 fileinfo、gd 和 intl。
安装
下载最新的 发布版 或使用 Composer
composer require harmim/images
用法
要处理图像,我们需要 \Harmim\Images\ImageStorage
不使用 Nette
$customConfig = [ 'wwwDir' => __DIR__ . DIRECTORY_SEPARATOR . 'www', 'compression' => 90, 'placeholder' => 'images/foo.png', 'types' => [ 'img-small' => [ 'width' => 50, 'height' => 50, 'transform' => \Harmim\Images\Resize::Exact, ... ], ... ], ... ]; $imageStorage = $customConfig + \Harmim\Images\Config\Config::Defaults;
在 $customConfig 中,您可以指定自定义配置。请参阅 配置。
使用 Nette
您可以使用您的 NEON 配置来启用和自定义扩展
extensions: images: \Harmim\Images\DI\ImagesExtension images: compression: 90 placeholder: images/foo.png types: img-small: width: 50 height: 50 transform: ::constant(\Harmim\Images\Resize::Exact) ... ... ...
在 images 部分,您可以指定自定义配置。请参阅 配置。
\Harmim\Images\ImageStorage 已在 DI 容器中注册。您可以直接从容器中获取它
/** @var \Nette\DI\Container $container */ $imageStorage = $container->getService('images.imageStorage'); // or $imageStorage = $container->getByType(\Harmim\Images\ImageStorage::class);
当然,您可以通过构造函数、注入方法、注入注解或任何其他方式注入 \Harmim\Images\ImageStorage。
如果您想在调用注入方法的演示者或控制器中使用 \Harmim\Images\ImageStorage,您可以使用 trait \Harmim\Images\TImageStorage。在您的演示者、控制器及其模板中,将存在变量 $imageStorage。
abstract class BasePresenter extends \Nette\Application\UI\Presenter { use \Harmim\Images\TImageStorage; } abstract class BaseControl extends \Nette\Application\UI\Control { use \Harmim\Images\TImageStorage; }
该扩展将图像宏安装到 Latte。请参阅 宏。
存储图像
您可以使用方法 \Harmim\Images\ImageStorage::saveImage(string $name, string $path): string 或 \Harmim\Images\ImageStorage::saveUpload(\Nette\Http\FileUpload $file): string 来存储图像。将存储原始图像;然后将其压缩。
两种方法都返回存储的图像文件名。您可以使用此文件名来删除、调整大小或检索图像。
图像以唯一的文件名和位置存储。
删除图像
使用方法 \Harmim\Images\ImageStorage::deleteImage(string $fileName, array $excludedTypes = []): void,您可以通过 $fileName 删除图像,该文件名应为 \Harmim\Images\ImageStorage::saveImage 或 \Harmim\Images\ImageStorage::saveUpload 返回的文件名。
如果您传递 $excludedTypes,则仅删除其他类型;否则,将删除所有类型、原始图像和压缩图像。
获取存储图像的路径
您可以使用方法 \Harmim\Images\ImageStorage::getImageLink(string $fileName, ?string $type = null, array $options = []): ?string 或 宏 来获取存储图像的路径。您可以传递在初始配置中定义的特定类型,或者可以传递特定选项。请参阅 配置。$fileName 应为 \Harmim\Images\ImageStorage::saveImage 或 \Harmim\Images\ImageStorage::saveUpload 返回的文件名。
如果您第一次尝试获取特定大小或类型的图像,则该图像尚未创建,因此现在将创建它。下次,您将获取调整大小的图像。
如果图像不存在,将返回占位符。
如果您需要获取原始/压缩的图像,在配置中,您可以使用分别的orig/compressed。例如,['orig' => true]。您也可以在宏中使用这些选项。
宏
img
{img [$image] [image-type] [options]}
渲染img标签
<img src="foo.jpg" width="100" height="100" alt="foo">
或带有lazy选项的懒加载标签
<img class="lazy" data-src="foo.jpg" width="100" height="100" alt="foo"> <noscript><img src="foo.jpg" width="100" height="100" alt="foo"></noscript>
示例
{img alt => 'foo'} {* returns a path to a placeholder *} {* '$image' is a file name *} {img $image alt => 'foo'} {img $image width => 200, height => 200, alt => 'foo'} {* 'img-small' is an image type defined in the configuration *} {img $image img-small alt => 'foo'} {img $image img-small compression => 90, alt => 'foo', class => 'bar'} {img $image img-small lazy => true, alt => 'foo'} {img $image img-small lazy => true, width => 500, height => 650, alt => 'foo'}
n:img
<img n:img="[$image] [image-type] [options]" alt="foo">
渲染src属性。它可以用在例如img元素中。
示例
<img n:img alt="foo"> {* renders a path to a placeholder *} {* '$image' is a file name *} <img n:img="$image" alt="foo"> <img n:img="$image width => 200, height => 200" width="200" height="200" alt="foo"> {* 'img-small' is an image type defined in the configuration *} <img n:img="$image img-small" alt="foo"> <img n:img="$image img-small compression => 90" alt="foo" class="bar">
imgLink
{imgLink [$image] [image-type] [options]}
返回给定图像的相对路径(从资源根目录)。
示例
{imgLink} {* returns a path to a placeholder *} {* '$image' is a file name *} {imgLink $image} {imgLink $image width => 200, height => 200} {* 'img-small' is an image type defined in the configuration *} {imgLink $image img-small} {imgLink $image img-small compression => 90} <div class="image-box" data-src="{imgLink $image img-small}}"></div>
配置
wwwDir:(string)资源根目录的绝对路径。- 默认:Nette中的
%wwwDir%;否则,您必须指定此参数。
- 默认:Nette中的
imagesDir:(string)存储图像的目录的相对路径(从wwwDir)。- 默认:
data/images。
- 默认:
origDir:(string)存储原始图像的目录的相对路径(从imagesDir)。- 默认:
orig。
- 默认:
compressionDir:(string)存储压缩图像的目录的相对路径(从imagesDir)。- 默认:
imgs。
- 默认:
placeholder:(string)图像占位符的相对路径(当找不到图像时)(从wwwDir)。- 默认:
img/noimg.jpg。
- 默认:
width:(int)图像宽度。- 默认:
1024。
- 默认:
height:(int)图像高度。- 默认:
1024。
- 默认:
compression:(int)压缩质量。请参阅\Nette\Utils\Image::save。- 默认:
85。
- 默认:
transform:(\Harmim\Images\Resize|list<\Harmim\Images\Resize>)请参阅变换选项。- 默认:
\Harmim\Images\Resize::OrSmaller。
- 默认:
allowedImgTagAttrs:(list<string>)在{img}Latte宏中可以使用的img属性,其他属性将被忽略。- 默认:
[alt, height, width, class, hidden, id, style, title, data]。
- 默认:
lazy:(bool)将{img}Latte宏渲染为懒加载图像(带有data-src属性、lazy类和noscript标签中的正常img标签)。- 默认:
false。
- 默认:
types:(array<string, mixed>)覆盖默认配置的图像类型配置。- 默认:
[]。 - 示例
- 默认:
types: img-small: width: 50 height: 50 img-gallery: lazy: true transform: - ::constant(\Harmim\Images\Resize::Stretch) - ::constant(\Harmim\Images\Resize::Cover)
destDir:(?string)查找图像的目录。- 默认:
null。
- 默认:
orig:(?bool)当设置为true时,将返回原始图像。- 默认:
null。
- 默认:
compressed:(?bool)当设置为true时,将返回原始压缩图像。- 默认:
null。
- 默认:
变换选项
许可证
此工具受MIT许可证许可。
作者:Dominik Harmim <harmim6@gmail.com>