Creating Vector Graphics with PHP Creating Vector Graphics with PHP php php

Creating Vector Graphics with 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 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 endfile_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/bashinkscape -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.