Skip to content

Commit c4a7459

Browse files
dnelson-nagiosLarsMichelsen
authored andcommitted
extend iconset_size for SVGs
Extends iconset_size function to also resolve the image sizes of SVGs
1 parent 16a7e3a commit c4a7459

File tree

1 file changed

+41
-4
lines changed

1 file changed

+41
-4
lines changed

share/server/core/sources/general.php

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,51 @@ function iconset_size($iconset) {
5050
$fileType = $CORE->getIconsetFiletype($iconset);
5151
$iconPath = path('sys', 'global', 'icons').'/'.$iconset.'_ok.'.$fileType;
5252
$iconPathLocal = path('sys', 'local', 'icons').'/'.$iconset.'_ok.'.$fileType;
53-
if(file_exists($iconPathLocal))
54-
return getimagesize($iconPathLocal);
55-
elseif(file_exists($iconPath))
56-
return getimagesize($iconPath);
53+
if(file_exists($iconPathLocal)) {
54+
if($fileType == "svg") {
55+
return svg_size($iconPathLocal);
56+
}
57+
else {
58+
return getimagesize($iconPathLocal);
59+
}
60+
}
61+
elseif(file_exists($iconPath)){
62+
if($fileType == "svg") {
63+
return svg_size($iconPath);
64+
}
65+
else {
66+
return getimagesize($iconPath);
67+
}
68+
}
5769
else
5870
return array(0, 0);
5971
}
6072

73+
function svg_size($filepath) {
74+
if (!file_exists($filepath)) {
75+
return array(0, 0);
76+
}
77+
78+
$doc = new DOMDocument();
79+
$doc->load($filepath);
80+
81+
$svg = $doc->getElementsByTagName('svg')->item(0);
82+
$width = $svg->getAttribute('width');
83+
$height = $svg->getAttribute('height');
84+
85+
// Fallback to viewBox
86+
if (empty($width) || empty($height)) {
87+
$viewBox = $svg->getAttribute('viewBox');
88+
$parts = preg_split('/[\s,]+/', $viewBox);
89+
if (count($parts) === 4) {
90+
$width = $parts[2];
91+
$height = $parts[3];
92+
}
93+
}
94+
95+
return array(floatval($width), floatval($height));
96+
}
97+
6198
function shape_size($icon) {
6299
$iconPath = path('sys', 'global', 'shapes').'/'.$icon;
63100
$iconPathLocal = path('sys', 'local', 'shapes').'/'.$icon;

0 commit comments

Comments
 (0)