svsoft / yii2-thumbnails
基于 svsoft/thumbnails 的 Yii2 组件,用于调整图像大小并创建缩略图
dev-master
2019-05-20 06:28 UTC
Requires
- php: >=7.0
- svsoft/thumbnails: *
This package is auto-updated.
Last update: 2024-09-20 18:18:38 UTC
README
它是基于 svsoft/thumbnails 组件的 Yii2 适配器,用于在原生 PHP 上调整图像大小。
使用各种处理器创建图片缩略图:调整大小、裁剪、填充、水印。您还可以创建自己的处理器,并将它们添加到缩略图描述中。
功能
- 支持不同类型的源图像存储。
- 支持各种类型的缩略图存储
- 能够创建自己的存储类型(FTP、数据库、Http)
- 简单地将新处理器应用于先前描述的缩略图
- 能够创建自己的自定义图像处理器
在库中到处使用接口,您可以在自己的类中实现必要的逻辑
安装
Composer
添加到 composer.json
{ "require": { "svsoft/yii2-thumbnails": "*" } }
然后运行 php composer.phar update
或者
运行 php composer.phar require svsoft/yii2-thumbnails
配置
添加到配置
'components' => [ // .... 'thumbnails' => [ 'class' => \svsoft\yii\thumbnails\Thumbnails::class, // 'dirPath' => '@app/web/resize', 'webDirPath' => '@web/resize', 'thumbs' => [ // Short description thumbnail 'logo'=> [ // simple resize 200x40 'width' => 200, 'height' => 40, ], 'favicon'=> [ // Resize fixed size filled with transparent fields 'class'=> \svsoft\yii\thumbnails\handlers\ResizeFillingHandlerFactory::class, 'width' => 40, 'height' => 40, ], // Full description of thumbnail 'product'=>[ 'class' => \svsoft\yii\thumbnails\ThumbFactory::class, 'handlers' => [ [ 'class' => \svsoft\yii\thumbnails\handlers\ResizeCropHandlerFactory::class, 'width' => 600, 'height' => 600, ], [ 'class' => \svsoft\yii\thumbnails\handlers\WatermarkHandlerFactory::class, 'watermarkFilePath' => '@app/web/images/watermark.jpg', 'opacity' => 50, ], ], ], // ... ] ], // .... ],
属性
- dirPath - 缩略图文件存储目录的路径
- webDirPath - 缩略图文件存储目录的公共 Web 路径
- thumbs - 缩略图配置列表
- $imageStorage - 不必需要。图像存储的描述。现在仅实现了默认设置的本地图像存储。(FTP、HTTP、数据库未实现!)
- $thumbStorage - 不必需要。缩略图存储的描述。现在仅实现了默认设置的本地图缩略图存储。(FTP、HTTP、数据库未实现!)
处理器类
\svsoft\yii\thumbnails\handlers\ResizeCropHandlerFactory - fixed resize and crop
\svsoft\yii\thumbnails\handlers\ResizeFillingHandlerFactory - fixed resize with filling fields
\svsoft\yii\thumbnails\handlers\ResizeHandlerFactory - resize to a certain size
\svsoft\yii\thumbnails\handlers\WatermarkHandlerFactory - watermark overlay
每个处理器都有其自己的设置,您可以在相应的类中看到它们
您可以添加自己的处理器。
使用
获取组件的位置取决于应用程序和开发者。通过服务定位器、单例、容器等。
示例输出 favicon
<link rel="shortcut icon" href="<?=Yii::$app->thumbnails->thumb('/var/www/site.ru/images/1.jpg', 'favicon') ?>" type="image/png">
示例输出 img 标签中的产品图片
<img src="<?=Yii::$app->thumbnails->thumb('/var/www/site.ru/images/product/product-1.jpg', 'product') ?>">