@@ -31,214 +31,214 @@ |
||
31 | 31 | use OCP\Files\SimpleFS\ISimpleFile; |
32 | 32 | |
33 | 33 | class IconBuilder { |
34 | - /** @var ThemingDefaults */ |
|
35 | - private $themingDefaults; |
|
36 | - /** @var Util */ |
|
37 | - private $util; |
|
38 | - /** @var ImageManager */ |
|
39 | - private $imageManager; |
|
40 | - |
|
41 | - /** |
|
42 | - * IconBuilder constructor. |
|
43 | - * |
|
44 | - * @param ThemingDefaults $themingDefaults |
|
45 | - * @param Util $util |
|
46 | - * @param ImageManager $imageManager |
|
47 | - */ |
|
48 | - public function __construct( |
|
49 | - ThemingDefaults $themingDefaults, |
|
50 | - Util $util, |
|
51 | - ImageManager $imageManager |
|
52 | - ) { |
|
53 | - $this->themingDefaults = $themingDefaults; |
|
54 | - $this->util = $util; |
|
55 | - $this->imageManager = $imageManager; |
|
56 | - } |
|
57 | - |
|
58 | - /** |
|
59 | - * @param $app string app name |
|
60 | - * @return string|false image blob |
|
61 | - */ |
|
62 | - public function getFavicon($app) { |
|
63 | - if (!$this->imageManager->shouldReplaceIcons()) { |
|
64 | - return false; |
|
65 | - } |
|
66 | - try { |
|
67 | - $favicon = new Imagick(); |
|
68 | - $favicon->setFormat("ico"); |
|
69 | - $icon = $this->renderAppIcon($app, 128); |
|
70 | - if ($icon === false) { |
|
71 | - return false; |
|
72 | - } |
|
73 | - $icon->setImageFormat("png32"); |
|
74 | - |
|
75 | - $clone = clone $icon; |
|
76 | - $clone->scaleImage(16,0); |
|
77 | - $favicon->addImage($clone); |
|
78 | - |
|
79 | - $clone = clone $icon; |
|
80 | - $clone->scaleImage(32,0); |
|
81 | - $favicon->addImage($clone); |
|
82 | - |
|
83 | - $clone = clone $icon; |
|
84 | - $clone->scaleImage(64,0); |
|
85 | - $favicon->addImage($clone); |
|
86 | - |
|
87 | - $clone = clone $icon; |
|
88 | - $clone->scaleImage(128,0); |
|
89 | - $favicon->addImage($clone); |
|
90 | - |
|
91 | - $data = $favicon->getImagesBlob(); |
|
92 | - $favicon->destroy(); |
|
93 | - $icon->destroy(); |
|
94 | - $clone->destroy(); |
|
95 | - return $data; |
|
96 | - } catch (\ImagickException $e) { |
|
97 | - return false; |
|
98 | - } |
|
99 | - } |
|
100 | - |
|
101 | - /** |
|
102 | - * @param $app string app name |
|
103 | - * @return string|false image blob |
|
104 | - */ |
|
105 | - public function getTouchIcon($app) { |
|
106 | - try { |
|
107 | - $icon = $this->renderAppIcon($app, 512); |
|
108 | - if ($icon === false) { |
|
109 | - return false; |
|
110 | - } |
|
111 | - $icon->setImageFormat("png32"); |
|
112 | - $data = $icon->getImageBlob(); |
|
113 | - $icon->destroy(); |
|
114 | - return $data; |
|
115 | - } catch (\ImagickException $e) { |
|
116 | - return false; |
|
117 | - } |
|
118 | - } |
|
119 | - |
|
120 | - /** |
|
121 | - * Render app icon on themed background color |
|
122 | - * fallback to logo |
|
123 | - * |
|
124 | - * @param $app string app name |
|
125 | - * @param $size int size of the icon in px |
|
126 | - * @return Imagick|false |
|
127 | - */ |
|
128 | - public function renderAppIcon($app, $size) { |
|
129 | - $appIcon = $this->util->getAppIcon($app); |
|
130 | - if ($appIcon === false) { |
|
131 | - return false; |
|
132 | - } |
|
133 | - if ($appIcon instanceof ISimpleFile) { |
|
134 | - $appIconContent = $appIcon->getContent(); |
|
135 | - $mime = $appIcon->getMimeType(); |
|
136 | - } else { |
|
137 | - $appIconContent = file_get_contents($appIcon); |
|
138 | - $mime = mime_content_type($appIcon); |
|
139 | - } |
|
140 | - |
|
141 | - if ($appIconContent === false || $appIconContent === "") { |
|
142 | - return false; |
|
143 | - } |
|
144 | - |
|
145 | - $color = $this->themingDefaults->getColorPrimary(); |
|
146 | - |
|
147 | - // generate background image with rounded corners |
|
148 | - $background = '<?xml version="1.0" encoding="UTF-8"?>' . |
|
149 | - '<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">' . |
|
150 | - '<rect x="0" y="0" rx="100" ry="100" width="512" height="512" style="fill:' . $color . ';" />' . |
|
151 | - '</svg>'; |
|
152 | - // resize svg magic as this seems broken in Imagemagick |
|
153 | - if ($mime === "image/svg+xml" || substr($appIconContent, 0, 4) === "<svg") { |
|
154 | - if (substr($appIconContent, 0, 5) !== "<?xml") { |
|
155 | - $svg = "<?xml version=\"1.0\"?>".$appIconContent; |
|
156 | - } else { |
|
157 | - $svg = $appIconContent; |
|
158 | - } |
|
159 | - $tmp = new Imagick(); |
|
160 | - $tmp->readImageBlob($svg); |
|
161 | - $x = $tmp->getImageWidth(); |
|
162 | - $y = $tmp->getImageHeight(); |
|
163 | - $res = $tmp->getImageResolution(); |
|
164 | - $tmp->destroy(); |
|
165 | - |
|
166 | - if ($x > $y) { |
|
167 | - $max = $x; |
|
168 | - } else { |
|
169 | - $max = $y; |
|
170 | - } |
|
171 | - |
|
172 | - // convert svg to resized image |
|
173 | - $appIconFile = new Imagick(); |
|
174 | - $resX = (int)(512 * $res['x'] / $max * 2.53); |
|
175 | - $resY = (int)(512 * $res['y'] / $max * 2.53); |
|
176 | - $appIconFile->setResolution($resX, $resY); |
|
177 | - $appIconFile->setBackgroundColor(new ImagickPixel('transparent')); |
|
178 | - $appIconFile->readImageBlob($svg); |
|
179 | - |
|
180 | - /** |
|
181 | - * invert app icons for bright primary colors |
|
182 | - * the default nextcloud logo will not be inverted to black |
|
183 | - */ |
|
184 | - if ($this->util->invertTextColor($color) |
|
185 | - && !$appIcon instanceof ISimpleFile |
|
186 | - && $app !== "core" |
|
187 | - ) { |
|
188 | - $appIconFile->negateImage(false); |
|
189 | - } |
|
190 | - $appIconFile->scaleImage(512, 512, true); |
|
191 | - } else { |
|
192 | - $appIconFile = new Imagick(); |
|
193 | - $appIconFile->setBackgroundColor(new ImagickPixel('transparent')); |
|
194 | - $appIconFile->readImageBlob($appIconContent); |
|
195 | - $appIconFile->scaleImage(512, 512, true); |
|
196 | - } |
|
197 | - // offset for icon positioning |
|
198 | - $border_w = (int)($appIconFile->getImageWidth() * 0.05); |
|
199 | - $border_h = (int)($appIconFile->getImageHeight() * 0.05); |
|
200 | - $innerWidth = ($appIconFile->getImageWidth() - $border_w * 2); |
|
201 | - $innerHeight = ($appIconFile->getImageHeight() - $border_h * 2); |
|
202 | - $appIconFile->adaptiveResizeImage($innerWidth, $innerHeight); |
|
203 | - // center icon |
|
204 | - $offset_w = (int)(512 / 2 - $innerWidth / 2); |
|
205 | - $offset_h = (int)(512 / 2 - $innerHeight / 2); |
|
206 | - |
|
207 | - $finalIconFile = new Imagick(); |
|
208 | - $finalIconFile->setBackgroundColor(new ImagickPixel('transparent')); |
|
209 | - $finalIconFile->readImageBlob($background); |
|
210 | - $finalIconFile->setImageVirtualPixelMethod(Imagick::VIRTUALPIXELMETHOD_TRANSPARENT); |
|
211 | - $finalIconFile->setImageArtifact('compose:args', "1,0,-0.5,0.5"); |
|
212 | - $finalIconFile->compositeImage($appIconFile, Imagick::COMPOSITE_ATOP, $offset_w, $offset_h); |
|
213 | - $finalIconFile->setImageFormat('png24'); |
|
214 | - if (defined("Imagick::INTERPOLATE_BICUBIC") === true) { |
|
215 | - $filter = Imagick::INTERPOLATE_BICUBIC; |
|
216 | - } else { |
|
217 | - $filter = Imagick::FILTER_LANCZOS; |
|
218 | - } |
|
219 | - $finalIconFile->resizeImage($size, $size, $filter, 1, false); |
|
220 | - |
|
221 | - $appIconFile->destroy(); |
|
222 | - return $finalIconFile; |
|
223 | - } |
|
224 | - |
|
225 | - /** |
|
226 | - * @param $app string app name |
|
227 | - * @param $image string relative path to svg file in app directory |
|
228 | - * @return string|false content of a colorized svg file |
|
229 | - */ |
|
230 | - public function colorSvg($app, $image) { |
|
231 | - $imageFile = $this->util->getAppImage($app, $image); |
|
232 | - if ($imageFile === false || $imageFile === "") { |
|
233 | - return false; |
|
234 | - } |
|
235 | - $svg = file_get_contents($imageFile); |
|
236 | - if ($svg !== false && $svg !== "") { |
|
237 | - $color = $this->util->elementColor($this->themingDefaults->getColorPrimary()); |
|
238 | - $svg = $this->util->colorizeSvg($svg, $color); |
|
239 | - return $svg; |
|
240 | - } else { |
|
241 | - return false; |
|
242 | - } |
|
243 | - } |
|
34 | + /** @var ThemingDefaults */ |
|
35 | + private $themingDefaults; |
|
36 | + /** @var Util */ |
|
37 | + private $util; |
|
38 | + /** @var ImageManager */ |
|
39 | + private $imageManager; |
|
40 | + |
|
41 | + /** |
|
42 | + * IconBuilder constructor. |
|
43 | + * |
|
44 | + * @param ThemingDefaults $themingDefaults |
|
45 | + * @param Util $util |
|
46 | + * @param ImageManager $imageManager |
|
47 | + */ |
|
48 | + public function __construct( |
|
49 | + ThemingDefaults $themingDefaults, |
|
50 | + Util $util, |
|
51 | + ImageManager $imageManager |
|
52 | + ) { |
|
53 | + $this->themingDefaults = $themingDefaults; |
|
54 | + $this->util = $util; |
|
55 | + $this->imageManager = $imageManager; |
|
56 | + } |
|
57 | + |
|
58 | + /** |
|
59 | + * @param $app string app name |
|
60 | + * @return string|false image blob |
|
61 | + */ |
|
62 | + public function getFavicon($app) { |
|
63 | + if (!$this->imageManager->shouldReplaceIcons()) { |
|
64 | + return false; |
|
65 | + } |
|
66 | + try { |
|
67 | + $favicon = new Imagick(); |
|
68 | + $favicon->setFormat("ico"); |
|
69 | + $icon = $this->renderAppIcon($app, 128); |
|
70 | + if ($icon === false) { |
|
71 | + return false; |
|
72 | + } |
|
73 | + $icon->setImageFormat("png32"); |
|
74 | + |
|
75 | + $clone = clone $icon; |
|
76 | + $clone->scaleImage(16,0); |
|
77 | + $favicon->addImage($clone); |
|
78 | + |
|
79 | + $clone = clone $icon; |
|
80 | + $clone->scaleImage(32,0); |
|
81 | + $favicon->addImage($clone); |
|
82 | + |
|
83 | + $clone = clone $icon; |
|
84 | + $clone->scaleImage(64,0); |
|
85 | + $favicon->addImage($clone); |
|
86 | + |
|
87 | + $clone = clone $icon; |
|
88 | + $clone->scaleImage(128,0); |
|
89 | + $favicon->addImage($clone); |
|
90 | + |
|
91 | + $data = $favicon->getImagesBlob(); |
|
92 | + $favicon->destroy(); |
|
93 | + $icon->destroy(); |
|
94 | + $clone->destroy(); |
|
95 | + return $data; |
|
96 | + } catch (\ImagickException $e) { |
|
97 | + return false; |
|
98 | + } |
|
99 | + } |
|
100 | + |
|
101 | + /** |
|
102 | + * @param $app string app name |
|
103 | + * @return string|false image blob |
|
104 | + */ |
|
105 | + public function getTouchIcon($app) { |
|
106 | + try { |
|
107 | + $icon = $this->renderAppIcon($app, 512); |
|
108 | + if ($icon === false) { |
|
109 | + return false; |
|
110 | + } |
|
111 | + $icon->setImageFormat("png32"); |
|
112 | + $data = $icon->getImageBlob(); |
|
113 | + $icon->destroy(); |
|
114 | + return $data; |
|
115 | + } catch (\ImagickException $e) { |
|
116 | + return false; |
|
117 | + } |
|
118 | + } |
|
119 | + |
|
120 | + /** |
|
121 | + * Render app icon on themed background color |
|
122 | + * fallback to logo |
|
123 | + * |
|
124 | + * @param $app string app name |
|
125 | + * @param $size int size of the icon in px |
|
126 | + * @return Imagick|false |
|
127 | + */ |
|
128 | + public function renderAppIcon($app, $size) { |
|
129 | + $appIcon = $this->util->getAppIcon($app); |
|
130 | + if ($appIcon === false) { |
|
131 | + return false; |
|
132 | + } |
|
133 | + if ($appIcon instanceof ISimpleFile) { |
|
134 | + $appIconContent = $appIcon->getContent(); |
|
135 | + $mime = $appIcon->getMimeType(); |
|
136 | + } else { |
|
137 | + $appIconContent = file_get_contents($appIcon); |
|
138 | + $mime = mime_content_type($appIcon); |
|
139 | + } |
|
140 | + |
|
141 | + if ($appIconContent === false || $appIconContent === "") { |
|
142 | + return false; |
|
143 | + } |
|
144 | + |
|
145 | + $color = $this->themingDefaults->getColorPrimary(); |
|
146 | + |
|
147 | + // generate background image with rounded corners |
|
148 | + $background = '<?xml version="1.0" encoding="UTF-8"?>' . |
|
149 | + '<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">' . |
|
150 | + '<rect x="0" y="0" rx="100" ry="100" width="512" height="512" style="fill:' . $color . ';" />' . |
|
151 | + '</svg>'; |
|
152 | + // resize svg magic as this seems broken in Imagemagick |
|
153 | + if ($mime === "image/svg+xml" || substr($appIconContent, 0, 4) === "<svg") { |
|
154 | + if (substr($appIconContent, 0, 5) !== "<?xml") { |
|
155 | + $svg = "<?xml version=\"1.0\"?>".$appIconContent; |
|
156 | + } else { |
|
157 | + $svg = $appIconContent; |
|
158 | + } |
|
159 | + $tmp = new Imagick(); |
|
160 | + $tmp->readImageBlob($svg); |
|
161 | + $x = $tmp->getImageWidth(); |
|
162 | + $y = $tmp->getImageHeight(); |
|
163 | + $res = $tmp->getImageResolution(); |
|
164 | + $tmp->destroy(); |
|
165 | + |
|
166 | + if ($x > $y) { |
|
167 | + $max = $x; |
|
168 | + } else { |
|
169 | + $max = $y; |
|
170 | + } |
|
171 | + |
|
172 | + // convert svg to resized image |
|
173 | + $appIconFile = new Imagick(); |
|
174 | + $resX = (int)(512 * $res['x'] / $max * 2.53); |
|
175 | + $resY = (int)(512 * $res['y'] / $max * 2.53); |
|
176 | + $appIconFile->setResolution($resX, $resY); |
|
177 | + $appIconFile->setBackgroundColor(new ImagickPixel('transparent')); |
|
178 | + $appIconFile->readImageBlob($svg); |
|
179 | + |
|
180 | + /** |
|
181 | + * invert app icons for bright primary colors |
|
182 | + * the default nextcloud logo will not be inverted to black |
|
183 | + */ |
|
184 | + if ($this->util->invertTextColor($color) |
|
185 | + && !$appIcon instanceof ISimpleFile |
|
186 | + && $app !== "core" |
|
187 | + ) { |
|
188 | + $appIconFile->negateImage(false); |
|
189 | + } |
|
190 | + $appIconFile->scaleImage(512, 512, true); |
|
191 | + } else { |
|
192 | + $appIconFile = new Imagick(); |
|
193 | + $appIconFile->setBackgroundColor(new ImagickPixel('transparent')); |
|
194 | + $appIconFile->readImageBlob($appIconContent); |
|
195 | + $appIconFile->scaleImage(512, 512, true); |
|
196 | + } |
|
197 | + // offset for icon positioning |
|
198 | + $border_w = (int)($appIconFile->getImageWidth() * 0.05); |
|
199 | + $border_h = (int)($appIconFile->getImageHeight() * 0.05); |
|
200 | + $innerWidth = ($appIconFile->getImageWidth() - $border_w * 2); |
|
201 | + $innerHeight = ($appIconFile->getImageHeight() - $border_h * 2); |
|
202 | + $appIconFile->adaptiveResizeImage($innerWidth, $innerHeight); |
|
203 | + // center icon |
|
204 | + $offset_w = (int)(512 / 2 - $innerWidth / 2); |
|
205 | + $offset_h = (int)(512 / 2 - $innerHeight / 2); |
|
206 | + |
|
207 | + $finalIconFile = new Imagick(); |
|
208 | + $finalIconFile->setBackgroundColor(new ImagickPixel('transparent')); |
|
209 | + $finalIconFile->readImageBlob($background); |
|
210 | + $finalIconFile->setImageVirtualPixelMethod(Imagick::VIRTUALPIXELMETHOD_TRANSPARENT); |
|
211 | + $finalIconFile->setImageArtifact('compose:args', "1,0,-0.5,0.5"); |
|
212 | + $finalIconFile->compositeImage($appIconFile, Imagick::COMPOSITE_ATOP, $offset_w, $offset_h); |
|
213 | + $finalIconFile->setImageFormat('png24'); |
|
214 | + if (defined("Imagick::INTERPOLATE_BICUBIC") === true) { |
|
215 | + $filter = Imagick::INTERPOLATE_BICUBIC; |
|
216 | + } else { |
|
217 | + $filter = Imagick::FILTER_LANCZOS; |
|
218 | + } |
|
219 | + $finalIconFile->resizeImage($size, $size, $filter, 1, false); |
|
220 | + |
|
221 | + $appIconFile->destroy(); |
|
222 | + return $finalIconFile; |
|
223 | + } |
|
224 | + |
|
225 | + /** |
|
226 | + * @param $app string app name |
|
227 | + * @param $image string relative path to svg file in app directory |
|
228 | + * @return string|false content of a colorized svg file |
|
229 | + */ |
|
230 | + public function colorSvg($app, $image) { |
|
231 | + $imageFile = $this->util->getAppImage($app, $image); |
|
232 | + if ($imageFile === false || $imageFile === "") { |
|
233 | + return false; |
|
234 | + } |
|
235 | + $svg = file_get_contents($imageFile); |
|
236 | + if ($svg !== false && $svg !== "") { |
|
237 | + $color = $this->util->elementColor($this->themingDefaults->getColorPrimary()); |
|
238 | + $svg = $this->util->colorizeSvg($svg, $color); |
|
239 | + return $svg; |
|
240 | + } else { |
|
241 | + return false; |
|
242 | + } |
|
243 | + } |
|
244 | 244 | } |
@@ -73,19 +73,19 @@ discard block |
||
73 | 73 | $icon->setImageFormat("png32"); |
74 | 74 | |
75 | 75 | $clone = clone $icon; |
76 | - $clone->scaleImage(16,0); |
|
76 | + $clone->scaleImage(16, 0); |
|
77 | 77 | $favicon->addImage($clone); |
78 | 78 | |
79 | 79 | $clone = clone $icon; |
80 | - $clone->scaleImage(32,0); |
|
80 | + $clone->scaleImage(32, 0); |
|
81 | 81 | $favicon->addImage($clone); |
82 | 82 | |
83 | 83 | $clone = clone $icon; |
84 | - $clone->scaleImage(64,0); |
|
84 | + $clone->scaleImage(64, 0); |
|
85 | 85 | $favicon->addImage($clone); |
86 | 86 | |
87 | 87 | $clone = clone $icon; |
88 | - $clone->scaleImage(128,0); |
|
88 | + $clone->scaleImage(128, 0); |
|
89 | 89 | $favicon->addImage($clone); |
90 | 90 | |
91 | 91 | $data = $favicon->getImagesBlob(); |
@@ -145,9 +145,9 @@ discard block |
||
145 | 145 | $color = $this->themingDefaults->getColorPrimary(); |
146 | 146 | |
147 | 147 | // generate background image with rounded corners |
148 | - $background = '<?xml version="1.0" encoding="UTF-8"?>' . |
|
149 | - '<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">' . |
|
150 | - '<rect x="0" y="0" rx="100" ry="100" width="512" height="512" style="fill:' . $color . ';" />' . |
|
148 | + $background = '<?xml version="1.0" encoding="UTF-8"?>'. |
|
149 | + '<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">'. |
|
150 | + '<rect x="0" y="0" rx="100" ry="100" width="512" height="512" style="fill:'.$color.';" />'. |
|
151 | 151 | '</svg>'; |
152 | 152 | // resize svg magic as this seems broken in Imagemagick |
153 | 153 | if ($mime === "image/svg+xml" || substr($appIconContent, 0, 4) === "<svg") { |
@@ -171,8 +171,8 @@ discard block |
||
171 | 171 | |
172 | 172 | // convert svg to resized image |
173 | 173 | $appIconFile = new Imagick(); |
174 | - $resX = (int)(512 * $res['x'] / $max * 2.53); |
|
175 | - $resY = (int)(512 * $res['y'] / $max * 2.53); |
|
174 | + $resX = (int) (512 * $res['x'] / $max * 2.53); |
|
175 | + $resY = (int) (512 * $res['y'] / $max * 2.53); |
|
176 | 176 | $appIconFile->setResolution($resX, $resY); |
177 | 177 | $appIconFile->setBackgroundColor(new ImagickPixel('transparent')); |
178 | 178 | $appIconFile->readImageBlob($svg); |
@@ -195,14 +195,14 @@ discard block |
||
195 | 195 | $appIconFile->scaleImage(512, 512, true); |
196 | 196 | } |
197 | 197 | // offset for icon positioning |
198 | - $border_w = (int)($appIconFile->getImageWidth() * 0.05); |
|
199 | - $border_h = (int)($appIconFile->getImageHeight() * 0.05); |
|
198 | + $border_w = (int) ($appIconFile->getImageWidth() * 0.05); |
|
199 | + $border_h = (int) ($appIconFile->getImageHeight() * 0.05); |
|
200 | 200 | $innerWidth = ($appIconFile->getImageWidth() - $border_w * 2); |
201 | 201 | $innerHeight = ($appIconFile->getImageHeight() - $border_h * 2); |
202 | 202 | $appIconFile->adaptiveResizeImage($innerWidth, $innerHeight); |
203 | 203 | // center icon |
204 | - $offset_w = (int)(512 / 2 - $innerWidth / 2); |
|
205 | - $offset_h = (int)(512 / 2 - $innerHeight / 2); |
|
204 | + $offset_w = (int) (512 / 2 - $innerWidth / 2); |
|
205 | + $offset_h = (int) (512 / 2 - $innerHeight / 2); |
|
206 | 206 | |
207 | 207 | $finalIconFile = new Imagick(); |
208 | 208 | $finalIconFile->setBackgroundColor(new ImagickPixel('transparent')); |