svsoft / thumbnails
用于调整图片大小和创建缩略图的php组件
dev-master
2019-05-16 09:36 UTC
Requires
- php: >=7.0
- imagine/imagine: *
This package is auto-updated.
Last update: 2024-09-16 21:30:10 UTC
README
PHP图像调整组件。使用多种处理器(调整大小、裁剪、填充、水印)创建图片缩略图。您还可以创建自己的处理器,并将其添加到缩略图描述中。
功能
- 支持不同类型的源图像存储。
- 支持各种类型的缩略图存储
- 能够创建自己的存储类型(ftp、数据库、Http)
- 在之前描述的缩略图中简单应用新处理器
- 能够创建自己的自定义图像处理器
在库中到处都使用了接口,您可以在自己的类中实现必要的逻辑
对于在YII2上开发的人,此库有一个适配器 svsoft/yii2-thumbnails
安装
Composer
将其添加到 composer.json 中
{ "require": { "svsoft/thumbnails": "*" } }
然后运行 php composer.phar update
或者
运行 php composer.phar require svsoft/thumbnails
配置和初始化组件
使用本地源图像存储和本地缩略图存储创建组件
// Creating local image storage $imageStorage = new ImageLocalStorage(); // Create local thumbnail storage // $dirPath - path to direcrory where storage thambnail files // $webDirPath - public url the same folder $thumbStorage = new ThumbLocalStorage($dirPath, $webDirPath); // Based on these storages we create an object that creates thumbnails. $creator = new ThumbCreator($imageStorage, $thumbStorage); // Create thumbnail manager, where description thumbnails $thumbManager = new ThumbManager(); $thumbManager->setThumbs([ 'content' => new Thumb([ // simple resize 1140x1140 new Resize(1140, 1140), ]), 'favicon' => new Thumb([ // Resize fixed size filled with transparent fields new ResizeFillingHandler(40, 40), ]), 'productDetail' => new Thumb([ // image handler fixed size with crop centered new ResizeCropHandler(600, 600), // watermark handler with transparent 30% new WatermarkHandler('/var/www/site.ru/....', 30) ]), // ... ]); // Create component that create thumbnails. $thumbnails = new Thumbnails($thumbManager, $creator);
使用
组件的使用位置取决于应用和开发者。通过服务定位器、单例、容器等。
示例输出favicon
<? /** @var ThumbnailsInterface $thumbnails */ ?> <link rel="shortcut icon" href="<?=$thumbnails->thumb('/var/www/site.ru/images/1.jpg', 'favicon') ?>" type="image/png">
示例输出标签img中的产品图片
<? /** @var ThumbnailsInterface $thumbnails */ ?> <img src="<?=$thumbnails->thumb('/var/www/site.ru/images/product/product-1.jpg', 'product') ?>">
从实现接口ThumbInterface的对象创建示例
$thumb = new Thumb([ // Resize fixed size filled with transparent fields new ResizeCropHandler(100, 100), ]); /** @var ThumbnailsInterface $thumbnails */ // $url - thumbnail url $url = $thumbnails->getCreator()->create('/var/www/site.ru/images/product/product-1.jpg', $thumb);