Skip to content

Commit 7964058

Browse files
authored
Merge pull request #47670 from nextcloud/fix/touchicon-calc
fix: Properly calculate intermediate icon for touch icon generation
2 parents 873c42b + 463c21d commit 7964058

1 file changed

Lines changed: 12 additions & 17 deletions

File tree

apps/theming/lib/IconBuilder.php

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -124,9 +124,10 @@ public function renderAppIcon($app, $size) {
124124
$color = $this->themingDefaults->getColorPrimary();
125125

126126
// generate background image with rounded corners
127+
$cornerRadius = 0.2 * $size;
127128
$background = '<?xml version="1.0" encoding="UTF-8"?>' .
128-
'<svg xmlns="http://www.w3.org/2000/svg" version="1.1" xmlns:cc="http://creativecommons.org/ns#" width="512" height="512" xmlns:xlink="http://www.w3.org/1999/xlink">' .
129-
'<rect x="0" y="0" rx="100" ry="100" width="512" height="512" style="fill:' . $color . ';" />' .
129+
'<svg xmlns="http://www.w3.org/2000/svg" version="1.1" xmlns:cc="http://creativecommons.org/ns#" width="' . $size . '" height="' . $size . '" xmlns:xlink="http://www.w3.org/1999/xlink">' .
130+
'<rect x="0" y="0" rx="' . $cornerRadius . '" ry="' . $cornerRadius . '" width="' . $size. '" height="' . $size . '" style="fill:' . $color . ';" />' .
130131
'</svg>';
131132
// resize svg magic as this seems broken in Imagemagick
132133
if ($mime === 'image/svg+xml' || substr($appIconContent, 0, 4) === '<svg') {
@@ -136,22 +137,17 @@ public function renderAppIcon($app, $size) {
136137
$svg = $appIconContent;
137138
}
138139
$tmp = new Imagick();
140+
$tmp->setBackgroundColor(new ImagickPixel('transparent'));
141+
$tmp->setResolution(72, 72);
139142
$tmp->readImageBlob($svg);
140143
$x = $tmp->getImageWidth();
141144
$y = $tmp->getImageHeight();
142-
$res = $tmp->getImageResolution();
143145
$tmp->destroy();
144146

145-
if ($x > $y) {
146-
$max = $x;
147-
} else {
148-
$max = $y;
149-
}
150-
151147
// convert svg to resized image
152148
$appIconFile = new Imagick();
153-
$resX = (int)(512 * $res['x'] / $max * 2.53);
154-
$resY = (int)(512 * $res['y'] / $max * 2.53);
149+
$resX = (int)(72 * $size / $x);
150+
$resY = (int)(72 * $size / $y);
155151
$appIconFile->setResolution($resX, $resY);
156152
$appIconFile->setBackgroundColor(new ImagickPixel('transparent'));
157153
$appIconFile->readImageBlob($svg);
@@ -166,22 +162,21 @@ public function renderAppIcon($app, $size) {
166162
) {
167163
$appIconFile->negateImage(false);
168164
}
169-
$appIconFile->scaleImage(512, 512, true);
170165
} else {
171166
$appIconFile = new Imagick();
172167
$appIconFile->setBackgroundColor(new ImagickPixel('transparent'));
173168
$appIconFile->readImageBlob($appIconContent);
174-
$appIconFile->scaleImage(512, 512, true);
175169
}
176170
// offset for icon positioning
177-
$border_w = (int)($appIconFile->getImageWidth() * 0.05);
178-
$border_h = (int)($appIconFile->getImageHeight() * 0.05);
171+
$padding = 0.15;
172+
$border_w = (int)($appIconFile->getImageWidth() * $padding);
173+
$border_h = (int)($appIconFile->getImageHeight() * $padding);
179174
$innerWidth = ($appIconFile->getImageWidth() - $border_w * 2);
180175
$innerHeight = ($appIconFile->getImageHeight() - $border_h * 2);
181176
$appIconFile->adaptiveResizeImage($innerWidth, $innerHeight);
182177
// center icon
183-
$offset_w = (int)(512 / 2 - $innerWidth / 2);
184-
$offset_h = (int)(512 / 2 - $innerHeight / 2);
178+
$offset_w = (int)($size / 2 - $innerWidth / 2);
179+
$offset_h = (int)($size / 2 - $innerHeight / 2);
185180

186181
$finalIconFile = new Imagick();
187182
$finalIconFile->setBackgroundColor(new ImagickPixel('transparent'));

0 commit comments

Comments
 (0)