-
-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Expand file tree
/
Copy pathIconBuilder.php
More file actions
233 lines (214 loc) · 6.51 KB
/
IconBuilder.php
File metadata and controls
233 lines (214 loc) · 6.51 KB
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
<?php
/**
* SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OCA\Theming;
use Imagick;
use ImagickDraw;
use ImagickPixel;
use OCP\Files\SimpleFS\ISimpleFile;
class IconBuilder {
/**
* IconBuilder constructor.
*
* @param ThemingDefaults $themingDefaults
* @param Util $util
* @param ImageManager $imageManager
*/
public function __construct(
private ThemingDefaults $themingDefaults,
private Util $util,
private ImageManager $imageManager,
) {
}
/**
* @param $app string app name
* @return string|false image blob
*/
public function getFavicon($app) {
if (!$this->imageManager->canConvert('PNG')) {
return false;
}
try {
$icon = $this->renderAppIcon($app, 128);
if ($icon === false) {
return false;
}
$icon->setImageFormat('PNG32');
$favicon = new Imagick();
$favicon->setFormat('ICO');
$clone = clone $icon;
$clone->scaleImage(16, 0);
$favicon->addImage($clone);
$clone = clone $icon;
$clone->scaleImage(32, 0);
$favicon->addImage($clone);
$clone = clone $icon;
$clone->scaleImage(64, 0);
$favicon->addImage($clone);
$clone = clone $icon;
$clone->scaleImage(128, 0);
$favicon->addImage($clone);
$data = $favicon->getImagesBlob();
$favicon->destroy();
$icon->destroy();
$clone->destroy();
return $data;
} catch (\ImagickException $e) {
return false;
}
}
/**
* @param $app string app name
* @return string|false image blob
*/
public function getTouchIcon($app) {
try {
$icon = $this->renderAppIcon($app, 512);
if ($icon === false) {
return false;
}
$icon->setImageFormat('png32');
$data = $icon->getImageBlob();
$icon->destroy();
return $data;
} catch (\ImagickException $e) {
return false;
}
}
/**
* Render app icon on themed background color
* fallback to logo
*
* @param string $app app name
* @param int $size size of the icon in px
* @return Imagick|false
*/
public function renderAppIcon($app, $size) {
$supportSvg = $this->imageManager->canConvert('SVG');
// retrieve app icon
$appIcon = $this->util->getAppIcon($app, $supportSvg);
if ($appIcon instanceof ISimpleFile) {
$appIconContent = $appIcon->getContent();
$mime = $appIcon->getMimeType();
} elseif (!file_exists($appIcon)) {
return false;
} else {
$appIconContent = file_get_contents($appIcon);
$mime = mime_content_type($appIcon);
}
if ($appIconContent === false || $appIconContent === '') {
return false;
}
$appIconIsSvg = ($mime === 'image/svg+xml' || str_starts_with($appIconContent, '<svg') || str_starts_with($appIconContent, '<?xml'));
// if source image is svg but svg not supported, abort.
// source images are both user and developer set, and there is guarantees that mime and extension match actual contents type
if ($appIconIsSvg && !$supportSvg) {
return false;
}
// construct original image object
try {
$appIconFile = new Imagick();
$appIconFile->setBackgroundColor(new ImagickPixel('transparent'));
if ($appIconIsSvg) {
// handle SVG images
// ensure proper XML declaration
if (!str_starts_with($appIconContent, '<?xml')) {
$svg = '<?xml version="1.0"?>' . $appIconContent;
} else {
$svg = $appIconContent;
}
// get dimensions for resolution calculation
$tmp = new Imagick();
$tmp->setBackgroundColor(new ImagickPixel('transparent'));
$tmp->setResolution(72, 72);
$tmp->readImageBlob($svg);
$x = $tmp->getImageWidth();
$y = $tmp->getImageHeight();
$tmp->destroy();
// set resolution for proper scaling
$resX = (int)(72 * $size / $x);
$resY = (int)(72 * $size / $y);
$appIconFile->setResolution($resX, $resY);
$appIconFile->readImageBlob($svg);
} else {
// handle non-SVG images
$appIconFile->readImageBlob($appIconContent);
}
} catch (\ImagickException $e) {
return false;
}
// calculate final image size and position
$padding = 0.85;
$original_w = $appIconFile->getImageWidth();
$original_h = $appIconFile->getImageHeight();
$contentSize = (int)floor($size * $padding);
$scale = min($contentSize / $original_w, $contentSize / $original_h);
$new_w = max(1, (int)floor($original_w * $scale));
$new_h = max(1, (int)floor($original_h * $scale));
$offset_w = (int)floor(($size - $new_w) / 2);
$offset_h = (int)floor(($size - $new_h) / 2);
$cornerRadius = 0.2 * $size;
$color = $this->themingDefaults->getColorPrimary();
// resize original image
$appIconFile->resizeImage($new_w, $new_h, Imagick::FILTER_LANCZOS, 1);
/**
* invert app icons for bright primary colors
* the default nextcloud logo will not be inverted to black
*/
if ($this->util->isBrightColor($color)
&& !$appIcon instanceof ISimpleFile
&& $app !== 'core'
) {
$appIconFile->negateImage(false);
}
// construct final image object
try {
// image background
$finalIconFile = new Imagick();
$finalIconFile->setBackgroundColor(new ImagickPixel('transparent'));
// icon background
$finalIconFile->newImage($size, $size, new ImagickPixel('transparent'));
$draw = new ImagickDraw();
$draw->setFillColor($color);
$draw->roundRectangle(0, 0, $size - 1, $size - 1, $cornerRadius, $cornerRadius);
$finalIconFile->drawImage($draw);
$draw->destroy();
// overlay icon
$finalIconFile->setImageVirtualPixelMethod(Imagick::VIRTUALPIXELMETHOD_TRANSPARENT);
$finalIconFile->setImageArtifact('compose:args', '1,0,-0.5,0.5');
$finalIconFile->compositeImage($appIconFile, Imagick::COMPOSITE_ATOP, $offset_w, $offset_h);
$finalIconFile->setImageFormat('PNG32');
if (defined('Imagick::INTERPOLATE_BICUBIC') === true) {
$filter = Imagick::INTERPOLATE_BICUBIC;
} else {
$filter = Imagick::FILTER_LANCZOS;
}
$finalIconFile->resizeImage($size, $size, $filter, 1, false);
return $finalIconFile;
} finally {
unset($appIconFile);
}
return false;
}
/**
* @param string $app app name
* @param string $image relative path to svg file in app directory
* @return string|false content of a colorized svg file
*/
public function colorSvg($app, $image) {
$imageFile = $this->util->getAppImage($app, $image);
if ($imageFile === false || $imageFile === '' || !file_exists($imageFile)) {
return false;
}
$svg = file_get_contents($imageFile);
if ($svg !== false && $svg !== '') {
$color = $this->util->elementColor($this->themingDefaults->getColorPrimary());
$svg = $this->util->colorizeSvg($svg, $color);
return $svg;
} else {
return false;
}
}
}