查看: 240|回复: 0

[PHP代码] PHP转换图片为ico格式源码

发表于 2017-7-17 08:00:04

来自: http://blog.csdn.net/diandianxiyu_geek/article/details/50443364


Class
  1. <?php
  2. namespace App\Libs;
  3. class Iconv {
  4. function phpmake_ico() {
  5. return true;
  6. }
  7. function GDtoICOstr(&$gd_ico_array) {
  8. foreach ($gd_ico_array as $key => $gd_image) {
  9. $IcoWidths[$key] = ImageSX($gd_image);
  10. $IcoHeights[$key] = ImageSY($gd_image);
  11. $bpp[$key] = ImageIsTrueColor($gd_image) ? 32 : 24;
  12. $totalcolors[$key] = ImageColorsTotal($gd_image);
  13. $icXOR[$key] = '';
  14. for ($y = $IcoHeights[$key] - 1; $y >= 0; $y--) {
  15. for ($x = 0; $x < $IcoWidths[$key]; $x++) {
  16. $argb = $this->gpc($gd_image, $x, $y);
  17. $a = round(255 * ((127 - $argb['alpha']) / 127));
  18. $r = $argb['red'];
  19. $g = $argb['green'];
  20. $b = $argb['blue'];
  21. if ($bpp[$key] == 32) {
  22. $icXOR[$key] .= chr($b).chr($g).chr($r).chr($a);
  23. } elseif ($bpp[$key] == 24) {
  24. $icXOR[$key] .= chr($b).chr($g).chr($r);
  25. }
  26. if ($a < 128) {
  27. @$icANDmask[$key][$y] .= '1';
  28. } else {
  29. @$icANDmask[$key][$y] .= '0';
  30. }
  31. }
  32. while (strlen($icANDmask[$key][$y]) % 32) {
  33. $icANDmask[$key][$y] .= '0';
  34. }
  35. }
  36. $icAND[$key] = '';
  37. foreach ($icANDmask[$key] as $y => $scanlinemaskbits) {
  38. for ($i = 0; $i < strlen($scanlinemaskbits); $i += 8) {
  39. $icAND[$key] .= chr(bindec(str_pad(substr($scanlinemaskbits, $i, 8), 8, '0', STR_PAD_LEFT)));
  40. }
  41. }
  42. }
  43. foreach ($gd_ico_array as $key => $gd_image) {
  44. $biSizeImage = $IcoWidths[$key] * $IcoHeights[$key] * ($bpp[$key] / 8);
  45. $bfh[$key] = '';
  46. $bfh[$key] .= "\x28\x00\x00\x00";
  47. $bfh[$key] .= $this->le2s($IcoWidths[$key], 4);
  48. $bfh[$key] .= $this->le2s($IcoHeights[$key] * 2, 4);
  49. $bfh[$key] .= "\x01\x00";
  50. $bfh[$key] .= chr($bpp[$key])."\x00";
  51. $bfh[$key] .= "\x00\x00\x00\x00";
  52. $bfh[$key] .= $this->le2s($biSizeImage, 4);
  53. $bfh[$key] .= "\x00\x00\x00\x00";
  54. $bfh[$key] .= "\x00\x00\x00\x00";
  55. $bfh[$key] .= "\x00\x00\x00\x00";
  56. $bfh[$key] .= "\x00\x00\x00\x00";
  57. }
  58. $icondata = "\x00\x00";
  59. $icondata .= "\x01\x00";
  60. $icondata .= $this->le2s(count($gd_ico_array), 2);
  61. $dwImageOffset = 6 + (count($gd_ico_array) * 16);
  62. foreach ($gd_ico_array as $key => $gd_image) {
  63. $icondata .= chr($IcoWidths[$key]);
  64. $icondata .= chr($IcoHeights[$key]);
  65. $icondata .= chr($totalcolors[$key]);
  66. $icondata .= "\x00";
  67. $icondata .= "\x01\x00";
  68. $icondata .= chr($bpp[$key])."\x00";
  69. $dwBytesInRes = 40 + strlen($icXOR[$key]) + strlen($icAND[$key]);
  70. $icondata .= $this->le2s($dwBytesInRes, 4);
  71. $icondata .= $this->le2s($dwImageOffset, 4);
  72. $dwImageOffset += strlen($bfh[$key]);
  73. $dwImageOffset += strlen($icXOR[$key]);
  74. $dwImageOffset += strlen($icAND[$key]);
  75. }
  76. foreach ($gd_ico_array as $key => $gd_image) {
  77. $icondata .= $bfh[$key];
  78. $icondata .= $icXOR[$key];
  79. $icondata .= $icAND[$key];
  80. }
  81. return $icondata;
  82. }
  83. function le2s($number, $minbytes=1) {
  84. $intstring = '';
  85. while ($number > 0) {
  86. $intstring = $intstring.chr($number & 255);
  87. $number >>= 8;
  88. }
  89. return str_pad($intstring, $minbytes, "\x00", STR_PAD_RIGHT);
  90. }
  91. function gpc(&$img, $x, $y) {
  92. if (!is_resource($img)) {
  93. return false;
  94. }
  95. return @ImageColorsForIndex($img, @ImageColorAt($img, $x, $y));
  96. }
  97. }
  98. ?>
复制代码
Controller
  1. if ( $error['text'] == "" && isset($_FILES['upimage']['tmp_name']) && $_FILES['upimage']['tmp_name'] && is_uploaded_file($_FILES['upimage']['tmp_name'])) {
  2. if ($_FILES['upimage']['type'] > 210000) {
  3. $error['text'] = "你上传的文件体积超过了限制 最大不能超过200k";
  4. } else {
  5. $fileext = array("image/pjpeg", "image/gif", "image/x-png", "image/png", "image/jpeg", "image/jpg");
  6. if (!in_array($_FILES['upimage']['type'], $fileext)) {
  7. $error['text'] = "你上传的文件格式不正确 仅支持 jpg,gif,png";
  8. }else {
  9. if ($im = @imagecreatefrompng($_FILES['upimage']['tmp_name']) or $im = @imagecreatefromgif($_FILES['upimage']['tmp_name']) or $im = @imagecreatefromjpeg($_FILES['upimage']['tmp_name'])) {
  10. $imginfo = @getimagesize($_FILES['upimage']['tmp_name']);
  11. if (!is_array($imginfo)) {
  12. $error['text'] = "图形格式错误!";
  13. }else {
  14. switch ($_POST['size']) {
  15. case 1;
  16. $resize_im = @imagecreatetruecolor(16, 16);
  17. $size = 16;
  18. break;
  19. case 2;
  20. $resize_im = @imagecreatetruecolor(32, 32);
  21. $size = 32;
  22. break;
  23. case 3;
  24. $resize_im = @imagecreatetruecolor(48, 48);
  25. $size = 48;
  26. break;
  27. case 4;
  28. $resize_im = @imagecreatetruecolor(64, 64);
  29. $size = 64;
  30. break;
  31. case 5;
  32. $resize_im = @imagecreatetruecolor(128, 128);
  33. $size = 128;
  34. break;
  35. default;
  36. $resize_im = @imagecreatetruecolor(64, 64);
  37. $size = 64;
  38. break;
  39. }
  40. imagecopyresampled($resize_im, $im, 0, 0, 0, 0, $size, $size, $imginfo[0], $imginfo[1]);
  41. $icon = new Iconv();
  42. $gd_image_array = array($resize_im);
  43. $icon_data = $icon->GDtoICOstr($gd_image_array);
  44. $filename = "temp/" . date("Ymdhis") . rand(1, 1000) . ".ico";
  45. if (file_put_contents($filename, $icon_data)) {
  46. // $output = "生成成功!请点右键->另存为 保存到本地<br><a href="/" mce_href="/""".$filename."/" target=/"_blank/">点击下载</a>";
  47. // echo $filename;
  48. //数据展示
  49. $icon_arr=[
  50. 'class'=>'',
  51. 'time'=>date("Y-m-d H:i:s"),
  52. 'filename'=>$_FILES['upimage']['name'],
  53. 'filepath'=>$filename,
  54. 'size'=>$size
  55. ];
  56. }
  57. }
  58. } else {
  59. $error['text'] = "生成错误请重试";
  60. }
  61. }
  62. }
  63. }else{
  64. $error['text'] = "请选择图片!";
  65. }
复制代码
展示效果

这里写图片描述

源码地址

ico在线转换工具已经集成到了开源项目 https://github.com/diandianxiyu/ApiTesting 中 ,在线demo地址稍后再放出



回复

使用道具 举报