使用 PHP 创建矢量图形

发布于 2024-10-10 10:08:23 字数 135 浏览 9 评论 0原文

我正在尝试用 PHP 创建矢量图形。我试过开罗,但没能让它发挥作用。我知道 imageMagick 具有矢量功能,但 php.net 上的文档非常差,有人可以引导我走向正确的方向吗?这个想法是能够将图形保存到 EPS。我还需要能够使用不同的字体来输出文本。

Im trying to create vector graphics in PHP. Ive tried Cairo and I havn't been able to get it to work. I understand that imageMagick has vector functionality but the documentation on php.net is very poor can some one lead me in the right direction? The ideas is to be able to save the graphic to EPS. I also need to be able to use different fonts to output text.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

_蜘蛛 2024-10-17 10:08:23

尽管您想要创建 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

Although you're looking to create eps I would still aim to create a PDF. PDF's are fully editable in any major package: Adobe Illustrator, Corel Draw, Xara Pro etc

TCPDF works well and there is a bunch of code samples including fonts and support for vector images eps and ai output to PDF

eps/ai example http://www.tcpdf.org/examples/example_032.pdf

All the examples and php code http://www.tcpdf.org/examples.php

荆棘i 2024-10-17 10:08:23

我知道这是一个很老的问题,但几周前我遇到了一些问题并自己解决了它,希望这个答案对某人有所帮助。
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)将所有自定义字体安装到系统字体。

I know what this is quite old question, but I had some problem few weeks ago and solve it for myself, hope this answer helps someone.
Cairo library have PHP bindings, but it also have few bugs which break convertation between formats - forget about it. We need something native here on start. Look at SVG format - open your vector image in editor (I use Inkscape) and save it as SVG file. After that you can change it via php just like xml file.
Adding custom fonts in 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

Ok, we have SVG with needed fonts, but we need EPS. For converting SVG to EPS I used Inkscape with simple bash script svg2eps.sh:

#!/bin/bash
inkscape -f $1 -z -T -E $2

You can call it from php:

 exec('/path/to/svg2eps.sh /path/to/in.svg path/to/out.eps');

Other tips:

1)Install latest version of Inkscape. I tested it on openSuse 12.3 - works great.

2)Install all custom fonts to system fonts.

热风软妹 2024-10-17 10:08:23

我无法告诉您如何在 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);

I can't tell you how to create vector images in PHP but perhaps you would like a bit different approach- create raster images in PHP and convert them to vectors? It works okay for black & white images not sure about color ones.

<?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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文