contao / imagine-svg
Contao Imagine SVG 库
1.0.3
2021-08-10 09:33 UTC
Requires
- php: ^7.2 || ^8.0
- ext-dom: *
- imagine/imagine: ^1.1
Requires (Dev)
- contao/easy-coding-standard: ^3.0
- contao/test-case: ^4.4
- phpstan/phpstan: ^0.12
- phpstan/phpstan-phpunit: ^0.12.8
- phpstan/phpstan-symfony: ^0.12.6
- phpunit/phpunit: ^8.5.4
- psalm/plugin-phpunit: ^0.15
- psalm/plugin-symfony: ^2.0
- slam/phpstan-extensions: ^5.0
- symfony/filesystem: ^5.0
- thecodingmachine/phpstan-strict-rules: ^0.12
- vimeo/psalm: ^4.1
README
本项目实现了 Imagine 的接口,允许您对 SVG 图像进行简单修改。它在 Contao 中用于处理 SVG 图像的即时调整大小。
安装
php composer.phar require contao/imagine-svg
用法
use Contao\ImagineSvg\Imagine; use Imagine\Image\Box; use Imagine\Image\Point; $imagine = new Imagine(); $imagine ->open('/path/to/image.svg') ->crop(new Point(50, 50), new Box(100, 100)) ->resize(new Box(40, 40)) ->save('/path/to/thumbnail.svg') ; $image = $imagine->open('/path/to/image.svg'); $image->effects() ->gamma(1.5) ->negative() ->grayscale() ->colorize($color) ->sharpen() ->blur(2) ; $image->save('/path/to/image.svg');
由于 SVG 图像的性质,getSize()
方法与其他实现略有不同。您可以通过以下示例检查返回值
use Contao\ImagineSvg\Imagine; use Contao\ImagineSvg\SvgBox; $imagine = new Imagine(); $size = $imagine->open('/path/to/image.svg')->getSize(); if (SvgBox::TYPE_NONE === $size->getType()) { // The image has no defined size } elseif (SvgBox::TYPE_ASPECT_RATIO === $size->getType()) { // The image has a relative size, $size->getWidth() and $size->getHeight() // should be treated as an aspect ratio } else { // The image has a defined size like a regular image // $size->getType() would return SvgBox::TYPE_ABSOLUTE in this case }