function GD2ICOstring(&$gd_image_array) { foreach ($gd_image_array as $key => $gd_image) { $ImageWidths[$key] = ImageSX($gd_image); $ImageHeights[$key] = ImageSY($gd_image); $bpp[$key] = ImageIsTrueColor($gd_image) ? 32 : 24; $totalcolors[$key] = ImageColorsTotal($gd_image); $icXOR[$key] = ''; for ($y = $ImageHeights[$key] - 1; $y >= 0; $y--) { for ($x = 0; $x < $ImageWidths[$key]; $x++) { $argb = phpthumb_functions::GetPixelColor($gd_image, $x, $y); $a = round(255 * ((127 - $argb['alpha']) / 127)); $r = $argb['red']; $g = $argb['green']; $b = $argb['blue']; if ($bpp[$key] == 32) { $icXOR[$key] .= chr($b) . chr($g) . chr($r) . chr($a); } elseif ($bpp[$key] == 24) { $icXOR[$key] .= chr($b) . chr($g) . chr($r); } if ($a < 128) { @($icANDmask[$key][$y] .= '1'); } else { @($icANDmask[$key][$y] .= '0'); } } // mask bits are 32-bit aligned per scanline while (strlen($icANDmask[$key][$y]) % 32) { $icANDmask[$key][$y] .= '0'; } } $icAND[$key] = ''; foreach ($icANDmask[$key] as $y => $scanlinemaskbits) { for ($i = 0; $i < strlen($scanlinemaskbits); $i += 8) { $icAND[$key] .= chr(bindec(str_pad(substr($scanlinemaskbits, $i, 8), 8, '0', STR_PAD_LEFT))); } } } foreach ($gd_image_array as $key => $gd_image) { $biSizeImage = $ImageWidths[$key] * $ImageHeights[$key] * ($bpp[$key] / 8); // BITMAPINFOHEADER - 40 bytes $BitmapInfoHeader[$key] = ''; $BitmapInfoHeader[$key] .= "("; // DWORD biSize; $BitmapInfoHeader[$key] .= phpthumb_functions::LittleEndian2String($ImageWidths[$key], 4); // LONG biWidth; // The biHeight member specifies the combined // height of the XOR and AND masks. $BitmapInfoHeader[$key] .= phpthumb_functions::LittleEndian2String($ImageHeights[$key] * 2, 4); // LONG biHeight; $BitmapInfoHeader[$key] .= ""; // WORD biPlanes; $BitmapInfoHeader[$key] .= chr($bpp[$key]) . ""; // wBitCount; $BitmapInfoHeader[$key] .= ""; // DWORD biCompression; $BitmapInfoHeader[$key] .= phpthumb_functions::LittleEndian2String($biSizeImage, 4); // DWORD biSizeImage; $BitmapInfoHeader[$key] .= ""; // LONG biXPelsPerMeter; $BitmapInfoHeader[$key] .= ""; // LONG biYPelsPerMeter; $BitmapInfoHeader[$key] .= ""; // DWORD biClrUsed; $BitmapInfoHeader[$key] .= ""; // DWORD biClrImportant; } $icondata = ""; // idReserved; // Reserved (must be 0) $icondata .= ""; // idType; // Resource Type (1 for icons) $icondata .= phpthumb_functions::LittleEndian2String(count($gd_image_array), 2); // idCount; // How many images? $dwImageOffset = 6 + count($gd_image_array) * 16; foreach ($gd_image_array as $key => $gd_image) { // ICONDIRENTRY idEntries[1]; // An entry for each image (idCount of 'em) $icondata .= chr($ImageWidths[$key]); // bWidth; // Width, in pixels, of the image $icondata .= chr($ImageHeights[$key]); // bHeight; // Height, in pixels, of the image $icondata .= chr($totalcolors[$key]); // bColorCount; // Number of colors in image (0 if >=8bpp) $icondata .= ""; // bReserved; // Reserved ( must be 0) $icondata .= ""; // wPlanes; // Color Planes $icondata .= chr($bpp[$key]) . ""; // wBitCount; // Bits per pixel $dwBytesInRes = 40 + strlen($icXOR[$key]) + strlen($icAND[$key]); $icondata .= phpthumb_functions::LittleEndian2String($dwBytesInRes, 4); // dwBytesInRes; // How many bytes in this resource? $icondata .= phpthumb_functions::LittleEndian2String($dwImageOffset, 4); // dwImageOffset; // Where in the file is this image? $dwImageOffset += strlen($BitmapInfoHeader[$key]); $dwImageOffset += strlen($icXOR[$key]); $dwImageOffset += strlen($icAND[$key]); } foreach ($gd_image_array as $key => $gd_image) { $icondata .= $BitmapInfoHeader[$key]; $icondata .= $icXOR[$key]; $icondata .= $icAND[$key]; } return $icondata; }
function GD2BMPstring(&$gd_image) { $imageX = ImageSX($gd_image); $imageY = ImageSY($gd_image); $BMP = ''; for ($y = $imageY - 1; $y >= 0; $y--) { $thisline = ''; for ($x = 0; $x < $imageX; $x++) { $argb = phpthumb_functions::GetPixelColor($gd_image, $x, $y); $thisline .= chr($argb['blue']) . chr($argb['green']) . chr($argb['red']); } while (strlen($thisline) % 4) { $thisline .= ""; } $BMP .= $thisline; } $bmpSize = strlen($BMP) + 14 + 40; // BITMAPFILEHEADER [14 bytes] - http://msdn.microsoft.com/library/en-us/gdi/bitmaps_62uq.asp $BITMAPFILEHEADER = 'BM'; // WORD bfType; $BITMAPFILEHEADER .= phpthumb_functions::LittleEndian2String($bmpSize, 4); // DWORD bfSize; $BITMAPFILEHEADER .= phpthumb_functions::LittleEndian2String(0, 2); // WORD bfReserved1; $BITMAPFILEHEADER .= phpthumb_functions::LittleEndian2String(0, 2); // WORD bfReserved2; $BITMAPFILEHEADER .= phpthumb_functions::LittleEndian2String(54, 4); // DWORD bfOffBits; // BITMAPINFOHEADER - [40 bytes] http://msdn.microsoft.com/library/en-us/gdi/bitmaps_1rw2.asp $BITMAPINFOHEADER = phpthumb_functions::LittleEndian2String(40, 4); // DWORD biSize; $BITMAPINFOHEADER .= phpthumb_functions::LittleEndian2String($imageX, 4); // LONG biWidth; $BITMAPINFOHEADER .= phpthumb_functions::LittleEndian2String($imageY, 4); // LONG biHeight; $BITMAPINFOHEADER .= phpthumb_functions::LittleEndian2String(1, 2); // WORD biPlanes; $BITMAPINFOHEADER .= phpthumb_functions::LittleEndian2String(24, 2); // WORD biBitCount; $BITMAPINFOHEADER .= phpthumb_functions::LittleEndian2String(0, 4); // DWORD biCompression; $BITMAPINFOHEADER .= phpthumb_functions::LittleEndian2String(0, 4); // DWORD biSizeImage; $BITMAPINFOHEADER .= phpthumb_functions::LittleEndian2String(2835, 4); // LONG biXPelsPerMeter; $BITMAPINFOHEADER .= phpthumb_functions::LittleEndian2String(2835, 4); // LONG biYPelsPerMeter; $BITMAPINFOHEADER .= phpthumb_functions::LittleEndian2String(0, 4); // DWORD biClrUsed; $BITMAPINFOHEADER .= phpthumb_functions::LittleEndian2String(0, 4); // DWORD biClrImportant; return $BITMAPFILEHEADER . $BITMAPINFOHEADER . $BMP; }