我试图在 PHP 中创建矢量图形。我试过开罗,但我无法让它工作。我知道 imageMagick 具有矢量功能,但 php.net 上的文档很差,有人能引导我走向正确的方向吗?想法是能够将图形保存到EPS。我还需要能够使用不同的字体来输出文本。
4 回答
我知道这是一个很老的问题,但是几周前我遇到了一些问题并自己解决了,希望这个答案对某人有所帮助。Cairo 库具有 PHP 绑定,但它也有一些破坏格式之间转换的错误 - 忘记它。我们一开始就需要一些本地的东西。查看 SVG 格式 - 在编辑器中打开您的矢量图像(我使用 Inkscape)并将其保存为 SVG 文件。之后,您可以像 xml 文件一样通过 php 更改它。在 SVG 中添加自定义字体:
$text_path = 'm 100,200'
$font_name = 'Some_font.ttf';
$font_size = '20px';
$font = base64_encode('font_file_content');
$text = 'Bla bla bla';
$font_svg = '<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<defs>
<path d="' . $text_path . '" id="font_id_123"/>
<style type="text/css">
<![CDATA[
@font-face {
font-family: ' . $font_name . ';
src: url("data:font/ttf;charset=utf-8;base64,' . $font . '");
]]>
</style>
</defs>
<text style="font-family: ' . $font_name . '; font-size: ' . $font_size . ';">
<textPath xlink:href="#font_id_123">' . $text . '</textPath>
</text>
</svg>';
$content = file_get_contents($svg_file); // $svg_file - your vector image
$content = substr($content, 0, -6); // cut last '</svg>' tag from file
$newContent = $content . $font_svg . '</svg>'; // add font to the end
file_put_contents($svg_file, $newContent); // save changes
好的,我们有带有所需字体的 SVG,但我们需要 EPS。为了将 SVG 转换为 EPS,我使用 Inkscape 和简单的 bash 脚本 svg2eps.sh:
#!/bin/bash
inkscape -f $1 -z -T -E $2
您可以从 php 调用它:
exec('/path/to/svg2eps.sh /path/to/in.svg path/to/out.eps');
其他提示:
1) 安装最新版本的 Inkscape。我在 openSuse 12.3 上对其进行了测试——效果很好。
2)将所有自定义字体安装到系统字体。
尽管您正在寻找创建 eps,但我仍然希望创建 PDF。PDF 可以在任何主要软件包中完全编辑:Adobe Illustrator、Corel Draw、Xara Pro 等
TCPDF 运行良好,有一堆代码示例,包括字体和对矢量图像 eps 和 ai 输出到 PDF 的支持
eps/ai 示例http://www.tcpdf.org/examples/example_032.pdf
所有示例和 php 代码http://www.tcpdf.org/examples.php
Try these links:
http://www.imagemagick.org/script/magick-vector-graphics.php
and
http://www.imagemagick.org/discourse-server/viewtopic.php?f=10&t=10144
我无法告诉您如何在 PHP 中创建矢量图像,但也许您想要一些不同的方法 - 在 PHP 中创建光栅图像并将它们转换为矢量?它适用于不确定彩色图像的黑白图像。
<?php
$im = imagecreatetruecolor(500,500);
//draw something on $im
imagepng($im, 'image.png');
$url = 'http://server.com/image.png'; //change to your server's domain
$data = json_decode(file_get_contents('http://api.rest7.com/v1/raster_to_vector.php?url=' . $url . '&format=svg'));
if (@$data->success !== 1)
{
die('Failed');
}
$vec = file_get_contents($data->file);
file_put_contents('vectors.svg', $vec);