-
Notifications
You must be signed in to change notification settings - Fork 0
/
camogen.svgexport.php
293 lines (262 loc) · 10.3 KB
/
camogen.svgexport.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
<?php
/***************************************************************************************************
* CAMOuflage GENerator for PHP
* _________ _____ ___ ____ ____ ____ ____
* / ___/ __ `/ __ `__ \/ __ \/ __ `/ _ \/ __ \
* / /__/ /_/ / / / / / / /_/ / /_/ / __/ / / /
* \___/\__,_/_/ /_/ /_/\____/\__, /\___/_/ /_/
* /____/
*
* @author Neil Withnall with thanks to Gael Lederrey and Ulf Åström
* @version 0.1 ("Stealthy Salamander")
* @licence MIT
* @system-requirements PHP 5.4+ with Imagick or GD extensions
*
* camogen for PHP (c) 2024 Neil Withnall (gammalogic). This software contains code that has been
* directly derived from, or inspired by, the camogen Python application (c) 2017 Gael Lederrey.
*
* Please see LICENSE for full copyright notice and licensing terms.
***************************************************************************************************/
class SVG_Export
{
public static $svg_paths = array();
public static function initialize()
{
self::$svg_paths = array();
}
public static function build_svg()
{
$svg = new SimpleXMLElement('<?xml version="1.0" encoding="UTF-8" standalone="no"?><svg/>');
$svg->addAttribute('xmlns', 'http://www.w3.org/2000/svg');
$svg->addAttribute('width', Pattern::$width);
$svg->addAttribute('height', Pattern::$height);
$svg->addAttribute('version', '1.1');
// Add pattern parameters as meta data
$metadata = $svg->addChild('metadata');
$camogen = $metadata->addChild('camogen');
$camogen->addAttribute('xmlns:xmlns:camogen', 'https://camogen-php.com/');
$camogen->addAttribute('version', CAMOGEN_VERSION);
$camogen->addChild('camogen:camogen:width', Pattern::$width);
$camogen->addChild('camogen:camogen:height', Pattern::$height);
$camogen->addChild('camogen:camogen:polygon-size', Pattern::$polygon_size);
$camogen->addChild('camogen:camogen:polygon-draw-style', Pattern::$polygon_draw_style);
$camogen->addChild('camogen:camogen:polygon-color-bleed', Pattern::$color_bleed);
$camogen->addChild('camogen:camogen:max-depth', Pattern::$max_depth);
$camogen_colors = $camogen->addChild('camogen:camogen:colors');
for ($i = 0; $i <= Pattern::$nbr_colors; $i++) {
$camogen_color = $camogen_colors->addChild('camogen:camogen:color');
$camogen_color->addAttribute('id', $i);
$camogen_color->addAttribute('color', Pattern::$colors[$i]);
}
$camogen->addChild('camogen:camogen:preprocess-distort', (Pattern::$preprocess_distort ? 'true' : 'false'));
$camogen->addChild('camogen:camogen:preprocess-hexagons', (Pattern::$preprocess_hexagons ? 'true' : 'false'));
if (Pattern::$preprocess_hexagons) {
$camogen->addChild('camogen:camogen:hexagon-percentage', Pattern::$hexagon_percentage);
$camogen->addChild('camogen:camogen:hexagon-height', Pattern::$hexagon_height);
}
$camogen->addChild('camogen:camogen:postprocess-spots', (Pattern::$postprocess_spots ? 'true' : 'false'));
if (Pattern::$postprocess_spots) {
$camogen->addChild('camogen:camogen:spots-amount', Pattern::$spots_amount);
$camogen->addChild('camogen:camogen:spots-radius-min', Pattern::$spots_radius_min);
$camogen->addChild('camogen:camogen:spots-radius-max', Pattern::$spots_radius_max);
$camogen->addChild('camogen:camogen:spots-sampling', Pattern::$spots_sampling);
}
$camogen->addChild('camogen:camogen:postprocess-rain', (Pattern::$postprocess_rain ? 'true' : 'false'));
if (Pattern::$postprocess_rain) {
$camogen->addChild('camogen:camogen:rain-stroke-width', Pattern::$rain_stroke_width);
$camogen->addChild('camogen:camogen:rain-stroke-width-padding-factor', Pattern::$rain_stroke_width_padding_factor);
$camogen->addChild('camogen:camogen:rain-stroke-length', Pattern::$rain_stroke_length);
$camogen->addChild('camogen:camogen:rain-stroke-segments', Pattern::$rain_stroke_segments);
$camogen->addChild('camogen:camogen:rain-stroke-line-thickness', Pattern::$rain_stroke_line_thickness);
$camogen->addChild('camogen:camogen:rain-stroke-color', Pattern::$rain_stroke_color);
}
$camogen->addChild('camogen:camogen:postprocess-pixelize', (Pattern::$postprocess_pixelize ? 'true' : 'false'));
if (Pattern::$postprocess_pixelize) {
$camogen->addChild('camogen:camogen:pixelize-percentage', Pattern::$pixelize_percentage);
$camogen->addChild('camogen:camogen:pixelize-sampling', Pattern::$pixelize_sampling);
$camogen->addChild('camogen:camogen:pixelize-density-x', Pattern::$pixelize_density_x);
$camogen->addChild('camogen:camogen:pixelize-density-y', Pattern::$pixelize_density_y);
}
$camogen->addChild('camogen:camogen:postprocess-motion-blur', (Pattern::$postprocess_motion_blur ? 'true' : 'false'));
if (Pattern::$postprocess_motion_blur) {
$camogen->addChild('camogen:camogen:motion-blur-radius', Pattern::$motion_blur_radius);
$camogen->addChild('camogen:camogen:motion-blur-sigma', Pattern::$motion_blur_sigma);
$camogen->addChild('camogen:camogen:motion-blur-angle', Pattern::$motion_blur_angle);
}
// Apply a stroke to some types of shapes to cover any visible gaps; these gaps would not
// normally be visible in the rasterized versions because of anti-aliasing
$default_polygon_stroke_width = (int) 1;
$default_rect_stroke_width = (int) 1;
foreach (self::$svg_paths as $svg_path) {
switch ($svg_path['type']) {
case 'circle':
$circle = $svg->addChild('circle');
$circle->addAttribute('cx', $svg_path['cx']);
$circle->addAttribute('cy', $svg_path['cy']);
$circle->addAttribute('r', $svg_path['r']);
$circle->addAttribute('style', self::build_style_attribute(
$svg_path['fill']
));
break;
case 'path':
$path = $svg->addChild('path');
$path->addAttribute('d', $svg_path['points']);
$path->addAttribute('style', self::build_style_attribute(
$svg_path['fill'],
$svg_path['stroke_color'],
$svg_path['stroke_width']
));
break;
case 'polygon':
$polygon = $svg->addChild('polygon');
$polygon->addAttribute('points', implode(' ', $svg_path['points']));
$polygon->addAttribute('style', self::build_style_attribute(
$svg_path['fill'],
$svg_path['fill'],
$default_polygon_stroke_width
));
break;
case 'rect':
$rect = $svg->addChild('rect');
$rect->addAttribute('x', $svg_path['x']);
$rect->addAttribute('y', $svg_path['y']);
$rect->addAttribute('width', $svg_path['width']);
$rect->addAttribute('height', $svg_path['height']);
$rect->addAttribute('style', self::build_style_attribute(
$svg_path['fill'],
$svg_path['fill'],
$default_rect_stroke_width
));
break;
default:break;
}
}
return self::pretty_print_xml($svg->asXML());
}
public static function add_svg_circle($cx, $cy, $r, $fill)
{
self::$svg_paths[] = array(
'type' => 'circle',
'cx' => $cx,
'cy' => $cy,
'r' => $r,
'fill' => $fill,
);
}
public static function add_svg_cubic_bezier_curve($points)
{
$points_temp = null;
$points_temp .= 'M ' . $points[0] . ' ' . $points[1];
$points_temp .= ' C ';
$points_temp .= $points[2] . ' ' . $points[3] . ',';
$points_temp .= $points[4] . ' ' . $points[5] . ',';
$points_temp .= $points[6] . ' ' . $points[7];
self::$svg_paths[] = array(
'type' => 'path',
'points' => $points_temp,
'fill' => 'transparent',
'stroke_color' => Pattern::$rain_stroke_color,
'stroke_width' => Pattern::$rain_stroke_line_thickness,
);
}
public static function add_svg_polygon($points, $fill)
{
$points_temp = array();
$nbr_points = count($points);
if (Image_Generator::get_extension() === 'Imagick') {
for ($i = 0; $i < $nbr_points; $i++) {
$points_temp[] = $points[$i]['x'] . ',' . $points[$i]['y'];
}
} else if (Image_Generator::get_extension() === 'GD') {
for ($i = 0; $i < $nbr_points; $i += 2) {
$points_temp[] = $points[$i] . ',' . $points[$i + 1];
}
}
self::$svg_paths[] = array(
'type' => 'polygon',
'points' => $points_temp,
'fill' => $fill,
);
}
public static function add_svg_rectangle($x, $y, $width, $height, $fill)
{
self::$svg_paths[] = array(
'type' => 'rect',
'x' => $x,
'y' => $y,
'width' => $width,
'height' => $height,
'fill' => $fill,
);
}
public static function build_style_attribute($fill_color=null, $stroke_color=null, $stroke_width=null)
{
$attribute = null;
$attribute_array = array();
if ($fill_color != null) {
$attribute_array['fill'] = $fill_color;
}
if ($stroke_color != null) {
$attribute_array['stroke'] = $stroke_color;
}
if ($stroke_width != null) {
$attribute_array['stroke-width'] = $stroke_width;
}
array_walk(
$attribute_array,
function ($value, $key) use (&$attribute) {
$attribute .= $key . ':' . $value . ';';
}
); // fill:#668F46;stroke:#668F46;stroke-width:1;
return $attribute;
}
public static function pretty_print_xml($xml)
{
// Basic pretty printer routine; SimpleXML will normally output all of the data after the
// XML string as a single line
$indent = "\t";
$search = array(
'<camogen ',
'<camogen:',
'</camogen:colors>',
'</camogen>',
'<circle',
'<metadata>',
'</metadata>',
'<path',
'<polygon',
'<rect',
'</svg>',
);
$replace = array(
PHP_EOL . str_repeat($indent, 2) . '<camogen ',
PHP_EOL . str_repeat($indent, 3) . '<camogen:',
PHP_EOL . str_repeat($indent, 3) . '</camogen:colors>',
PHP_EOL . str_repeat($indent, 2) . '</camogen>',
PHP_EOL . str_repeat($indent, 1) . '<circle',
PHP_EOL . str_repeat($indent, 1) . '<metadata>',
PHP_EOL . str_repeat($indent, 1) . '</metadata>',
PHP_EOL . str_repeat($indent, 1) . '<path',
PHP_EOL . str_repeat($indent, 1) . '<polygon',
PHP_EOL . str_repeat($indent, 1) . '<rect',
PHP_EOL . str_repeat($indent, 0) . '</svg>'
);
return str_replace($search, $replace, $xml);
}
public static function save_svg_to_file($filename)
{
$svg = self::build_svg();
file_put_contents($filename, $svg);
unset($svg);
try {
if (!file_exists($filename)) {
throw new Exception('Sorry, an error occurred (SVG was not saved to file)');
}
if (filesize($filename) === 0) {
throw new Exception('Sorry, an error occurred (SVG saved but file is 0 bytes in size)');
}
} catch (Exception $e) {
die($e->getMessage());
}
}
}