webiny / image
Requires
- php: ^7
- ext-gd: *
- imagine/imagine: 0.6.*
- webiny/config: ~1.6
- webiny/event-manager: ~1.6
- webiny/std-lib: ~1.6
- webiny/storage: ~1.6
Requires (Dev)
- mybuilder/phpunit-accelerator: dev-master
- phpunit/phpunit: ~6
README
Image
组件提供图像处理的基本功能。组件还修复了由 iPhone 或 Android 等智能手机拍摄的图片上的旋转错误。
安装组件
安装组件的最佳方式是使用 Composer。
composer require webiny/image
要获取该包的附加版本,请访问 Packagist 页面。
使用方法
此组件与 Storage
组件深度耦合,因此建议您首先熟悉该组件,如果您还没有的话。
在 基本功能 下,意味着您可以对任何图像执行以下操作
调整大小
裁剪
旋转
但在您进行任何操作之前,您必须首先加载一张图片。您可以通过调用 ImageLoader
类的适当方法从多个不同的来源加载图片
create
- 创建一个空白图片open
- 从给定的 File 存储实例创建图像load
- 从给定的二进制字符串创建图像resource
- 从给定的(流)资源创建图像
这些方法中的每一个都返回一个 ImageInterface
实例。使用 ImageInterface
,您可以对加载的图像执行定义的操作。
一旦您完成了图像操作,您可以通过调用 save
方法保存它,或者通过调用 show
方法输出它。
以下是一个使用示例
// storage $imageStorage = \Webiny\Component\ServiceManager\ServiceManager::getInstance()->getService('storage.local'); $image = new \Webiny\Component\Storage\File\File('embed.jpg', $imageStorage); // load the image using the `open` method $imgInstance = \Webiny\Component\Image\ImageLoader::open($image); // perform manipulations $imgInstance->resize(800, 800) ->crop(200, 200, 50, 40); ->rotate(30, 'bfbfbf'); // save the new image $destination = new \Webiny\Component\Storage\File\File('embed-rotated.jpg', $imageStorage); $result = $imgInstance->save($destination); // if you don't set the destination, the original image will be overwritten
使用 ImageTrait
加载图像并创建 ImageInterface 实例的一个更简单的方法是使用 ImageTrait。以下是从 \Webiny\Component\Storage\File\File 对象加载图像的示例
class MyClass{ use \Webiny\Component\Image\ImageTrait; function __construct(){ $imageStorage = \Webiny\Component\ServiceManager\ServiceManager::getInstance()->getService('Storage.Local'); $image = new \Webiny\Component\Storage\File\File('embed.jpg', $imageStorage); $imgInstance = $this->image($image); $imgInstance->resize(800, 800); $imgInstance->crop(200, 200, 50, 40); $imgInstance->rotate(30, 'bfbfbf'); $imgInstance->show(); } }
以下是从文件存储密钥和存储驱动程序加载图像的另一个示例
class MyClass{ use \Webiny\Component\Image\ImageTrait; function __construct(){ $imgInstance = $this->image('embed.jpg', 'Local'); $imgInstance->resize(800, 800); $imgInstance->crop(200, 200, 50, 40); $imgInstance->rotate(30, 'bfbfbf'); $imgInstance->show(); } }
所以正如您所看到的,您可以通过直接传递 File 对象或传递文件密钥和存储服务名称来加载和创建 ImageInterface 实例。
桥接器
图像库的默认桥接器使用 Bulat Shakirzyanov 的 Imagine
库(https://github.com/avalanche123/Imagine),该库执行所有图像操作。
如果您想扩展或更改当前桥接的库,您需要创建两个类
- 一个实现了
Webiny\Component\Image\Bridge\ImageLoaderInterface
的加载类 - 一个扩展了
Webiny\Component\Image\Bridge\AbstractImage
的图像操作类
之后,只需更改配置中的 Bridge
,框架将使用您的桥接器而不是默认的桥接器。
配置
Image
组件需要非常少的配置。以下是一个示例
Image: Library: gd Quality: 90
Library
参数定义了将使用哪个图像库。由 Imagine
库支持的库有
gd
- 使用 PHP 的原生 GD 库imagick
- 使用 ImageMagick 库,需要 php-imagick 扩展gmagick
- 使用 GraphicsMagick API,也需要 php-gmagick 扩展
Quality
参数定义了保存图像时的质量。默认质量为 90(最大为 100)。
要注册配置到组件,只需调用 Image::setConfig($pathToYamlConfig)
。
资源
要运行单元测试,您需要使用以下命令
$ cd path/to/Webiny/Component/Image/
$ composer.phar install
$ phpunit