IconBuilder.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016 Julius Härtl <jus@bitgrid.net>
  4. *
  5. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  6. * @author Jan-Christoph Borchardt <hey@jancborchardt.net>
  7. * @author Julius Haertl <jus@bitgrid.net>
  8. * @author Julius Härtl <jus@bitgrid.net>
  9. *
  10. * @license GNU AGPL version 3 or any later version
  11. *
  12. * This program is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License as
  14. * published by the Free Software Foundation, either version 3 of the
  15. * License, or (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU Affero General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Affero General Public License
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  24. *
  25. */
  26. namespace OCA\Theming;
  27. use Imagick;
  28. use ImagickPixel;
  29. use OCP\App\AppPathNotFoundException;
  30. use OCP\Files\SimpleFS\ISimpleFile;
  31. class IconBuilder {
  32. /** @var ThemingDefaults */
  33. private $themingDefaults;
  34. /** @var Util */
  35. private $util;
  36. /** @var ImageManager */
  37. private $imageManager;
  38. /**
  39. * IconBuilder constructor.
  40. *
  41. * @param ThemingDefaults $themingDefaults
  42. * @param Util $util
  43. * @param ImageManager $imageManager
  44. */
  45. public function __construct(
  46. ThemingDefaults $themingDefaults,
  47. Util $util,
  48. ImageManager $imageManager
  49. ) {
  50. $this->themingDefaults = $themingDefaults;
  51. $this->util = $util;
  52. $this->imageManager = $imageManager;
  53. }
  54. /**
  55. * @param $app string app name
  56. * @return string|false image blob
  57. */
  58. public function getFavicon($app) {
  59. if (!$this->imageManager->shouldReplaceIcons()) {
  60. return false;
  61. }
  62. try {
  63. $favicon = new Imagick();
  64. $favicon->setFormat("ico");
  65. $icon = $this->renderAppIcon($app, 128);
  66. if ($icon === false) {
  67. return false;
  68. }
  69. $icon->setImageFormat("png32");
  70. $clone = clone $icon;
  71. $clone->scaleImage(16,0);
  72. $favicon->addImage($clone);
  73. $clone = clone $icon;
  74. $clone->scaleImage(32,0);
  75. $favicon->addImage($clone);
  76. $clone = clone $icon;
  77. $clone->scaleImage(64,0);
  78. $favicon->addImage($clone);
  79. $clone = clone $icon;
  80. $clone->scaleImage(128,0);
  81. $favicon->addImage($clone);
  82. $data = $favicon->getImagesBlob();
  83. $favicon->destroy();
  84. $icon->destroy();
  85. $clone->destroy();
  86. return $data;
  87. } catch (\ImagickException $e) {
  88. return false;
  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("png32");
  102. $data = $icon->getImageBlob();
  103. $icon->destroy();
  104. return $data;
  105. } catch (\ImagickException $e) {
  106. return false;
  107. }
  108. }
  109. /**
  110. * Render app icon on themed background color
  111. * fallback to logo
  112. *
  113. * @param $app string app name
  114. * @param $size int size of the icon in px
  115. * @return Imagick|false
  116. */
  117. public function renderAppIcon($app, $size) {
  118. $appIcon = $this->util->getAppIcon($app);
  119. if ($appIcon === false) {
  120. return false;
  121. }
  122. if ($appIcon instanceof ISimpleFile) {
  123. $appIconContent = $appIcon->getContent();
  124. $mime = $appIcon->getMimeType();
  125. } else {
  126. $appIconContent = file_get_contents($appIcon);
  127. $mime = mime_content_type($appIcon);
  128. }
  129. if ($appIconContent === false || $appIconContent === "") {
  130. return false;
  131. }
  132. $color = $this->themingDefaults->getColorPrimary();
  133. // generate background image with rounded corners
  134. $background = '<?xml version="1.0" encoding="UTF-8"?>' .
  135. '<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">' .
  136. '<rect x="0" y="0" rx="100" ry="100" width="512" height="512" style="fill:' . $color . ';" />' .
  137. '</svg>';
  138. // resize svg magic as this seems broken in Imagemagick
  139. if ($mime === "image/svg+xml" || substr($appIconContent, 0, 4) === "<svg") {
  140. if (substr($appIconContent, 0, 5) !== "<?xml") {
  141. $svg = "<?xml version=\"1.0\"?>".$appIconContent;
  142. } else {
  143. $svg = $appIconContent;
  144. }
  145. $tmp = new Imagick();
  146. $tmp->readImageBlob($svg);
  147. $x = $tmp->getImageWidth();
  148. $y = $tmp->getImageHeight();
  149. $res = $tmp->getImageResolution();
  150. $tmp->destroy();
  151. if ($x > $y) {
  152. $max = $x;
  153. } else {
  154. $max = $y;
  155. }
  156. // convert svg to resized image
  157. $appIconFile = new Imagick();
  158. $resX = (int)(512 * $res['x'] / $max * 2.53);
  159. $resY = (int)(512 * $res['y'] / $max * 2.53);
  160. $appIconFile->setResolution($resX, $resY);
  161. $appIconFile->setBackgroundColor(new ImagickPixel('transparent'));
  162. $appIconFile->readImageBlob($svg);
  163. /**
  164. * invert app icons for bright primary colors
  165. * the default nextcloud logo will not be inverted to black
  166. */
  167. if ($this->util->invertTextColor($color)
  168. && !$appIcon instanceof ISimpleFile
  169. && $app !== "core"
  170. ) {
  171. $appIconFile->negateImage(false);
  172. }
  173. $appIconFile->scaleImage(512, 512, true);
  174. } else {
  175. $appIconFile = new Imagick();
  176. $appIconFile->setBackgroundColor(new ImagickPixel('transparent'));
  177. $appIconFile->readImageBlob($appIconContent);
  178. $appIconFile->scaleImage(512, 512, true);
  179. }
  180. // offset for icon positioning
  181. $border_w = (int)($appIconFile->getImageWidth() * 0.05);
  182. $border_h = (int)($appIconFile->getImageHeight() * 0.05);
  183. $innerWidth = (int)($appIconFile->getImageWidth() - $border_w * 2);
  184. $innerHeight = (int)($appIconFile->getImageHeight() - $border_h * 2);
  185. $appIconFile->adaptiveResizeImage($innerWidth, $innerHeight);
  186. // center icon
  187. $offset_w = 512 / 2 - $innerWidth / 2;
  188. $offset_h = 512 / 2 - $innerHeight / 2;
  189. $finalIconFile = new Imagick();
  190. $finalIconFile->setBackgroundColor(new ImagickPixel('transparent'));
  191. $finalIconFile->readImageBlob($background);
  192. $finalIconFile->setImageVirtualPixelMethod(Imagick::VIRTUALPIXELMETHOD_TRANSPARENT);
  193. $finalIconFile->setImageArtifact('compose:args', "1,0,-0.5,0.5");
  194. $finalIconFile->compositeImage($appIconFile, Imagick::COMPOSITE_ATOP, $offset_w, $offset_h);
  195. $finalIconFile->setImageFormat('png24');
  196. if (defined("Imagick::INTERPOLATE_BICUBIC") === true) {
  197. $filter = Imagick::INTERPOLATE_BICUBIC;
  198. } else {
  199. $filter = Imagick::FILTER_LANCZOS;
  200. }
  201. $finalIconFile->resizeImage($size, $size, $filter, 1, false);
  202. $appIconFile->destroy();
  203. return $finalIconFile;
  204. }
  205. public function colorSvg($app, $image) {
  206. try {
  207. $imageFile = $this->util->getAppImage($app, $image);
  208. } catch (AppPathNotFoundException $e) {
  209. return false;
  210. }
  211. $svg = file_get_contents($imageFile);
  212. if ($svg !== false && $svg !== "") {
  213. $color = $this->util->elementColor($this->themingDefaults->getColorPrimary());
  214. $svg = $this->util->colorizeSvg($svg, $color);
  215. return $svg;
  216. } else {
  217. return false;
  218. }
  219. }
  220. }