Completed
Pull Request — master (#4888)
by Julius
15:06
created
apps/theming/lib/Controller/IconController.php 2 patches
Indentation   +145 added lines, -145 removed lines patch added patch discarded remove patch
@@ -38,154 +38,154 @@
 block discarded – undo
38 38
 use OCP\IConfig;
39 39
 
40 40
 class IconController extends Controller {
41
-	/** @var ThemingDefaults */
42
-	private $themingDefaults;
43
-	/** @var Util */
44
-	private $util;
45
-	/** @var ITimeFactory */
46
-	private $timeFactory;
47
-	/** @var IConfig */
48
-	private $config;
49
-	/** @var IconBuilder */
50
-	private $iconBuilder;
51
-	/** @var ImageManager */
52
-	private $imageManager;
53
-	/** @var FileAccessHelper */
54
-	private $fileAccessHelper;
41
+    /** @var ThemingDefaults */
42
+    private $themingDefaults;
43
+    /** @var Util */
44
+    private $util;
45
+    /** @var ITimeFactory */
46
+    private $timeFactory;
47
+    /** @var IConfig */
48
+    private $config;
49
+    /** @var IconBuilder */
50
+    private $iconBuilder;
51
+    /** @var ImageManager */
52
+    private $imageManager;
53
+    /** @var FileAccessHelper */
54
+    private $fileAccessHelper;
55 55
 
56
-	/**
57
-	 * IconController constructor.
58
-	 *
59
-	 * @param string $appName
60
-	 * @param IRequest $request
61
-	 * @param ThemingDefaults $themingDefaults
62
-	 * @param Util $util
63
-	 * @param ITimeFactory $timeFactory
64
-	 * @param IConfig $config
65
-	 * @param IconBuilder $iconBuilder
66
-	 * @param ImageManager $imageManager
67
-	 */
68
-	public function __construct(
69
-		$appName,
70
-		IRequest $request,
71
-		ThemingDefaults $themingDefaults,
72
-		Util $util,
73
-		ITimeFactory $timeFactory,
74
-		IConfig $config,
75
-		IconBuilder $iconBuilder,
76
-		ImageManager $imageManager,
77
-		FileAccessHelper $fileAccessHelper
78
-	) {
79
-		parent::__construct($appName, $request);
56
+    /**
57
+     * IconController constructor.
58
+     *
59
+     * @param string $appName
60
+     * @param IRequest $request
61
+     * @param ThemingDefaults $themingDefaults
62
+     * @param Util $util
63
+     * @param ITimeFactory $timeFactory
64
+     * @param IConfig $config
65
+     * @param IconBuilder $iconBuilder
66
+     * @param ImageManager $imageManager
67
+     */
68
+    public function __construct(
69
+        $appName,
70
+        IRequest $request,
71
+        ThemingDefaults $themingDefaults,
72
+        Util $util,
73
+        ITimeFactory $timeFactory,
74
+        IConfig $config,
75
+        IconBuilder $iconBuilder,
76
+        ImageManager $imageManager,
77
+        FileAccessHelper $fileAccessHelper
78
+    ) {
79
+        parent::__construct($appName, $request);
80 80
 
81
-		$this->themingDefaults = $themingDefaults;
82
-		$this->util = $util;
83
-		$this->timeFactory = $timeFactory;
84
-		$this->config = $config;
85
-		$this->iconBuilder = $iconBuilder;
86
-		$this->imageManager = $imageManager;
87
-		$this->fileAccessHelper = $fileAccessHelper;
88
-	}
81
+        $this->themingDefaults = $themingDefaults;
82
+        $this->util = $util;
83
+        $this->timeFactory = $timeFactory;
84
+        $this->config = $config;
85
+        $this->iconBuilder = $iconBuilder;
86
+        $this->imageManager = $imageManager;
87
+        $this->fileAccessHelper = $fileAccessHelper;
88
+    }
89 89
 
90
-	/**
91
-	 * @PublicPage
92
-	 * @NoCSRFRequired
93
-	 *
94
-	 * @param $app string app name
95
-	 * @param $image string image file name (svg required)
96
-	 * @return FileDisplayResponse|NotFoundResponse
97
-	 */
98
-	public function getThemedIcon($app, $image) {
99
-		try {
100
-			$iconFile = $this->imageManager->getCachedImage("icon-" . $app . '-' . str_replace("/","_",$image));
101
-		} catch (NotFoundException $exception) {
102
-			$icon = $this->iconBuilder->colorSvg($app, $image);
103
-			if ($icon === false || $icon === "") {
104
-				return new NotFoundResponse();
105
-			}
106
-			$iconFile = $this->imageManager->setCachedImage("icon-" . $app . '-' . str_replace("/","_",$image), $icon);
107
-		}
108
-		if ($iconFile !== false) {
109
-			$response = new FileDisplayResponse($iconFile, Http::STATUS_OK, ['Content-Type' => 'image/svg+xml']);
110
-			$response->cacheFor(86400);
111
-			$expires = new \DateTime();
112
-			$expires->setTimestamp($this->timeFactory->getTime());
113
-			$expires->add(new \DateInterval('PT24H'));
114
-			$response->addHeader('Expires', $expires->format(\DateTime::RFC2822));
115
-			$response->addHeader('Pragma', 'cache');
116
-			return $response;
117
-		} else {
118
-			return new NotFoundResponse();
119
-		}
120
-	}
90
+    /**
91
+     * @PublicPage
92
+     * @NoCSRFRequired
93
+     *
94
+     * @param $app string app name
95
+     * @param $image string image file name (svg required)
96
+     * @return FileDisplayResponse|NotFoundResponse
97
+     */
98
+    public function getThemedIcon($app, $image) {
99
+        try {
100
+            $iconFile = $this->imageManager->getCachedImage("icon-" . $app . '-' . str_replace("/","_",$image));
101
+        } catch (NotFoundException $exception) {
102
+            $icon = $this->iconBuilder->colorSvg($app, $image);
103
+            if ($icon === false || $icon === "") {
104
+                return new NotFoundResponse();
105
+            }
106
+            $iconFile = $this->imageManager->setCachedImage("icon-" . $app . '-' . str_replace("/","_",$image), $icon);
107
+        }
108
+        if ($iconFile !== false) {
109
+            $response = new FileDisplayResponse($iconFile, Http::STATUS_OK, ['Content-Type' => 'image/svg+xml']);
110
+            $response->cacheFor(86400);
111
+            $expires = new \DateTime();
112
+            $expires->setTimestamp($this->timeFactory->getTime());
113
+            $expires->add(new \DateInterval('PT24H'));
114
+            $response->addHeader('Expires', $expires->format(\DateTime::RFC2822));
115
+            $response->addHeader('Pragma', 'cache');
116
+            return $response;
117
+        } else {
118
+            return new NotFoundResponse();
119
+        }
120
+    }
121 121
 
122
-	/**
123
-	 * Return a 32x32 favicon as png
124
-	 *
125
-	 * @PublicPage
126
-	 * @NoCSRFRequired
127
-	 *
128
-	 * @param $app string app name
129
-	 * @return FileDisplayResponse|DataDisplayResponse
130
-	 */
131
-	public function getFavicon($app = "core") {
132
-		$response = null;
133
-		if ($this->themingDefaults->shouldReplaceIcons()) {
134
-			try {
135
-				$iconFile = $this->imageManager->getCachedImage('favIcon-' . $app);
136
-			} catch (NotFoundException $exception) {
137
-				$icon = $this->iconBuilder->getFavicon($app);
138
-				$iconFile = $this->imageManager->setCachedImage('favIcon-' . $app, $icon);
139
-			}
140
-			if ($iconFile !== false) {
141
-				$response = new FileDisplayResponse($iconFile, Http::STATUS_OK, ['Content-Type' => 'image/x-icon']);
142
-			}
143
-		}
144
-		if($response === null) {
145
-			$fallbackLogo = \OC::$SERVERROOT . '/core/img/favicon.png';
146
-			$response = new DataDisplayResponse($this->fileAccessHelper->file_get_contents($fallbackLogo), Http::STATUS_OK, ['Content-Type' => 'image/x-icon']);
147
-		}
148
-		$response->cacheFor(86400);
149
-		$expires = new \DateTime();
150
-		$expires->setTimestamp($this->timeFactory->getTime());
151
-		$expires->add(new \DateInterval('PT24H'));
152
-		$response->addHeader('Expires', $expires->format(\DateTime::RFC2822));
153
-		$response->addHeader('Pragma', 'cache');
154
-		return $response;
155
-	}
122
+    /**
123
+     * Return a 32x32 favicon as png
124
+     *
125
+     * @PublicPage
126
+     * @NoCSRFRequired
127
+     *
128
+     * @param $app string app name
129
+     * @return FileDisplayResponse|DataDisplayResponse
130
+     */
131
+    public function getFavicon($app = "core") {
132
+        $response = null;
133
+        if ($this->themingDefaults->shouldReplaceIcons()) {
134
+            try {
135
+                $iconFile = $this->imageManager->getCachedImage('favIcon-' . $app);
136
+            } catch (NotFoundException $exception) {
137
+                $icon = $this->iconBuilder->getFavicon($app);
138
+                $iconFile = $this->imageManager->setCachedImage('favIcon-' . $app, $icon);
139
+            }
140
+            if ($iconFile !== false) {
141
+                $response = new FileDisplayResponse($iconFile, Http::STATUS_OK, ['Content-Type' => 'image/x-icon']);
142
+            }
143
+        }
144
+        if($response === null) {
145
+            $fallbackLogo = \OC::$SERVERROOT . '/core/img/favicon.png';
146
+            $response = new DataDisplayResponse($this->fileAccessHelper->file_get_contents($fallbackLogo), Http::STATUS_OK, ['Content-Type' => 'image/x-icon']);
147
+        }
148
+        $response->cacheFor(86400);
149
+        $expires = new \DateTime();
150
+        $expires->setTimestamp($this->timeFactory->getTime());
151
+        $expires->add(new \DateInterval('PT24H'));
152
+        $response->addHeader('Expires', $expires->format(\DateTime::RFC2822));
153
+        $response->addHeader('Pragma', 'cache');
154
+        return $response;
155
+    }
156 156
 
157
-	/**
158
-	 * Return a 512x512 icon for touch devices
159
-	 *
160
-	 * @PublicPage
161
-	 * @NoCSRFRequired
162
-	 *
163
-	 * @param $app string app name
164
-	 * @return FileDisplayResponse|NotFoundResponse
165
-	 */
166
-	public function getTouchIcon($app = "core") {
167
-		$response = null;
168
-		if ($this->themingDefaults->shouldReplaceIcons()) {
169
-			try {
170
-				$iconFile = $this->imageManager->getCachedImage('touchIcon-' . $app);
171
-			} catch (NotFoundException $exception) {
172
-				$icon = $this->iconBuilder->getTouchIcon($app);
173
-				$iconFile = $this->imageManager->setCachedImage('touchIcon-' . $app, $icon);
174
-			}
175
-			if ($iconFile !== false) {
176
-				$response = new FileDisplayResponse($iconFile, Http::STATUS_OK, ['Content-Type' => 'image/png']);
177
-			}
178
-		}
179
-		if($response === null) {
180
-			$fallbackLogo = \OC::$SERVERROOT . '/core/img/favicon-touch.png';
181
-			$response = new DataDisplayResponse($this->fileAccessHelper->file_get_contents($fallbackLogo), Http::STATUS_OK, ['Content-Type' => 'image/png']);
182
-		}
183
-		$response->cacheFor(86400);
184
-		$expires = new \DateTime();
185
-		$expires->setTimestamp($this->timeFactory->getTime());
186
-		$expires->add(new \DateInterval('PT24H'));
187
-		$response->addHeader('Expires', $expires->format(\DateTime::RFC2822));
188
-		$response->addHeader('Pragma', 'cache');
189
-		return $response;
190
-	}
157
+    /**
158
+     * Return a 512x512 icon for touch devices
159
+     *
160
+     * @PublicPage
161
+     * @NoCSRFRequired
162
+     *
163
+     * @param $app string app name
164
+     * @return FileDisplayResponse|NotFoundResponse
165
+     */
166
+    public function getTouchIcon($app = "core") {
167
+        $response = null;
168
+        if ($this->themingDefaults->shouldReplaceIcons()) {
169
+            try {
170
+                $iconFile = $this->imageManager->getCachedImage('touchIcon-' . $app);
171
+            } catch (NotFoundException $exception) {
172
+                $icon = $this->iconBuilder->getTouchIcon($app);
173
+                $iconFile = $this->imageManager->setCachedImage('touchIcon-' . $app, $icon);
174
+            }
175
+            if ($iconFile !== false) {
176
+                $response = new FileDisplayResponse($iconFile, Http::STATUS_OK, ['Content-Type' => 'image/png']);
177
+            }
178
+        }
179
+        if($response === null) {
180
+            $fallbackLogo = \OC::$SERVERROOT . '/core/img/favicon-touch.png';
181
+            $response = new DataDisplayResponse($this->fileAccessHelper->file_get_contents($fallbackLogo), Http::STATUS_OK, ['Content-Type' => 'image/png']);
182
+        }
183
+        $response->cacheFor(86400);
184
+        $expires = new \DateTime();
185
+        $expires->setTimestamp($this->timeFactory->getTime());
186
+        $expires->add(new \DateInterval('PT24H'));
187
+        $response->addHeader('Expires', $expires->format(\DateTime::RFC2822));
188
+        $response->addHeader('Pragma', 'cache');
189
+        return $response;
190
+    }
191 191
 }
Please login to merge, or discard this patch.
Spacing   +10 added lines, -10 removed lines patch added patch discarded remove patch
@@ -97,13 +97,13 @@  discard block
 block discarded – undo
97 97
 	 */
98 98
 	public function getThemedIcon($app, $image) {
99 99
 		try {
100
-			$iconFile = $this->imageManager->getCachedImage("icon-" . $app . '-' . str_replace("/","_",$image));
100
+			$iconFile = $this->imageManager->getCachedImage("icon-".$app.'-'.str_replace("/", "_", $image));
101 101
 		} catch (NotFoundException $exception) {
102 102
 			$icon = $this->iconBuilder->colorSvg($app, $image);
103 103
 			if ($icon === false || $icon === "") {
104 104
 				return new NotFoundResponse();
105 105
 			}
106
-			$iconFile = $this->imageManager->setCachedImage("icon-" . $app . '-' . str_replace("/","_",$image), $icon);
106
+			$iconFile = $this->imageManager->setCachedImage("icon-".$app.'-'.str_replace("/", "_", $image), $icon);
107 107
 		}
108 108
 		if ($iconFile !== false) {
109 109
 			$response = new FileDisplayResponse($iconFile, Http::STATUS_OK, ['Content-Type' => 'image/svg+xml']);
@@ -132,17 +132,17 @@  discard block
 block discarded – undo
132 132
 		$response = null;
133 133
 		if ($this->themingDefaults->shouldReplaceIcons()) {
134 134
 			try {
135
-				$iconFile = $this->imageManager->getCachedImage('favIcon-' . $app);
135
+				$iconFile = $this->imageManager->getCachedImage('favIcon-'.$app);
136 136
 			} catch (NotFoundException $exception) {
137 137
 				$icon = $this->iconBuilder->getFavicon($app);
138
-				$iconFile = $this->imageManager->setCachedImage('favIcon-' . $app, $icon);
138
+				$iconFile = $this->imageManager->setCachedImage('favIcon-'.$app, $icon);
139 139
 			}
140 140
 			if ($iconFile !== false) {
141 141
 				$response = new FileDisplayResponse($iconFile, Http::STATUS_OK, ['Content-Type' => 'image/x-icon']);
142 142
 			}
143 143
 		}
144
-		if($response === null) {
145
-			$fallbackLogo = \OC::$SERVERROOT . '/core/img/favicon.png';
144
+		if ($response === null) {
145
+			$fallbackLogo = \OC::$SERVERROOT.'/core/img/favicon.png';
146 146
 			$response = new DataDisplayResponse($this->fileAccessHelper->file_get_contents($fallbackLogo), Http::STATUS_OK, ['Content-Type' => 'image/x-icon']);
147 147
 		}
148 148
 		$response->cacheFor(86400);
@@ -167,17 +167,17 @@  discard block
 block discarded – undo
167 167
 		$response = null;
168 168
 		if ($this->themingDefaults->shouldReplaceIcons()) {
169 169
 			try {
170
-				$iconFile = $this->imageManager->getCachedImage('touchIcon-' . $app);
170
+				$iconFile = $this->imageManager->getCachedImage('touchIcon-'.$app);
171 171
 			} catch (NotFoundException $exception) {
172 172
 				$icon = $this->iconBuilder->getTouchIcon($app);
173
-				$iconFile = $this->imageManager->setCachedImage('touchIcon-' . $app, $icon);
173
+				$iconFile = $this->imageManager->setCachedImage('touchIcon-'.$app, $icon);
174 174
 			}
175 175
 			if ($iconFile !== false) {
176 176
 				$response = new FileDisplayResponse($iconFile, Http::STATUS_OK, ['Content-Type' => 'image/png']);
177 177
 			}
178 178
 		}
179
-		if($response === null) {
180
-			$fallbackLogo = \OC::$SERVERROOT . '/core/img/favicon-touch.png';
179
+		if ($response === null) {
180
+			$fallbackLogo = \OC::$SERVERROOT.'/core/img/favicon-touch.png';
181 181
 			$response = new DataDisplayResponse($this->fileAccessHelper->file_get_contents($fallbackLogo), Http::STATUS_OK, ['Content-Type' => 'image/png']);
182 182
 		}
183 183
 		$response->cacheFor(86400);
Please login to merge, or discard this patch.
apps/theming/lib/IconBuilder.php 2 patches
Indentation   +168 added lines, -168 removed lines patch added patch discarded remove patch
@@ -29,173 +29,173 @@
 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
-			$icon = $this->renderAppIcon($app, 32);
58
-			if ($icon === false) {
59
-				return false;
60
-			}
61
-			$icon->setImageFormat("png24");
62
-			$data = $icon->getImageBlob();
63
-			$icon->destroy();
64
-			return $data;
65
-		} catch (\ImagickException $e) {
66
-			return false;
67
-		}
68
-	}
69
-
70
-	/**
71
-	 * @param $app string app name
72
-	 * @return string|false image blob
73
-	 */
74
-	public function getTouchIcon($app) {
75
-		try {
76
-			$icon = $this->renderAppIcon($app, 512);
77
-			if ($icon === false) {
78
-				return false;
79
-			}
80
-			$icon->setImageFormat("png24");
81
-			$data = $icon->getImageBlob();
82
-			$icon->destroy();
83
-			return $data;
84
-		} catch (\ImagickException $e) {
85
-			return false;
86
-		}
87
-	}
88
-
89
-	/**
90
-	 * Render app icon on themed background color
91
-	 * fallback to logo
92
-	 *
93
-	 * @param $app string app name
94
-	 * @param $size int size of the icon in px
95
-	 * @return Imagick|false
96
-	 */
97
-	public function renderAppIcon($app, $size) {
98
-		$appIcon = $this->util->getAppIcon($app);
99
-		if($appIcon === false) {
100
-			return false;
101
-		}
102
-		if ($appIcon instanceof ISimpleFile) {
103
-			$appIconContent = $appIcon->getContent();
104
-			$mime = $appIcon->getMimeType();
105
-		} else {
106
-			$appIconContent = file_get_contents($appIcon);
107
-			$mime = mime_content_type($appIcon);
108
-		}
109
-
110
-		if($appIconContent === false || $appIconContent === "") {
111
-			return false;
112
-		}
113
-
114
-		$color = $this->themingDefaults->getColorPrimary();
115
-
116
-		// generate background image with rounded corners
117
-		$background = '<?xml version="1.0" encoding="UTF-8"?>' .
118
-			'<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">' .
119
-			'<rect x="0" y="0" rx="100" ry="100" width="512" height="512" style="fill:' . $color . ';" />' .
120
-			'</svg>';
121
-		// resize svg magic as this seems broken in Imagemagick
122
-		if($mime === "image/svg+xml" || substr($appIconContent, 0, 4) === "<svg") {
123
-			if(substr($appIconContent, 0, 5) !== "<?xml") {
124
-				$svg = "<?xml version=\"1.0\"?>".$appIconContent;
125
-			} else {
126
-				$svg = $appIconContent;
127
-			}
128
-			$tmp = new Imagick();
129
-			$tmp->readImageBlob($svg);
130
-			$x = $tmp->getImageWidth();
131
-			$y = $tmp->getImageHeight();
132
-			$res = $tmp->getImageResolution();
133
-			$tmp->destroy();
134
-
135
-			if($x>$y) {
136
-				$max = $x;
137
-			} else {
138
-				$max = $y;
139
-			}
140
-
141
-			// convert svg to resized image
142
-			$appIconFile = new Imagick();
143
-			$resX = (int)(512 * $res['x'] / $max * 2.53);
144
-			$resY = (int)(512 * $res['y'] / $max * 2.53);
145
-			$appIconFile->setResolution($resX, $resY);
146
-			$appIconFile->setBackgroundColor(new ImagickPixel('transparent'));
147
-			$appIconFile->readImageBlob($svg);
148
-			$appIconFile->scaleImage(512, 512, true);
149
-		} else {
150
-			$appIconFile = new Imagick();
151
-			$appIconFile->setBackgroundColor(new ImagickPixel('transparent'));
152
-			$appIconFile->readImageBlob($appIconContent);
153
-			$appIconFile->scaleImage(512, 512, true);
154
-		}
155
-		// offset for icon positioning
156
-		$border_w = (int)($appIconFile->getImageWidth() * 0.05);
157
-		$border_h = (int)($appIconFile->getImageHeight() * 0.05);
158
-		$innerWidth = (int)($appIconFile->getImageWidth() - $border_w * 2);
159
-		$innerHeight = (int)($appIconFile->getImageHeight() - $border_h * 2);
160
-		$appIconFile->adaptiveResizeImage($innerWidth, $innerHeight);
161
-		// center icon
162
-		$offset_w = 512 / 2 - $innerWidth / 2;
163
-		$offset_h = 512 / 2 - $innerHeight / 2;
164
-
165
-		$appIconFile->setImageFormat("png24");
166
-
167
-		$finalIconFile = new Imagick();
168
-		$finalIconFile->setBackgroundColor(new ImagickPixel('transparent'));
169
-		$finalIconFile->readImageBlob($background);
170
-		$finalIconFile->setImageVirtualPixelMethod(Imagick::VIRTUALPIXELMETHOD_TRANSPARENT);
171
-		$finalIconFile->setImageArtifact('compose:args', "1,0,-0.5,0.5");
172
-		$finalIconFile->compositeImage($appIconFile, Imagick::COMPOSITE_ATOP, $offset_w, $offset_h);
173
-		$finalIconFile->setImageFormat('png24');
174
-		if (defined("Imagick::INTERPOLATE_BICUBIC") === true) {
175
-			$filter = Imagick::INTERPOLATE_BICUBIC;
176
-		} else {
177
-			$filter = Imagick::FILTER_LANCZOS;
178
-		}
179
-		$finalIconFile->resizeImage($size, $size, $filter, 1, false);
180
-
181
-		$appIconFile->destroy();
182
-		return $finalIconFile;
183
-	}
184
-
185
-	public function colorSvg($app, $image) {
186
-		try {
187
-			$imageFile = $this->util->getAppImage($app, $image);
188
-		} catch (AppPathNotFoundException $e) {
189
-			return false;
190
-		}
191
-		$svg = file_get_contents($imageFile);
192
-		if ($svg !== false && $svg !== "") {
193
-			$color = $this->util->elementColor($this->themingDefaults->getColorPrimary());
194
-			$svg = $this->util->colorizeSvg($svg, $color);
195
-			return $svg;
196
-		} else {
197
-			return false;
198
-		}
199
-	}
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
+            $icon = $this->renderAppIcon($app, 32);
58
+            if ($icon === false) {
59
+                return false;
60
+            }
61
+            $icon->setImageFormat("png24");
62
+            $data = $icon->getImageBlob();
63
+            $icon->destroy();
64
+            return $data;
65
+        } catch (\ImagickException $e) {
66
+            return false;
67
+        }
68
+    }
69
+
70
+    /**
71
+     * @param $app string app name
72
+     * @return string|false image blob
73
+     */
74
+    public function getTouchIcon($app) {
75
+        try {
76
+            $icon = $this->renderAppIcon($app, 512);
77
+            if ($icon === false) {
78
+                return false;
79
+            }
80
+            $icon->setImageFormat("png24");
81
+            $data = $icon->getImageBlob();
82
+            $icon->destroy();
83
+            return $data;
84
+        } catch (\ImagickException $e) {
85
+            return false;
86
+        }
87
+    }
88
+
89
+    /**
90
+     * Render app icon on themed background color
91
+     * fallback to logo
92
+     *
93
+     * @param $app string app name
94
+     * @param $size int size of the icon in px
95
+     * @return Imagick|false
96
+     */
97
+    public function renderAppIcon($app, $size) {
98
+        $appIcon = $this->util->getAppIcon($app);
99
+        if($appIcon === false) {
100
+            return false;
101
+        }
102
+        if ($appIcon instanceof ISimpleFile) {
103
+            $appIconContent = $appIcon->getContent();
104
+            $mime = $appIcon->getMimeType();
105
+        } else {
106
+            $appIconContent = file_get_contents($appIcon);
107
+            $mime = mime_content_type($appIcon);
108
+        }
109
+
110
+        if($appIconContent === false || $appIconContent === "") {
111
+            return false;
112
+        }
113
+
114
+        $color = $this->themingDefaults->getColorPrimary();
115
+
116
+        // generate background image with rounded corners
117
+        $background = '<?xml version="1.0" encoding="UTF-8"?>' .
118
+            '<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">' .
119
+            '<rect x="0" y="0" rx="100" ry="100" width="512" height="512" style="fill:' . $color . ';" />' .
120
+            '</svg>';
121
+        // resize svg magic as this seems broken in Imagemagick
122
+        if($mime === "image/svg+xml" || substr($appIconContent, 0, 4) === "<svg") {
123
+            if(substr($appIconContent, 0, 5) !== "<?xml") {
124
+                $svg = "<?xml version=\"1.0\"?>".$appIconContent;
125
+            } else {
126
+                $svg = $appIconContent;
127
+            }
128
+            $tmp = new Imagick();
129
+            $tmp->readImageBlob($svg);
130
+            $x = $tmp->getImageWidth();
131
+            $y = $tmp->getImageHeight();
132
+            $res = $tmp->getImageResolution();
133
+            $tmp->destroy();
134
+
135
+            if($x>$y) {
136
+                $max = $x;
137
+            } else {
138
+                $max = $y;
139
+            }
140
+
141
+            // convert svg to resized image
142
+            $appIconFile = new Imagick();
143
+            $resX = (int)(512 * $res['x'] / $max * 2.53);
144
+            $resY = (int)(512 * $res['y'] / $max * 2.53);
145
+            $appIconFile->setResolution($resX, $resY);
146
+            $appIconFile->setBackgroundColor(new ImagickPixel('transparent'));
147
+            $appIconFile->readImageBlob($svg);
148
+            $appIconFile->scaleImage(512, 512, true);
149
+        } else {
150
+            $appIconFile = new Imagick();
151
+            $appIconFile->setBackgroundColor(new ImagickPixel('transparent'));
152
+            $appIconFile->readImageBlob($appIconContent);
153
+            $appIconFile->scaleImage(512, 512, true);
154
+        }
155
+        // offset for icon positioning
156
+        $border_w = (int)($appIconFile->getImageWidth() * 0.05);
157
+        $border_h = (int)($appIconFile->getImageHeight() * 0.05);
158
+        $innerWidth = (int)($appIconFile->getImageWidth() - $border_w * 2);
159
+        $innerHeight = (int)($appIconFile->getImageHeight() - $border_h * 2);
160
+        $appIconFile->adaptiveResizeImage($innerWidth, $innerHeight);
161
+        // center icon
162
+        $offset_w = 512 / 2 - $innerWidth / 2;
163
+        $offset_h = 512 / 2 - $innerHeight / 2;
164
+
165
+        $appIconFile->setImageFormat("png24");
166
+
167
+        $finalIconFile = new Imagick();
168
+        $finalIconFile->setBackgroundColor(new ImagickPixel('transparent'));
169
+        $finalIconFile->readImageBlob($background);
170
+        $finalIconFile->setImageVirtualPixelMethod(Imagick::VIRTUALPIXELMETHOD_TRANSPARENT);
171
+        $finalIconFile->setImageArtifact('compose:args', "1,0,-0.5,0.5");
172
+        $finalIconFile->compositeImage($appIconFile, Imagick::COMPOSITE_ATOP, $offset_w, $offset_h);
173
+        $finalIconFile->setImageFormat('png24');
174
+        if (defined("Imagick::INTERPOLATE_BICUBIC") === true) {
175
+            $filter = Imagick::INTERPOLATE_BICUBIC;
176
+        } else {
177
+            $filter = Imagick::FILTER_LANCZOS;
178
+        }
179
+        $finalIconFile->resizeImage($size, $size, $filter, 1, false);
180
+
181
+        $appIconFile->destroy();
182
+        return $finalIconFile;
183
+    }
184
+
185
+    public function colorSvg($app, $image) {
186
+        try {
187
+            $imageFile = $this->util->getAppImage($app, $image);
188
+        } catch (AppPathNotFoundException $e) {
189
+            return false;
190
+        }
191
+        $svg = file_get_contents($imageFile);
192
+        if ($svg !== false && $svg !== "") {
193
+            $color = $this->util->elementColor($this->themingDefaults->getColorPrimary());
194
+            $svg = $this->util->colorizeSvg($svg, $color);
195
+            return $svg;
196
+        } else {
197
+            return false;
198
+        }
199
+    }
200 200
 
201 201
 }
Please login to merge, or discard this patch.
Spacing   +14 added lines, -14 removed lines patch added patch discarded remove patch
@@ -96,7 +96,7 @@  discard block
 block discarded – undo
96 96
 	 */
97 97
 	public function renderAppIcon($app, $size) {
98 98
 		$appIcon = $this->util->getAppIcon($app);
99
-		if($appIcon === false) {
99
+		if ($appIcon === false) {
100 100
 			return false;
101 101
 		}
102 102
 		if ($appIcon instanceof ISimpleFile) {
@@ -107,20 +107,20 @@  discard block
 block discarded – undo
107 107
 			$mime = mime_content_type($appIcon);
108 108
 		}
109 109
 
110
-		if($appIconContent === false || $appIconContent === "") {
110
+		if ($appIconContent === false || $appIconContent === "") {
111 111
 			return false;
112 112
 		}
113 113
 
114 114
 		$color = $this->themingDefaults->getColorPrimary();
115 115
 
116 116
 		// generate background image with rounded corners
117
-		$background = '<?xml version="1.0" encoding="UTF-8"?>' .
118
-			'<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">' .
119
-			'<rect x="0" y="0" rx="100" ry="100" width="512" height="512" style="fill:' . $color . ';" />' .
117
+		$background = '<?xml version="1.0" encoding="UTF-8"?>'.
118
+			'<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">'.
119
+			'<rect x="0" y="0" rx="100" ry="100" width="512" height="512" style="fill:'.$color.';" />'.
120 120
 			'</svg>';
121 121
 		// resize svg magic as this seems broken in Imagemagick
122
-		if($mime === "image/svg+xml" || substr($appIconContent, 0, 4) === "<svg") {
123
-			if(substr($appIconContent, 0, 5) !== "<?xml") {
122
+		if ($mime === "image/svg+xml" || substr($appIconContent, 0, 4) === "<svg") {
123
+			if (substr($appIconContent, 0, 5) !== "<?xml") {
124 124
 				$svg = "<?xml version=\"1.0\"?>".$appIconContent;
125 125
 			} else {
126 126
 				$svg = $appIconContent;
@@ -132,7 +132,7 @@  discard block
 block discarded – undo
132 132
 			$res = $tmp->getImageResolution();
133 133
 			$tmp->destroy();
134 134
 
135
-			if($x>$y) {
135
+			if ($x > $y) {
136 136
 				$max = $x;
137 137
 			} else {
138 138
 				$max = $y;
@@ -140,8 +140,8 @@  discard block
 block discarded – undo
140 140
 
141 141
 			// convert svg to resized image
142 142
 			$appIconFile = new Imagick();
143
-			$resX = (int)(512 * $res['x'] / $max * 2.53);
144
-			$resY = (int)(512 * $res['y'] / $max * 2.53);
143
+			$resX = (int) (512 * $res['x'] / $max * 2.53);
144
+			$resY = (int) (512 * $res['y'] / $max * 2.53);
145 145
 			$appIconFile->setResolution($resX, $resY);
146 146
 			$appIconFile->setBackgroundColor(new ImagickPixel('transparent'));
147 147
 			$appIconFile->readImageBlob($svg);
@@ -153,10 +153,10 @@  discard block
 block discarded – undo
153 153
 			$appIconFile->scaleImage(512, 512, true);
154 154
 		}
155 155
 		// offset for icon positioning
156
-		$border_w = (int)($appIconFile->getImageWidth() * 0.05);
157
-		$border_h = (int)($appIconFile->getImageHeight() * 0.05);
158
-		$innerWidth = (int)($appIconFile->getImageWidth() - $border_w * 2);
159
-		$innerHeight = (int)($appIconFile->getImageHeight() - $border_h * 2);
156
+		$border_w = (int) ($appIconFile->getImageWidth() * 0.05);
157
+		$border_h = (int) ($appIconFile->getImageHeight() * 0.05);
158
+		$innerWidth = (int) ($appIconFile->getImageWidth() - $border_w * 2);
159
+		$innerHeight = (int) ($appIconFile->getImageHeight() - $border_h * 2);
160 160
 		$appIconFile->adaptiveResizeImage($innerWidth, $innerHeight);
161 161
 		// center icon
162 162
 		$offset_w = 512 / 2 - $innerWidth / 2;
Please login to merge, or discard this patch.