Completed
Pull Request — master (#6460)
by Julius
54:28 queued 28:18
created
apps/theming/lib/IconBuilder.php 2 patches
Indentation   +189 added lines, -189 removed lines patch added patch discarded remove patch
@@ -29,194 +29,194 @@
 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
-		try {
57
-			$favicon = new Imagick();
58
-			$favicon->setFormat("ico");
59
-			$icon = $this->renderAppIcon($app, 128);
60
-			if ($icon === false) {
61
-				return false;
62
-			}
63
-			$icon->setImageFormat("png32");
64
-
65
-			$clone = $icon->clone();
66
-			$clone->scaleImage(16,0);
67
-			$favicon->addImage($clone);
68
-
69
-			$clone = $icon->clone();
70
-			$clone->scaleImage(32,0);
71
-			$favicon->addImage($clone);
72
-
73
-			$clone = $icon->clone();
74
-			$clone->scaleImage(64,0);
75
-			$favicon->addImage($clone);
76
-
77
-			$clone = $icon->clone();
78
-			$clone->scaleImage(128,0);
79
-			$favicon->addImage($clone);
80
-
81
-			$data = $favicon->getImagesBlob();
82
-			$favicon->destroy();
83
-			$icon->destroy();
84
-			$clone->destroy();
85
-			return $data;
86
-		} catch (\ImagickException $e) {
87
-			return false;
88
-		}
89
-	}
90
-
91
-	/**
92
-	 * @param $app string app name
93
-	 * @return string|false image blob
94
-	 */
95
-	public function getTouchIcon($app) {
96
-		try {
97
-			$icon = $this->renderAppIcon($app, 512);
98
-			if ($icon === false) {
99
-				return false;
100
-			}
101
-			$icon->setImageFormat("png24");
102
-			$data = $icon->getImageBlob();
103
-			$icon->destroy();
104
-			return $data;
105
-		} catch (\ImagickException $e) {
106
-			return false;
107
-		}
108
-	}
109
-
110
-	/**
111
-	 * Render app icon on themed background color
112
-	 * fallback to logo
113
-	 *
114
-	 * @param $app string app name
115
-	 * @param $size int size of the icon in px
116
-	 * @return Imagick|false
117
-	 */
118
-	public function renderAppIcon($app, $size) {
119
-		$appIcon = $this->util->getAppIcon($app);
120
-		if($appIcon === false) {
121
-			return false;
122
-		}
123
-		if ($appIcon instanceof ISimpleFile) {
124
-			$appIconContent = $appIcon->getContent();
125
-			$mime = $appIcon->getMimeType();
126
-		} else {
127
-			$appIconContent = file_get_contents($appIcon);
128
-			$mime = mime_content_type($appIcon);
129
-		}
130
-
131
-		if($appIconContent === false || $appIconContent === "") {
132
-			return false;
133
-		}
134
-
135
-		$color = $this->themingDefaults->getColorPrimary();
136
-
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 . ';" />' .
141
-			'</svg>';
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") {
145
-				$svg = "<?xml version=\"1.0\"?>".$appIconContent;
146
-			} else {
147
-				$svg = $appIconContent;
148
-			}
149
-			$tmp = new Imagick();
150
-			$tmp->readImageBlob($svg);
151
-			$x = $tmp->getImageWidth();
152
-			$y = $tmp->getImageHeight();
153
-			$res = $tmp->getImageResolution();
154
-			$tmp->destroy();
155
-
156
-			if($x>$y) {
157
-				$max = $x;
158
-			} else {
159
-				$max = $y;
160
-			}
161
-
162
-			// convert svg to resized image
163
-			$appIconFile = new Imagick();
164
-			$resX = (int)(512 * $res['x'] / $max * 2.53);
165
-			$resY = (int)(512 * $res['y'] / $max * 2.53);
166
-			$appIconFile->setResolution($resX, $resY);
167
-			$appIconFile->setBackgroundColor(new ImagickPixel('transparent'));
168
-			$appIconFile->readImageBlob($svg);
169
-			$appIconFile->scaleImage(512, 512, true);
170
-		} else {
171
-			$appIconFile = new Imagick();
172
-			$appIconFile->setBackgroundColor(new ImagickPixel('transparent'));
173
-			$appIconFile->readImageBlob($appIconContent);
174
-			$appIconFile->scaleImage(512, 512, true);
175
-		}
176
-		// offset for icon positioning
177
-		$border_w = (int)($appIconFile->getImageWidth() * 0.05);
178
-		$border_h = (int)($appIconFile->getImageHeight() * 0.05);
179
-		$innerWidth = (int)($appIconFile->getImageWidth() - $border_w * 2);
180
-		$innerHeight = (int)($appIconFile->getImageHeight() - $border_h * 2);
181
-		$appIconFile->adaptiveResizeImage($innerWidth, $innerHeight);
182
-		// center icon
183
-		$offset_w = 512 / 2 - $innerWidth / 2;
184
-		$offset_h = 512 / 2 - $innerHeight / 2;
185
-
186
-		$appIconFile->setImageFormat("png24");
187
-
188
-		$finalIconFile = new Imagick();
189
-		$finalIconFile->setBackgroundColor(new ImagickPixel('transparent'));
190
-		$finalIconFile->readImageBlob($background);
191
-		$finalIconFile->setImageVirtualPixelMethod(Imagick::VIRTUALPIXELMETHOD_TRANSPARENT);
192
-		$finalIconFile->setImageArtifact('compose:args', "1,0,-0.5,0.5");
193
-		$finalIconFile->compositeImage($appIconFile, Imagick::COMPOSITE_ATOP, $offset_w, $offset_h);
194
-		$finalIconFile->setImageFormat('png24');
195
-		if (defined("Imagick::INTERPOLATE_BICUBIC") === true) {
196
-			$filter = Imagick::INTERPOLATE_BICUBIC;
197
-		} else {
198
-			$filter = Imagick::FILTER_LANCZOS;
199
-		}
200
-		$finalIconFile->resizeImage($size, $size, $filter, 1, false);
201
-
202
-		$appIconFile->destroy();
203
-		return $finalIconFile;
204
-	}
205
-
206
-	public function colorSvg($app, $image) {
207
-		try {
208
-			$imageFile = $this->util->getAppImage($app, $image);
209
-		} catch (AppPathNotFoundException $e) {
210
-			return false;
211
-		}
212
-		$svg = file_get_contents($imageFile);
213
-		if ($svg !== false && $svg !== "") {
214
-			$color = $this->util->elementColor($this->themingDefaults->getColorPrimary());
215
-			$svg = $this->util->colorizeSvg($svg, $color);
216
-			return $svg;
217
-		} else {
218
-			return false;
219
-		}
220
-	}
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
+        try {
57
+            $favicon = new Imagick();
58
+            $favicon->setFormat("ico");
59
+            $icon = $this->renderAppIcon($app, 128);
60
+            if ($icon === false) {
61
+                return false;
62
+            }
63
+            $icon->setImageFormat("png32");
64
+
65
+            $clone = $icon->clone();
66
+            $clone->scaleImage(16,0);
67
+            $favicon->addImage($clone);
68
+
69
+            $clone = $icon->clone();
70
+            $clone->scaleImage(32,0);
71
+            $favicon->addImage($clone);
72
+
73
+            $clone = $icon->clone();
74
+            $clone->scaleImage(64,0);
75
+            $favicon->addImage($clone);
76
+
77
+            $clone = $icon->clone();
78
+            $clone->scaleImage(128,0);
79
+            $favicon->addImage($clone);
80
+
81
+            $data = $favicon->getImagesBlob();
82
+            $favicon->destroy();
83
+            $icon->destroy();
84
+            $clone->destroy();
85
+            return $data;
86
+        } catch (\ImagickException $e) {
87
+            return false;
88
+        }
89
+    }
90
+
91
+    /**
92
+     * @param $app string app name
93
+     * @return string|false image blob
94
+     */
95
+    public function getTouchIcon($app) {
96
+        try {
97
+            $icon = $this->renderAppIcon($app, 512);
98
+            if ($icon === false) {
99
+                return false;
100
+            }
101
+            $icon->setImageFormat("png24");
102
+            $data = $icon->getImageBlob();
103
+            $icon->destroy();
104
+            return $data;
105
+        } catch (\ImagickException $e) {
106
+            return false;
107
+        }
108
+    }
109
+
110
+    /**
111
+     * Render app icon on themed background color
112
+     * fallback to logo
113
+     *
114
+     * @param $app string app name
115
+     * @param $size int size of the icon in px
116
+     * @return Imagick|false
117
+     */
118
+    public function renderAppIcon($app, $size) {
119
+        $appIcon = $this->util->getAppIcon($app);
120
+        if($appIcon === false) {
121
+            return false;
122
+        }
123
+        if ($appIcon instanceof ISimpleFile) {
124
+            $appIconContent = $appIcon->getContent();
125
+            $mime = $appIcon->getMimeType();
126
+        } else {
127
+            $appIconContent = file_get_contents($appIcon);
128
+            $mime = mime_content_type($appIcon);
129
+        }
130
+
131
+        if($appIconContent === false || $appIconContent === "") {
132
+            return false;
133
+        }
134
+
135
+        $color = $this->themingDefaults->getColorPrimary();
136
+
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 . ';" />' .
141
+            '</svg>';
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") {
145
+                $svg = "<?xml version=\"1.0\"?>".$appIconContent;
146
+            } else {
147
+                $svg = $appIconContent;
148
+            }
149
+            $tmp = new Imagick();
150
+            $tmp->readImageBlob($svg);
151
+            $x = $tmp->getImageWidth();
152
+            $y = $tmp->getImageHeight();
153
+            $res = $tmp->getImageResolution();
154
+            $tmp->destroy();
155
+
156
+            if($x>$y) {
157
+                $max = $x;
158
+            } else {
159
+                $max = $y;
160
+            }
161
+
162
+            // convert svg to resized image
163
+            $appIconFile = new Imagick();
164
+            $resX = (int)(512 * $res['x'] / $max * 2.53);
165
+            $resY = (int)(512 * $res['y'] / $max * 2.53);
166
+            $appIconFile->setResolution($resX, $resY);
167
+            $appIconFile->setBackgroundColor(new ImagickPixel('transparent'));
168
+            $appIconFile->readImageBlob($svg);
169
+            $appIconFile->scaleImage(512, 512, true);
170
+        } else {
171
+            $appIconFile = new Imagick();
172
+            $appIconFile->setBackgroundColor(new ImagickPixel('transparent'));
173
+            $appIconFile->readImageBlob($appIconContent);
174
+            $appIconFile->scaleImage(512, 512, true);
175
+        }
176
+        // offset for icon positioning
177
+        $border_w = (int)($appIconFile->getImageWidth() * 0.05);
178
+        $border_h = (int)($appIconFile->getImageHeight() * 0.05);
179
+        $innerWidth = (int)($appIconFile->getImageWidth() - $border_w * 2);
180
+        $innerHeight = (int)($appIconFile->getImageHeight() - $border_h * 2);
181
+        $appIconFile->adaptiveResizeImage($innerWidth, $innerHeight);
182
+        // center icon
183
+        $offset_w = 512 / 2 - $innerWidth / 2;
184
+        $offset_h = 512 / 2 - $innerHeight / 2;
185
+
186
+        $appIconFile->setImageFormat("png24");
187
+
188
+        $finalIconFile = new Imagick();
189
+        $finalIconFile->setBackgroundColor(new ImagickPixel('transparent'));
190
+        $finalIconFile->readImageBlob($background);
191
+        $finalIconFile->setImageVirtualPixelMethod(Imagick::VIRTUALPIXELMETHOD_TRANSPARENT);
192
+        $finalIconFile->setImageArtifact('compose:args', "1,0,-0.5,0.5");
193
+        $finalIconFile->compositeImage($appIconFile, Imagick::COMPOSITE_ATOP, $offset_w, $offset_h);
194
+        $finalIconFile->setImageFormat('png24');
195
+        if (defined("Imagick::INTERPOLATE_BICUBIC") === true) {
196
+            $filter = Imagick::INTERPOLATE_BICUBIC;
197
+        } else {
198
+            $filter = Imagick::FILTER_LANCZOS;
199
+        }
200
+        $finalIconFile->resizeImage($size, $size, $filter, 1, false);
201
+
202
+        $appIconFile->destroy();
203
+        return $finalIconFile;
204
+    }
205
+
206
+    public function colorSvg($app, $image) {
207
+        try {
208
+            $imageFile = $this->util->getAppImage($app, $image);
209
+        } catch (AppPathNotFoundException $e) {
210
+            return false;
211
+        }
212
+        $svg = file_get_contents($imageFile);
213
+        if ($svg !== false && $svg !== "") {
214
+            $color = $this->util->elementColor($this->themingDefaults->getColorPrimary());
215
+            $svg = $this->util->colorizeSvg($svg, $color);
216
+            return $svg;
217
+        } else {
218
+            return false;
219
+        }
220
+    }
221 221
 
222 222
 }
Please login to merge, or discard this patch.
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 = $icon->clone();
66
-			$clone->scaleImage(16,0);
66
+			$clone->scaleImage(16, 0);
67 67
 			$favicon->addImage($clone);
68 68
 
69 69
 			$clone = $icon->clone();
70
-			$clone->scaleImage(32,0);
70
+			$clone->scaleImage(32, 0);
71 71
 			$favicon->addImage($clone);
72 72
 
73 73
 			$clone = $icon->clone();
74
-			$clone->scaleImage(64,0);
74
+			$clone->scaleImage(64, 0);
75 75
 			$favicon->addImage($clone);
76 76
 
77 77
 			$clone = $icon->clone();
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);
@@ -174,10 +174,10 @@  discard block
 block discarded – undo
174 174
 			$appIconFile->scaleImage(512, 512, true);
175 175
 		}
176 176
 		// offset for icon positioning
177
-		$border_w = (int)($appIconFile->getImageWidth() * 0.05);
178
-		$border_h = (int)($appIconFile->getImageHeight() * 0.05);
179
-		$innerWidth = (int)($appIconFile->getImageWidth() - $border_w * 2);
180
-		$innerHeight = (int)($appIconFile->getImageHeight() - $border_h * 2);
177
+		$border_w = (int) ($appIconFile->getImageWidth() * 0.05);
178
+		$border_h = (int) ($appIconFile->getImageHeight() * 0.05);
179
+		$innerWidth = (int) ($appIconFile->getImageWidth() - $border_w * 2);
180
+		$innerHeight = (int) ($appIconFile->getImageHeight() - $border_h * 2);
181 181
 		$appIconFile->adaptiveResizeImage($innerWidth, $innerHeight);
182 182
 		// center icon
183 183
 		$offset_w = 512 / 2 - $innerWidth / 2;
Please login to merge, or discard this patch.