2011-01-06 79 views
8

我試圖用PHP創建矢量圖形。我曾嘗試過開羅,但我無法使其開始工作。我明白imageMagick具有矢量功能,但php.net上的文檔非常差,有人能讓我走向正確的方向嗎?這些想法是能夠將圖形保存到EPS。我還需要能夠使用不同的字體來輸出文本。用PHP創建矢量圖形

+0

您是否考慮過使用PDF而不是EPS? (通常對創建PDF有更多的支持。) – 2011-01-06 22:22:16

+0

nope基本上該文件需要通過Adobe Illustrator進行編輯。不知道這是否可能與PDF。我將考慮使用SVG這是開羅使用,但我無法找到它的PHP包裝好文檔。我有開羅安裝,但我不知道如何開始創建一個圖像。 PHP只是遍佈各處的錯誤 – jef2904 2011-01-06 23:34:49

回答

2

我知道這是很老的問題,但我幾個星期前,有一些問題,並解決它爲我自己,希望這個答案可以幫助別人。 開羅圖書館有PHP綁定,但它也有一些錯誤格式之間的轉換 - 忘記它。我們在開始時需要一些本地的東西。查看SVG格式 - 在編輯器(我使用Inkscape)中打開您的矢量圖像並將其保存爲SVG文件。之後,你可以通過PHP來改變它,就像xml文件一樣。在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:

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

其他提示:

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

您可以從PHP調用它

1)安裝最新版本的Inkscape中。我在openSuse 12.3上測試過它 - 效果很好。

2)將所有自定義字體安裝到系統字體。

0

我不能告訴你如何在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);