[英]Creating Vector Graphics with PHP
我試圖在PHP中創建矢量圖形。 我試過開羅,但是我無法讓它開始工作。 我明白imageMagick有矢量功能,但php.net上的文檔非常差,有人可以引導我朝着正確的方向前進嗎? 這些想法是為了將圖形保存到EPS。 我還需要能夠使用不同的字體來輸出文本。
雖然你想創建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
我知道這是一個很老的問題,但幾周前我遇到了一些問題並為自己解決了這個問題,希望這個答案對某人有所幫助。 開羅庫有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:
#!/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)將所有自定義字體安裝到系統字體。
我不能告訴你如何在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);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.