复制代码 代码如下:
<?PHP
$output = “”;
if(isset($_GET[‘action’])&&$_GET[‘action’] == ‘make’){
if(isset($_FILES[‘upimage’][‘tmp_name’]) && $_FILES[‘upimage’][‘tmp_name’] && is_uploaded_file($_FILES[‘upimage’][‘tmp_name’])){
if($_FILES[‘upimage’][‘type’]>210000){
echo “你上传的文件体积超过了限制 最大不能超过200K”;
exit();
}
$fileext = array(“image/pjpeg”,”image/gif”,”image/x-png”);
if(!in_array($_FILES[‘upimage’][‘type’],$fileext)){
echo “你上传的文件格式不正确 仅支持 jpg,gif,png”;
exit();
}
if($im = @imagecreatefrompng($_FILES[‘upimage’][‘tmp_name’]) or $im = @imagecreatefromgif($_FILES[‘upimage’][‘tmp_name’]) or $im = @imagecreatefromjpeg($_FILES[‘upimage’][‘tmp_name’])){
$imginfo = @getimagesize($_FILES[‘upimage’][‘tmp_name’]);
if(!is_array($imginfo)){
echo “图形格式错误!”;
}
switch($_POST[‘size’]){
case 1;
$resize_im = @imagecreatetruecolor(16,16);
$size = 16;
break;
case 2;
$resize_im = @imagecreatetruecolor(32,32);
$size = 32;
break;
case 3;
$resize_im = @imagecreatetruecolor(48,48);
$size = 48;
break;
default;
$resize_im = @imagecreatetruecolor(32,32);
$size = 32;
break;
}
imagecopyresampled($resize_im,$im,0,0,0,0,$size,$size,$imginfo[0],$imginfo[1]);
include “phpthumb.ico.php”;
$icon = new phpthumb_ico();
$gd_image_array = array($resize_im);
$icon_data = $icon->GD2ICOstring($gd_image_array);
$filename = “temp/“.date(“Ymdhis”).rand(1,1000).”.ico”;
if(file_put_contents($filename, $icon_data)){
$output = “生成成功!请点右键->另存为 保存到本地<br><a href=\"".$filename."\" target=\"_blank\">点击下载</a>“;
}

  1. }else{
  2. echo "生成错误请重试!";
  3. }
  4. }
  5. }
  6. ?>
  7. <html>
  8. <head>
  9. <title>ICO图标在线转换</title>
  10. <style>
  11. body{background-color:#fff;color:#000000;font-family:arial;margin:30px;font-size:12px;}
  12. table{border:0}
  13. td{line-height:16px;}
  14. label{cursor:hand;}
  15. </style>
  16. </head>
  17. <body>
  18. <h1>ICO图标在线转换</h1>
  19. <form action="?action=make" method="post" enctype='multipart/form-data'>
  20. <table>
  21. <tr>
  22. <td><b>请上传你要转换的图片</b><br>支持格式 png,jpg,gif</td>
  23. </tr>
  24. <tr>
  25. <td><input type="file" name="upimage" size="30"></td>
  26. </tr>
  27. <tr>
  28. <td>目标尺寸:
  29. <input type="radio" name="size" value="1" id="s1"><label for="s1">16*16</label>
  30. <input type="radio" name="size" value="2" id="s2" checked><label for="s2">32*32</label>
  31. <input type="radio" name="size" value="3" id="s3"><label for="s3">48*48</label>
  32. </td>
  33. </tr>
  34. <tr>
  35. <td align="right"><input type="submit" value="生 成"></td>
  36. </tr>
  37. <?PHP
  38. if($output){
  39. echo "<tr><td><div style=\"border:1px solid #D8D8B2;background-color:#FFFFDD;padding:10px\">".$output."</div></td></tr>";
  40. }
  41. ?>
  42. <tr>
  43. <td><div style="color:#cccccc;font-size:11px;">Powered by <a href="http://veryim.com" style="color:#cccccc">非常爱漫</a> </div></td>
  44. </tr>
  45. </table>
  46. </form>
  47. </body>
  48. </html>
  49. phpthumb.ico.php
  50. 复制代码 代码如下:
  51. <?php
  52. //////////////////////////////////////////////////////////////
  53. /// phpThumb() by James Heinrich <info@silisoftware.com> //
  54. // available at http://phpthumb.sourceforge.net ///
  55. //////////////////////////////////////////////////////////////
  56. /// //
  57. // phpthumb.ico.php - .ICO output format functions //
  58. // ///
  59. //////////////////////////////////////////////////////////////
  60. class phpthumb_ico {
  61. function phpthumb_ico() {
  62. return true;
  63. }
  64. function GD2ICOstring(&$gd_image_array) {
  65. foreach ($gd_image_array as $key => $gd_image) {
  66. $ImageWidths[$key] = ImageSX($gd_image);
  67. $ImageHeights[$key] = ImageSY($gd_image);
  68. $bpp[$key] = ImageIsTrueColor($gd_image) ? 32 : 24;
  69. $totalcolors[$key] = ImageColorsTotal($gd_image);
  70. $icXOR[$key] = '';
  71. for ($y = $ImageHeights[$key] - 1; $y >= 0; $y--) {
  72. for ($x = 0; $x < $ImageWidths[$key]; $x++) {
  73. $argb = $this->GetPixelColor($gd_image, $x, $y);
  74. $a = round(255 * ((127 - $argb['alpha']) / 127));
  75. $r = $argb['red'];
  76. $g = $argb['green'];
  77. $b = $argb['blue'];
  78. if ($bpp[$key] == 32) {
  79. $icXOR[$key] .= chr($b).chr($g).chr($r).chr($a);
  80. } elseif ($bpp[$key] == 24) {
  81. $icXOR[$key] .= chr($b).chr($g).chr($r);
  82. }
  83. if ($a < 128) {
  84. @$icANDmask[$key][$y] .= '1';
  85. } else {
  86. @$icANDmask[$key][$y] .= '0';
  87. }
  88. }
  89. // mask bits are 32-bit aligned per scanline
  90. while (strlen($icANDmask[$key][$y]) % 32) {
  91. $icANDmask[$key][$y] .= '0';
  92. }
  93. }
  94. $icAND[$key] = '';
  95. foreach ($icANDmask[$key] as $y => $scanlinemaskbits) {
  96. for ($i = 0; $i < strlen($scanlinemaskbits); $i += 8) {
  97. $icAND[$key] .= chr(bindec(str_pad(substr($scanlinemaskbits, $i, 8), 8, '0', STR_PAD_LEFT)));
  98. }
  99. }
  100. }
  101. foreach ($gd_image_array as $key => $gd_image) {
  102. $biSizeImage = $ImageWidths[$key] * $ImageHeights[$key] * ($bpp[$key] / 8);
  103. // BITMAPINFOHEADER - 40 bytes
  104. $BitmapInfoHeader[$key] = '';
  105. $BitmapInfoHeader[$key] .= "\x28\x00\x00\x00"; // DWORD biSize;
  106. $BitmapInfoHeader[$key] .= $this->LittleEndian2String($ImageWidths[$key], 4); // LONG biWidth;
  107. // The biHeight member specifies the combined
  108. // height of the XOR and AND masks.
  109. $BitmapInfoHeader[$key] .= $this->LittleEndian2String($ImageHeights[$key] * 2, 4); // LONG biHeight;
  110. $BitmapInfoHeader[$key] .= "\x01\x00"; // WORD biPlanes;
  111. $BitmapInfoHeader[$key] .= chr($bpp[$key])."\x00"; // wBitCount;
  112. $BitmapInfoHeader[$key] .= "\x00\x00\x00\x00"; // DWORD biCompression;
  113. $BitmapInfoHeader[$key] .= $this->LittleEndian2String($biSizeImage, 4); // DWORD biSizeImage;
  114. $BitmapInfoHeader[$key] .= "\x00\x00\x00\x00"; // LONG biXPelsPerMeter;
  115. $BitmapInfoHeader[$key] .= "\x00\x00\x00\x00"; // LONG biYPelsPerMeter;
  116. $BitmapInfoHeader[$key] .= "\x00\x00\x00\x00"; // DWORD biClrUsed;
  117. $BitmapInfoHeader[$key] .= "\x00\x00\x00\x00"; // DWORD biClrImportant;
  118. }
  119. $icondata = "\x00\x00"; // idReserved; // Reserved (must be 0)
  120. $icondata .= "\x01\x00"; // idType; // Resource Type (1 for icons)
  121. $icondata .= $this->LittleEndian2String(count($gd_image_array), 2); // idCount; // How many images?
  122. $dwImageOffset = 6 + (count($gd_image_array) * 16);
  123. foreach ($gd_image_array as $key => $gd_image) {
  124. // ICONDIRENTRY idEntries[1]; // An entry for each image (idCount of 'em)
  125. $icondata .= chr($ImageWidths[$key]); // bWidth; // Width, in pixels, of the image
  126. $icondata .= chr($ImageHeights[$key]); // bHeight; // Height, in pixels, of the image
  127. $icondata .= chr($totalcolors[$key]); // bColorCount; // Number of colors in image (0 if >=8bpp)
  128. $icondata .= "\x00"; // bReserved; // Reserved ( must be 0)
  129. $icondata .= "\x01\x00"; // wPlanes; // Color Planes
  130. $icondata .= chr($bpp[$key])."\x00"; // wBitCount; // Bits per pixel
  131. $dwBytesInRes = 40 + strlen($icXOR[$key]) + strlen($icAND[$key]);
  132. $icondata .= $this->LittleEndian2String($dwBytesInRes, 4); // dwBytesInRes; // How many bytes in this resource?
  133. $icondata .= $this->LittleEndian2String($dwImageOffset, 4); // dwImageOffset; // Where in the file is this image?
  134. $dwImageOffset += strlen($BitmapInfoHeader[$key]);
  135. $dwImageOffset += strlen($icXOR[$key]);
  136. $dwImageOffset += strlen($icAND[$key]);
  137. }
  138. foreach ($gd_image_array as $key => $gd_image) {
  139. $icondata .= $BitmapInfoHeader[$key];
  140. $icondata .= $icXOR[$key];
  141. $icondata .= $icAND[$key];
  142. }
  143. return $icondata;
  144. }
  145. function LittleEndian2String($number, $minbytes=1) {
  146. $intstring = '';
  147. while ($number > 0) {
  148. $intstring = $intstring.chr($number & 255);
  149. $number >>= 8;
  150. }
  151. return str_pad($intstring, $minbytes, "\x00", STR_PAD_RIGHT);
  152. }
  153. function GetPixelColor(&$img, $x, $y) {
  154. if (!is_resource($img)) {
  155. return false;
  156. }
  157. return @ImageColorsForIndex($img, @ImageColorAt($img, $x, $y));
  158. }
  159. }
  160. ?>

更多相关文章

  1. PHP生成RSS文件类实例
  2. PHP变量类型及类型转换
  3. php判断数组元素不为空格实例代码
  4. JavaScript onblur与onfocus事件详解
  5. vue_1
  6. 代码优化-表驱动编程
  7. 入门php变量和常量
  8. 数组循环取模及代码实现计算器
  9. 变量的类型、类型转换、常量

随机推荐

  1. 【懒懒的Tensorflow学习笔记三之搭建简单
  2. 求助!为什么我连python的hello word都写不
  3. 【爬虫初探】新浪微博搜索爬虫实现
  4. KNN(K近邻分类器)Python3实现
  5. Python 浅拷贝和深拷贝使用例子
  6. Python.PyGanme -- 用Python写游戏
  7. Python笔记(十一):多线程
  8. python爬虫 scrapy2_初窥Scrapy
  9. 【Python】【面试必看】Python笔试题
  10. Python_常用的正则表达式处理函数