Lazy Icon—在线Icon转换
07 Mar 2012去年就有了工具箱,那时候是想做一个网盘MP3外链工具。
因为喜欢音乐,空间里的外链都需要现找,不稳定。
后来也知道了几个破解115或者威盘的方法,最后没做出来,可惜。
不过就有了工具箱这个念头。
第一个工具是利用威盘的api的上传功能。
今天这个是在线转换icon图标。
核心代码是别人封装好的php文件。
001
002 //////////////////////////////////////////////////////////////
003 /// phpThumb() by James Heinrich //
004 // available at http://phpthumb.sourceforge.net ///
005 //////////////////////////////////////////////////////////////
006 /// //
007 // phpthumb.ico.php – .ICO output format functions //
008 // ///
009 //////////////////////////////////////////////////////////////
010 class phpthumb_ico {
011 function phpthumb_ico() {
012 return true;
013 }
014 function GD2ICOstring(&$gd_image_array) {
015 foreach ($gd_image_array as $key => $gd_image) {
016 $ImageWidths[$key] = ImageSX($gd_image);
017 $ImageHeights[$key] = ImageSY($gd_image);
018 $bpp[$key] = ImageIsTrueColor($gd_image) ? 32 : 24;
019 $totalcolors[$key] = ImageColorsTotal($gd_image);
020 $icXOR[$key] = ”;
021 for ($y = $ImageHeights[$key] - 1; $y >= ; $y–) {
022 for ($x = ; $x < $ImageWidths[$key]; $x++) {
023 $argb = $this->GetPixelColor($gd_image, $x, $y);
024 $a = round(255 * ((127 - $argb['alpha']) / 127));
025 $r = $argb['red'];
026 $g = $argb['green'];
027 $b = $argb['blue'];
028 if ($bpp[$key] == 32) {
029 $icXOR[$key] .= chr($b).chr($g).chr($r).chr($a);
030 } elseif ($bpp[$key] == 24) {
031 $icXOR[$key] .= chr($b).chr($g).chr($r);
032 }
033 if ($a < 128) {
034 @$icANDmask[$key][$y] .= ’1′;
035 } else {
036 @$icANDmask[$key][$y] .= ’0′;
037 }
038 }
039 // mask bits are 32-bit aligned per scanline
040 while (strlen($icANDmask[$key][$y]) % 32) {
041 $icANDmask[$key][$y] .= ’0′;
042 }
043 }
044 $icAND[$key] = ”;
045 foreach ($icANDmask[$key] as $y => $scanlinemaskbits) {
046 for ($i = ; $i < strlen($scanlinemaskbits); $i += 8) {
047 $icAND[$key] .= chr(bindec(str_pad(substr($scanlinemaskbits, $i, 8), 8, ’0′, STR_PAD_LEFT)));
048 }
049 }
050 }
051 foreach ($gd_image_array as $key => $gd_image) {
052 $biSizeImage = $ImageWidths[$key] * $ImageHeights[$key] * ($bpp[$key] / 8);
053 // BITMAPINFOHEADER – 40 bytes
054 $BitmapInfoHeader[$key] = ”;
055 $BitmapInfoHeader[$key] .= “\x28\x00\x00\x00“; // DWORD biSize;
056 $BitmapInfoHeader[$key] .= $this->LittleEndian2String($ImageWidths[$key], 4); // LONG biWidth;
057 // The biHeight member specifies the combined
058 // height of the XOR and AND masks.
059 $BitmapInfoHeader[$key] .= $this->LittleEndian2String($ImageHeights[$key] * 2, 4); // LONG biHeight;
060 $BitmapInfoHeader[$key] .= “\x01\x00“; // WORD biPlanes;
061 $BitmapInfoHeader[$key] .= chr($bpp[$key]).“\x00“; // wBitCount;
062 $BitmapInfoHeader[$key] .= “\x00\x00\x00\x00“; // DWORD biCompression;
063 $BitmapInfoHeader[$key] .= $this->LittleEndian2String($biSizeImage, 4); // DWORD biSizeImage;
064 $BitmapInfoHeader[$key] .= “\x00\x00\x00\x00“; // LONG biXPelsPerMeter;
065 $BitmapInfoHeader[$key] .= “\x00\x00\x00\x00“; // LONG biYPelsPerMeter;
066 $BitmapInfoHeader[$key] .= “\x00\x00\x00\x00“; // DWORD biClrUsed;
067 $BitmapInfoHeader[$key] .= “\x00\x00\x00\x00“; // DWORD biClrImportant;
068 }
069 $icondata = “\x00\x00“; // idReserved; // Reserved (must be 0)
070 $icondata .= “\x01\x00“; // idType; // Resource Type (1 for icons)
071 $icondata .= $this->LittleEndian2String(count($gd_image_array), 2); // idCount; // How many images?
072 $dwImageOffset = 6 + (count($gd_image_array) * 16);
073 foreach ($gd_image_array as $key => $gd_image) {
074 // ICONDIRENTRY idEntries[1]; // An entry for each image (idCount of ‘em)
075 $icondata .= chr($ImageWidths[$key]); // bWidth; // Width, in pixels, of the image
076 $icondata .= chr($ImageHeights[$key]); // bHeight; // Height, in pixels, of the image
077 $icondata .= chr($totalcolors[$key]); // bColorCount; // Number of colors in image (0 if >=8bpp)
078 $icondata .= “\x00“; // bReserved; // Reserved ( must be 0)
079 $icondata .= “\x01\x00“; // wPlanes; // Color Planes
080 $icondata .= chr($bpp[$key]).“\x00“; // wBitCount; // Bits per pixel
081 $dwBytesInRes = 40 + strlen($icXOR[$key]) + strlen($icAND[$key]);
082 $icondata .= $this->LittleEndian2String($dwBytesInRes, 4); // dwBytesInRes; // How many bytes in this resource?
083 $icondata .= $this->LittleEndian2String($dwImageOffset, 4); // dwImageOffset; // Where in the file is this image?
084 $dwImageOffset += strlen($BitmapInfoHeader[$key]);
085 $dwImageOffset += strlen($icXOR[$key]);
086 $dwImageOffset += strlen($icAND[$key]);
087 }
088 foreach ($gd_image_array as $key => $gd_image) {
089 $icondata .= $BitmapInfoHeader[$key];
090 $icondata .= $icXOR[$key];
091 $icondata .= $icAND[$key];
092 }
093 return $icondata;
094 }
095 function LittleEndian2String($number, $minbytes=1) {
096 $intstring = ”;
097 while ($number > ) {
098 $intstring = $intstring.chr($number & 255);
099 $number >>= 8;
100 }
101 return str_pad($intstring, $minbytes, “\x00“, STR_PAD_RIGHT);
102 }
103 function GetPixelColor(&$img, $x, $y) {
104 if (!is_resource($img)) {
105 return false;
106 }
107 return @ImageColorsForIndex($img, @ImageColorAt($img, $x, $y));
108 }
109 }
110 ?>
002 //////////////////////////////////////////////////////////////
003 /// phpThumb() by James Heinrich
004 // available at http://phpthumb.sourceforge.net ///
005 //////////////////////////////////////////////////////////////
006 /// //
007 // phpthumb.ico.php – .ICO output format functions //
008 // ///
009 //////////////////////////////////////////////////////////////
010 class phpthumb_ico {
011 function phpthumb_ico() {
012 return true;
013 }
014 function GD2ICOstring(&$gd_image_array) {
015 foreach ($gd_image_array as $key => $gd_image) {
016 $ImageWidths[$key] = ImageSX($gd_image);
017 $ImageHeights[$key] = ImageSY($gd_image);
018 $bpp[$key] = ImageIsTrueColor($gd_image) ? 32 : 24;
019 $totalcolors[$key] = ImageColorsTotal($gd_image);
020 $icXOR[$key] = ”;
021 for ($y = $ImageHeights[$key] - 1; $y >= ; $y–) {
022 for ($x = ; $x < $ImageWidths[$key]; $x++) {
023 $argb = $this->GetPixelColor($gd_image, $x, $y);
024 $a = round(255 * ((127 - $argb['alpha']) / 127));
025 $r = $argb['red'];
026 $g = $argb['green'];
027 $b = $argb['blue'];
028 if ($bpp[$key] == 32) {
029 $icXOR[$key] .= chr($b).chr($g).chr($r).chr($a);
030 } elseif ($bpp[$key] == 24) {
031 $icXOR[$key] .= chr($b).chr($g).chr($r);
032 }
033 if ($a < 128) {
034 @$icANDmask[$key][$y] .= ’1′;
035 } else {
036 @$icANDmask[$key][$y] .= ’0′;
037 }
038 }
039 // mask bits are 32-bit aligned per scanline
040 while (strlen($icANDmask[$key][$y]) % 32) {
041 $icANDmask[$key][$y] .= ’0′;
042 }
043 }
044 $icAND[$key] = ”;
045 foreach ($icANDmask[$key] as $y => $scanlinemaskbits) {
046 for ($i = ; $i < strlen($scanlinemaskbits); $i += 8) {
047 $icAND[$key] .= chr(bindec(str_pad(substr($scanlinemaskbits, $i, 8), 8, ’0′, STR_PAD_LEFT)));
048 }
049 }
050 }
051 foreach ($gd_image_array as $key => $gd_image) {
052 $biSizeImage = $ImageWidths[$key] * $ImageHeights[$key] * ($bpp[$key] / 8);
053 // BITMAPINFOHEADER – 40 bytes
054 $BitmapInfoHeader[$key] = ”;
055 $BitmapInfoHeader[$key] .= “\x28\x00\x00\x00“; // DWORD biSize;
056 $BitmapInfoHeader[$key] .= $this->LittleEndian2String($ImageWidths[$key], 4); // LONG biWidth;
057 // The biHeight member specifies the combined
058 // height of the XOR and AND masks.
059 $BitmapInfoHeader[$key] .= $this->LittleEndian2String($ImageHeights[$key] * 2, 4); // LONG biHeight;
060 $BitmapInfoHeader[$key] .= “\x01\x00“; // WORD biPlanes;
061 $BitmapInfoHeader[$key] .= chr($bpp[$key]).“\x00“; // wBitCount;
062 $BitmapInfoHeader[$key] .= “\x00\x00\x00\x00“; // DWORD biCompression;
063 $BitmapInfoHeader[$key] .= $this->LittleEndian2String($biSizeImage, 4); // DWORD biSizeImage;
064 $BitmapInfoHeader[$key] .= “\x00\x00\x00\x00“; // LONG biXPelsPerMeter;
065 $BitmapInfoHeader[$key] .= “\x00\x00\x00\x00“; // LONG biYPelsPerMeter;
066 $BitmapInfoHeader[$key] .= “\x00\x00\x00\x00“; // DWORD biClrUsed;
067 $BitmapInfoHeader[$key] .= “\x00\x00\x00\x00“; // DWORD biClrImportant;
068 }
069 $icondata = “\x00\x00“; // idReserved; // Reserved (must be 0)
070 $icondata .= “\x01\x00“; // idType; // Resource Type (1 for icons)
071 $icondata .= $this->LittleEndian2String(count($gd_image_array), 2); // idCount; // How many images?
072 $dwImageOffset = 6 + (count($gd_image_array) * 16);
073 foreach ($gd_image_array as $key => $gd_image) {
074 // ICONDIRENTRY idEntries[1]; // An entry for each image (idCount of ‘em)
075 $icondata .= chr($ImageWidths[$key]); // bWidth; // Width, in pixels, of the image
076 $icondata .= chr($ImageHeights[$key]); // bHeight; // Height, in pixels, of the image
077 $icondata .= chr($totalcolors[$key]); // bColorCount; // Number of colors in image (0 if >=8bpp)
078 $icondata .= “\x00“; // bReserved; // Reserved ( must be 0)
079 $icondata .= “\x01\x00“; // wPlanes; // Color Planes
080 $icondata .= chr($bpp[$key]).“\x00“; // wBitCount; // Bits per pixel
081 $dwBytesInRes = 40 + strlen($icXOR[$key]) + strlen($icAND[$key]);
082 $icondata .= $this->LittleEndian2String($dwBytesInRes, 4); // dwBytesInRes; // How many bytes in this resource?
083 $icondata .= $this->LittleEndian2String($dwImageOffset, 4); // dwImageOffset; // Where in the file is this image?
084 $dwImageOffset += strlen($BitmapInfoHeader[$key]);
085 $dwImageOffset += strlen($icXOR[$key]);
086 $dwImageOffset += strlen($icAND[$key]);
087 }
088 foreach ($gd_image_array as $key => $gd_image) {
089 $icondata .= $BitmapInfoHeader[$key];
090 $icondata .= $icXOR[$key];
091 $icondata .= $icAND[$key];
092 }
093 return $icondata;
094 }
095 function LittleEndian2String($number, $minbytes=1) {
096 $intstring = ”;
097 while ($number > ) {
098 $intstring = $intstring.chr($number & 255);
099 $number >>= 8;
100 }
101 return str_pad($intstring, $minbytes, “\x00“, STR_PAD_RIGHT);
102 }
103 function GetPixelColor(&$img, $x, $y) {
104 if (!is_resource($img)) {
105 return false;
106 }
107 return @ImageColorsForIndex($img, @ImageColorAt($img, $x, $y));
108 }
109 }
110 ?>
转载请注明:于哲的博客 » Lazy Icon—在线Icon转换