Содержание блога

понедельник, 12 июля 2010 г.

генерация SVG на PHP

SVG очень хороший векторный формат, представленный схемой XML.
генератор XML в PHP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
<?php header("Content-type: image/svg+xml");

$dom = new DOMDocument('1.0', 'UTF-8');
$dom->xmlStandalone = false;

// указываем кодировку и версию xml файла
 
$root = $dom->createElement('svg');
// создаем корневой элемент
$root->setAttribute('xmlns:dc','http://purl.org/dc/elements/1.1/');
$root->setAttribute('xmlns:cc','http://creativecommons.org/ns#');
$root->setAttribute('xmlns:rdf','http://www.w3.org/1999/02/22-rdf-syntax-ns#');
$root->setAttribute('xmlns:svg','http://www.w3.org/2000/svg');
$root->setAttribute('xmlns','http://www.w3.org/2000/svg');
$root->setAttribute('xmlns:sodipodi','http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd');
$root->setAttribute('xmlns:inkscape','http://www.inkscape.org/namespaces/inkscape');
$root->setAttribute('width','1000');
$root->setAttribute('height','1000');
$root->setAttribute('id','svg2');
$root->setAttribute('sodipodi:docname','Пещера');
// добавляем в корневой элемен  атрибут с датой
 
$defs = $dom->createElement('defs');
$defs->setAttribute("id","defs4");
// создаем элемент defs
$inkscape_perspective = $dom->createElement('inkscape:perspective');
$inkscape_perspective->setAttribute('sodipodi:type','inkscape:persp3d');
$inkscape_perspective->setAttribute('inkscape:vp_x','0 : 526.18109 : 1');
$inkscape_perspective->setAttribute('inkscape:vp_y','0 : 1000 : 0');
$inkscape_perspective->setAttribute('inkscape:vp_z','744.09448 : 526.18109 : 1');
$inkscape_perspective->setAttribute('inkscape:persp3d-origin','372.04724 : 350.78739 : 1');
$inkscape_perspective->setAttribute('id','perspective10');
$defs->appendChild($inkscape_perspective);

$root->appendChild($defs);
// добавляем элемент defs в коневой элемент root


$sodipodi_namedview=$dom->createElement('sodipodi:namedview');
$sodipodi_namedview->setAttribute('id','base');
$sodipodi_namedview->setAttribute('pagecolor','#ffffff');
$sodipodi_namedview->setAttribute('bordercolor','#666666');
$sodipodi_namedview->setAttribute('borderopacity','1.0');
$sodipodi_namedview->setAttribute('inkscape:pageopacity','0.0');
$sodipodi_namedview->setAttribute('inkscape:pageshadow','2');
$sodipodi_namedview->setAttribute('inkscape:zoom','0.35');
$sodipodi_namedview->setAttribute('inkscape:cx','375');
$sodipodi_namedview->setAttribute('inkscape:cy','520');
$sodipodi_namedview->setAttribute('inkscape:document-units','px');
$sodipodi_namedview->setAttribute('inkscape:current-layer','layer1');
$sodipodi_namedview->setAttribute('showgrid','false');
$sodipodi_namedview->setAttribute('inkscape:window-width','1920');
$sodipodi_namedview->setAttribute('inkscape:window-height','1024');
$sodipodi_namedview->setAttribute('inkscape:window-x','-4');
$sodipodi_namedview->setAttribute('inkscape:window-y','-4');
$sodipodi_namedview->setAttribute('inkscape:window-maximized','1');
$root->appendChild($sodipodi_namedview);

$metadata=$dom->createElement('metadata');
$metadata->setAttribute('id','metadata7');
    $rdf_RDF=$dom->createElement('rdf:RDF');
        $cc_Work=$dom->createElement('cc:Work');
        $cc_Work->setAttribute('rdf:about','');
            $dc_format=$dom->createElement('dc:format');
            $text = $dom->createTextNode('image/svg+xml');
            $dc_format->appendChild($text);
            $cc_Work->appendChild($dc_format);
            
            $dc_type=$dom->createElement('dc:type');
            $dc_type->setAttribute('rdf:resource','http://purl.org/dc/dcmitype/StillImage');
            $cc_Work->appendChild($dc_type);
            
            $dc_title=$dom->createElement('dc:title');
            $cc_Work->appendChild($dc_title);
        $rdf_RDF->appendChild($cc_Work);
    $metadata->appendChild($rdf_RDF);
$root->appendChild($metadata);

$g=$dom->createElement('g');
$g->setAttribute('inkscape:label','Layer 1');
$g->setAttribute('inkscape:groupmode','layer');
$g->setAttribute('id','layer1');
    $path=$dom->createElement('path');
    $path->setAttribute('sodipodi:type','arc');
    $path->setAttribute('style','fill:#c8beb7');
    $path->setAttribute('id','path2816');
    $path->setAttribute('sodipodi:cx','178.57143');
    $path->setAttribute('sodipodi:cy','150.93361');
    $path->setAttribute('sodipodi:rx','20');
    $path->setAttribute('sodipodi:ry','50');
    $path->setAttribute('d','m 228.57143,150.93361 a 50,50 0 1 1 -100,0 50,50 0 1 1 100,0 z');
    $g->appendChild($path);
$root->appendChild($g);    
//=============================================
$dom->appendChild($root);
// публикуем корневой элемент
$sxml=$dom->saveXML();
$xhtmldtd='<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.0//EN" "http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd">'."\n";
$sxml=preg_replace('/\n/',$xhtmldtd,$sxml,1);
$tsxml=str_replace(">",">\n",$sxml);

echo ($tsxml);
// вывод дерева
?>



Важная мелочь! первая строчка должна быть<?php header("Content-type: image/svg+xml"); для этого нужно чтобы обязательно файл не содержал ни каких символов перед <?php если это формат unicode то он может добавить невидимый символ *BOM, для этого нужно сохранять без этого *BOM если появляется ошибка :
Cannot add header information - headers already sent
то нужно в другом редакторе пересохранить, например NotePad++ мне помог, пересохранил и снова в Adobe dreamwaver работаю

Комментариев нет:

Отправить комментарий