Iconv.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. declare (strict_types=1);
  3. namespace library\utils;
  4. // +----------------------------------------------------------------------
  5. // | [ WE CAN DO IT MORE SIMPLE ]
  6. // +----------------------------------------------------------------------
  7. // | Copyright (c) 2018-2020 rights reserved.
  8. // +----------------------------------------------------------------------
  9. // | Author: TABLE ME
  10. // +----------------------------------------------------------------------
  11. // | Date: 2020-09-21 10:16
  12. // +----------------------------------------------------------------------
  13. class Iconv
  14. {
  15. function phpmake_ico()
  16. {
  17. return true;
  18. }
  19. function GDtoICOstr(&$gd_ico_array)
  20. {
  21. foreach ($gd_ico_array as $key => $gd_image) {
  22. $IcoWidths[$key] = ImageSX($gd_image);
  23. $IcoHeights[$key] = ImageSY($gd_image);
  24. $bpp[$key] = ImageIsTrueColor($gd_image) ? 32 : 24;
  25. $totalcolors[$key] = ImageColorsTotal($gd_image);
  26. $icXOR[$key] = '';
  27. for ($y = $IcoHeights[$key] - 1; $y >= 0; $y--) {
  28. for ($x = 0; $x < $IcoWidths[$key]; $x++) {
  29. $argb = $this->gpc($gd_image, $x, $y);
  30. $a = round(255 * ((127 - $argb['alpha']) / 127));
  31. $r = $argb['red'];
  32. $g = $argb['green'];
  33. $b = $argb['blue'];
  34. if ($bpp[$key] == 32) {
  35. $icXOR[$key] .= chr($b) . chr($g) . chr($r) . chr($a);
  36. } elseif ($bpp[$key] == 24) {
  37. $icXOR[$key] .= chr($b) . chr($g) . chr($r);
  38. }
  39. if ($a < 128) {
  40. @$icANDmask[$key][$y] .= '1';
  41. } else {
  42. @$icANDmask[$key][$y] .= '0';
  43. }
  44. }
  45. while (strlen($icANDmask[$key][$y]) % 32) {
  46. $icANDmask[$key][$y] .= '0';
  47. }
  48. }
  49. $icAND[$key] = '';
  50. foreach ($icANDmask[$key] as $y => $scanlinemaskbits) {
  51. for ($i = 0; $i < strlen($scanlinemaskbits); $i += 8) {
  52. $icAND[$key] .= chr(bindec(str_pad(substr($scanlinemaskbits, $i, 8), 8, '0', STR_PAD_LEFT)));
  53. }
  54. }
  55. }
  56. foreach ($gd_ico_array as $key => $gd_image) {
  57. $biSizeImage = $IcoWidths[$key] * $IcoHeights[$key] * ($bpp[$key] / 8);
  58. $bfh[$key] = '';
  59. $bfh[$key] .= "\x28\x00\x00\x00";
  60. $bfh[$key] .= $this->le2s($IcoWidths[$key], 4);
  61. $bfh[$key] .= $this->le2s($IcoHeights[$key] * 2, 4);
  62. $bfh[$key] .= "\x01\x00";
  63. $bfh[$key] .= chr($bpp[$key]) . "\x00";
  64. $bfh[$key] .= "\x00\x00\x00\x00";
  65. $bfh[$key] .= $this->le2s($biSizeImage, 4);
  66. $bfh[$key] .= "\x00\x00\x00\x00";
  67. $bfh[$key] .= "\x00\x00\x00\x00";
  68. $bfh[$key] .= "\x00\x00\x00\x00";
  69. $bfh[$key] .= "\x00\x00\x00\x00";
  70. }
  71. $icondata = "\x00\x00";
  72. $icondata .= "\x01\x00";
  73. $icondata .= $this->le2s(count($gd_ico_array), 2);
  74. $dwImageOffset = 6 + (count($gd_ico_array) * 16);
  75. foreach ($gd_ico_array as $key => $gd_image) {
  76. $icondata .= chr($IcoWidths[$key]);
  77. $icondata .= chr($IcoHeights[$key]);
  78. $icondata .= chr($totalcolors[$key]);
  79. $icondata .= "\x00";
  80. $icondata .= "\x01\x00";
  81. $icondata .= chr($bpp[$key]) . "\x00";
  82. $dwBytesInRes = 40 + strlen($icXOR[$key]) + strlen($icAND[$key]);
  83. $icondata .= $this->le2s($dwBytesInRes, 4);
  84. $icondata .= $this->le2s($dwImageOffset, 4);
  85. $dwImageOffset += strlen($bfh[$key]);
  86. $dwImageOffset += strlen($icXOR[$key]);
  87. $dwImageOffset += strlen($icAND[$key]);
  88. }
  89. foreach ($gd_ico_array as $key => $gd_image) {
  90. $icondata .= $bfh[$key];
  91. $icondata .= $icXOR[$key];
  92. $icondata .= $icAND[$key];
  93. }
  94. return $icondata;
  95. }
  96. function le2s($number, $minbytes = 1)
  97. {
  98. $intstring = '';
  99. while ($number > 0) {
  100. $intstring = $intstring . chr($number & 255);
  101. $number >>= 8;
  102. }
  103. return str_pad($intstring, $minbytes, "\x00", STR_PAD_RIGHT);
  104. }
  105. function gpc(&$img, $x, $y)
  106. {
  107. if (!is_resource($img)) {
  108. return false;
  109. }
  110. return @ImageColorsForIndex($img, @ImageColorAt($img, $x, $y));
  111. }
  112. }