makeIcons.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. const sharp = require("sharp");
  2. const fs = require("fs");
  3. if (!fs.existsSync("ios_icons")) {
  4. if (fs.mkdirSync("ios_icons")) {
  5. return;
  6. }
  7. }
  8. var iosSizes = [
  9. 20,
  10. 29,
  11. 40,
  12. 50,
  13. 57,
  14. 58,
  15. 60,
  16. 72,
  17. 76,
  18. 80,
  19. 87,
  20. 100,
  21. 114,
  22. 120,
  23. 144,
  24. 152,
  25. 167,
  26. 180,
  27. 1024,
  28. ];
  29. console.log("ios icons:");
  30. iosSizes.forEach((size) => {
  31. console.log(
  32. `<icon height="${size}" src="res/icon/ios/icon-${size}.png" width="${size}" />`
  33. );
  34. sharp("icon.png")
  35. .resize(size)
  36. .toBuffer()
  37. .then((data) => {
  38. var file = fs.open(
  39. `./res/icon/ios/icon-${size}.png`,
  40. "w",
  41. (err, fd) => {
  42. if (err) {
  43. return console.error(err);
  44. }
  45. fs.writeFile(fd, data, (err) => {
  46. if (err) {
  47. return console.error(err);
  48. }
  49. });
  50. }
  51. );
  52. })
  53. .catch((e) => {
  54. console.log(e);
  55. });
  56. });
  57. console.log("android icons:");
  58. var androidSizes = { hdpi: 72, xhdpi: 96, xxhdpi: 144, xxxhdpi: 192 };
  59. Object.keys(androidSizes).forEach((key) => {
  60. let size = androidSizes[key];
  61. console.log(
  62. `<icon src="res/icon/android/icon-${key}.png" density="${key}"/>`
  63. );
  64. const roundedCorners = Buffer.from(
  65. `<svg><rect x="0" y="0" width="${size}" height="${size}" rx="${
  66. size / 5
  67. }" ry="${size / 4}"/></svg>`
  68. );
  69. sharp("icon.png")
  70. .resize(size)
  71. .composite([
  72. {
  73. input: roundedCorners,
  74. blend: "dest-in",
  75. },
  76. ])
  77. .toBuffer()
  78. .then((data) => {
  79. var file = fs.open(
  80. `./res/icon/android/icon-${key}.png`,
  81. "w",
  82. (err, fd) => {
  83. if (err) {
  84. return console.error(err);
  85. }
  86. fs.writeFile(fd, data, (err) => {
  87. if (err) {
  88. return console.error(err);
  89. }
  90. });
  91. }
  92. );
  93. })
  94. .catch((e) => {
  95. console.log(e);
  96. });
  97. });