9

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.

Chris Smith
  • 18,244
  • 13
  • 59
  • 81
jef2904
  • 435
  • 2
  • 8
  • 15
  • Have you considered using PDF rather than EPS? (There's generally more support for creating PDF.) – John Parker Jan 06 '11 at 22:22
  • nope will basically the file need to be editable via Adobe Illustrator. Not sure if thats possible with a PDF. Im going to look into doing it with SVG which is what Cairo uses but i cant find good documentation on the PHP wrapper for it. I got Cairo to install but I don't know how to start creating an image. PHP just throughs errors everywhere – jef2904 Jan 06 '11 at 23:34
  • you don't need a wrapper to make svg with php. You just need to specify the correct `header` and print the svg like you would with html. – zzzzBov Jan 07 '11 at 07:07
  • Thasts the thing I don't want to output it to the browser I want to save it as an EPS file – jef2904 Jan 08 '11 at 17:48

4 Answers4

4

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.

ToxaBes
  • 1,587
  • 8
  • 17
4

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

33v
  • 103
  • 7
0

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);
Jack
  • 173
  • 1
  • 3
0

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

erjiang
  • 44,417
  • 10
  • 64
  • 100
Diablo
  • 3,378
  • 1
  • 22
  • 28
  • Ok i looked into both links and the second one makes it seem impossible to do what I want with Imagemagick & magickwand. Guess Ill drop that and more look into cairo – jef2904 Jan 06 '11 at 23:31
  • This is incorrect and should not have a positive answer. Imagemagick does rasterize vector images, but it does not vectorize raster images. – CommonKnowledge Jul 21 '15 at 21:11
  • Yes Imagemagick can create MVG. I never wrote imagemagick can vectorize rasters. Where did you pick that from? Whatever...rolleyes... – Diablo Jul 22 '15 at 15:06