-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathpng2svg.php
127 lines (98 loc) · 2.78 KB
/
png2svg.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
<?php
// convert PNG image to SVG
$pixel_style = array(
'fill' => '',
'fill-opacity' => 1,
'stroke' => '#d2d2d2',
'stroke-linejoin' => 'round',
'stroke-opacity' => 1,
);
$pixel_size = 10;
$filename = $argv[1];
if (! file_exists($filename) ) {
die('No existe la imágen: ' . $filename);
}
// read image
$size = getimagesize($filename);
$width = $size[0];
$height = $size[1];
// open image into a true color image
$orimage = imagecreatefrompng($filename);
$im = imagecreatetruecolor($width,$height);
imagecopyresampled($im,$orimage,0,0,0,0, $width,$height,$width,$height);
// header
output_head();
// body
$svg = '';
$colors = array();
for($y = 0; $y < $height; $y++) {
for($x = 0; $x < $width; $x++) {
// get pixel at (x,y)
$rgb = imagecolorat($im, $x, $y);
$r = ($rgb >> 16) & 0xFF;
$g = ($rgb >> 8) & 0xFF;
$b = $rgb & 0xFF;
// skip black pixels
if ($rgb == 0) continue;
$color = sprintf("#%02x%02x%02x", $r, $g, $b);
// calculate color for border
$dark_factor = 0.9;
$border_color = sprintf(
"#%02x%02x%02x",
(int) ($r * $dark_factor),
(int) ($g * $dark_factor),
(int) ($b * $dark_factor)
);
// calculamos el estilo del pixel
$style = $pixel_style;
$style['fill'] = $color;
$style['stroke'] = $border_color;
// guardamos el estilo para el color key
if (!isset($colors[$rgb])) {
$colors[$rgb] = $style;
}
$svg .= '<svg:rect height="10" width="10" x="' . $x * $pixel_size . '" y="' . $y * $pixel_size . '" style="' . to_svg_style($style) . '" />';
$svg .= "\n";
}
}
$y += 3 * $pixel_size;
$x = 0;
foreach($colors as $color => $style) {
$svg .= '<svg:rect height="10" width="10" x="' . 2 * $x * $pixel_size . '" y="' . $y * $pixel_size . '" style="' . to_svg_style($style) . '" />';
$x++;
}
echo $svg;
// footer
output_foot();
exit(0);
function to_svg_style($s) {
$l = array();
foreach($s as $k => $v) {
$l[] = "$k:$v";
}
return implode($l, ';');
}
function output_head() {
$svg = '<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!-- Created with Inkscape (http://www.inkscape.org/) -->
<svg
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:cc="http://creativecommons.org/ns#"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:svg="http://www.w3.org/2000/svg"
xmlns="http://www.w3.org/2000/svg"
width="744.09448819"
height="1052.3622047"
id="svg4435"
version="1.1">
<g
inkscape:label="Layer 1"
inkscape:groupmode="layer"
id="layer1">';
echo $svg;
}
function output_foot() {
$svg = ' </g></svg>';
echo $svg;
}
// vim: se ts=4 sw=4 ai expandtab: