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