msztorc / svg-convert
提供将 SVG 文件转换为其他格式的辅助工具,包括 PNG 和 PDF。
Requires (Dev)
- phpunit/phpunit: 4.0.*
This package is auto-updated.
Last update: 2024-09-24 03:33:49 UTC
README
svg-convert
提供将 SVG 文件转换为其他格式的辅助工具,尤其是 PNG 和 PDF。该软件包包含 CLI(phantomjs 脚本)以渲染,以及 PHP 适配器以使用 phantomjs 或 rsvg-convert(librsvg)操作和转换 SVG 格式。
安装 PhantomJS
在安装 PhantomJS 之前,您需要在本系统上安装一些必需的软件包。您可以使用以下命令安装所有这些软件包
sudo apt-get update
sudo apt-get install build-essential chrpath libssl-dev libxft-dev libfreetype6-dev libfreetype6 libfontconfig1-dev libfontconfig1 -y
接下来,您需要下载 PhantomJS。您可以从其官方网站下载 PhantomJS 的最新稳定版本。
wget https://bitbucket.org/ariya/phantomjs/downloads/phantomjs-2.1.1-linux-x86_64.tar.bz2
将下载的存档文件解压到所需的系统位置
sudo tar xvjf phantomjs-2.1.1-linux-x86_64.tar.bz2 -C /usr/local/share/
接下来,将 PhantomJS 二进制文件的符号链接创建到系统 bin 目录
sudo ln -s /usr/local/share/phantomjs-2.1.1-linux-x86_64/bin/phantomjs /usr/local/bin/
全局使用 psvg-convert 脚本
chmod +x src/js/psvg-convert.js sudo cp src/js/psvg-convert.js /usr/local/bin/psvg-convert
安装 RSVG(如果您想使用 librsvg 转换 svg 的话)
sudo apt-get update
sudo apt-get install libcairo2-dev/libspectre-dev librsvg2-dev \ libpoppler-glib-dev librsvg2-bin
示例
使用 PhantomJS CLI 脚本将 SVG 转换为 PNG 格式
psvg-convert inputfile.svg outputfile.png
用法
psvg-convert svgfile outputfile [--width=800 --height=600 --zoom=2.5 --format=png|pdf]
在 PHP 中转换 SVG 文件
当您想使用 PhantomJS 引擎时,使用带有 phantomjs
参数的初始化方法
$svg = (new SVG)->init('phantomjs');
或者如果您想使用 rsvg-convert
$svg = (new SVG)->init('rsvg');
<?php $svg = (new SVG)->init('phantomjs', 'inputfile.svg'); $svg->setFormat('png'); $svg->convert(); $svg->save('outputfile.png');
在 PHP 中操作和转换 SVG 文件
<?php $svg = (new SVG)->init('phantomjs', 'inputfile.svg'); $svg->setAttribute('path', 'fill', '#131C77'); //change fill color to all paths $svg->setFormat('png'); $svg->convert(); $svg->save('outputfile.png');
其他有用的方法
打开文件
$svg->open('file.svg'); //open file
设置缩放
$svg->setZoom(2.5); //enlarge original svg size up to 2.5x
设置大小
$svg->setWidth(500); //set output width to 500px $svg->setHeight(300); //set output height to 300px //echo 'output size: ' . $svg->getWidth() . 'x' . $svg->getHeight();
设置输出格式
$svg->setFormat('pdf'); //set output format to pdf //echo 'output format: ' . $svg->getFormat();
显示文件
$svg->display(); //display image
下载文件
$svg->download(); //download file
保存文件
$svg->save('file.png'); //save file
图像测试
您可以在单元测试文件夹中找到的所有图像。
SVG 操作
$svg = (new SVG)->init('phantomjs', 'debian.svg');
$svg->setAttribute('path', 'fill', '#131C77')->setFormat('png')->convert()->save('debian-blue.png');
$svg->setAttribute('path', 'fill', '#06A70D')->setFormat('png')->convert()->save('debian-green.png');
$svg1 = (new SVG)->init('phantomjs', 'ubuntu.svg')->setFormat('png')->convert()->save('ubuntu1.png');
$svg2 = (new SVG)->init('phantomjs', 'ubuntu.svg')
->setAttribute('use[1]', 'fill', '#f40')
->setAttribute('use[2]', 'fill', '#f80')
->setAttribute('use[3]', 'fill', '#d00')
->setAttribute('use[4]', 'fill', '#f40')
->setAttribute('use[5]', 'fill', '#f80')
->setAttribute('use[6]', 'fill', '#d00')
->setFormat('png')->convert()->save('ubuntu2.png');
$svg3 = (new SVG)->init('phantomjs', 'ubuntu.svg')
->setAttribute('use[1]', 'fill', '#f80')
->setAttribute('use[2]', 'fill', '#d00')
->setAttribute('use[3]', 'fill', '#f40')
->setAttribute('use[4]', 'fill', '#f80')
->setAttribute('use[5]', 'fill', '#d00')
->setAttribute('use[6]', 'fill', '#f40')
->setFormat('png')->convert()->save('ubuntu3.png');
使用 ImageMagick 创建 gif
convert -loop 0 -delay 25 ubuntu1.png ubuntu2.png ubuntu3.png ubuntu.gif
许可
MIT