function imagecreatefromblp($filename, $imgid = 0) { $f = fopen($filename, "rb") or die("Cannot open file " . $filename . "\n"); $filesize = filesize($filename); if ($filesize < 16) { die("File " . $filename . " is too small for a BLP file\n"); } $data = fread($f, $filesize); fclose($f); if (substr($data, 0, 4) != "BLP2") { die("File " . $filename . " has incorrect/unsupported magic bytes\n"); } $header = unpack("Vformat/Ctype/CalphaBits/CalphaType/Cmips/Vwidth/Vheight", substr($data, 4, 16)); $header["mipsOffs"] = unpack("V16", substr($data, 20, 64)); $header["mipsSize"] = unpack("V16", substr($data, 84, 64)); $debugstr = "\nheader = " . print_r($header, true) . "\n"; if ($header["format"] != 1) { die("File " . $filename . " has unsupported format" . $debugstr); } //print_r($debugstr); $offs = $header["mipsOffs"][$imgid + 1]; $size = $header["mipsSize"][$imgid + 1]; while ($imgid > 0) { $header["width"] /= 2; $header["height"] /= 2; $imgid--; } if ($size == 0) { die("File " . $filename . " contains zeroes in a mips table" . $debugstr); } if ($offs + $size > $filesize) { die("File " . $filename . " is corrupted/incomplete" . $debugstr); } if ($header["type"] == 1) { $img = icfb1($header["width"], $header["height"], substr($data, 148, 1024), substr($data, $offs, $size)); } elseif ($header["type"] == 2) { $img = icfb2($header["width"], $header["height"], substr($data, $offs, $size), $header["alphaBits"], $header["alphaType"]); } elseif ($header["type"] == 3) { $img = icfb3($header["width"], $header["height"], substr($data, $offs, $size)); } else { die("File " . $filename . " has unsupported type" . $debugstr); } if ($img) { return $img; } else { die($debugstr); } }
function imagecreatefromblp($fileName, $imgId = 0) { if (!CLISetup::fileExists($fileName)) { CLISetup::log('file ' . $fileName . ' could not be found', CLISetup::LOG_ERROR); return; } $file = fopen($fileName, 'rb'); if (!$file) { CLISetup::log('could not open file ' . $fileName, CLISetup::LOG_ERROR); return; } $fileSize = fileSize($fileName); if ($fileSize < 16) { CLISetup::log('file ' . $fileName . ' is too small for a BLP file', CLISetup::LOG_ERROR); return; } $data = fread($file, $fileSize); fclose($file); // predict replacement patch files // ref: http://www.zezula.net/en/mpq/patchfiles.html if (substr($data, 0x0, 0x4) == "PTCH") { // strip patch header if (substr($data, 0x40, 0x43) == "COPY") { $data = substr($data, 0x44); } else { CLISetup::log('file ' . $fileName . ' is an incremental patch file and cannot be used by this script.', CLISetup::LOG_ERROR); return; } } if (substr($data, 0, 4) != "BLP2") { CLISetup::log('file ' . $fileName . ' has incorrect/unsupported magic bytes', CLISetup::LOG_ERROR); return; } $header = unpack("Vformat/Ctype/CalphaBits/CalphaType/Cmips/Vwidth/Vheight", substr($data, 4, 16)); $header['mipsOffs'] = unpack("V16", substr($data, 20, 64)); $header['mipsSize'] = unpack("V16", substr($data, 84, 64)); $debugStr = ' header = ' . print_r($header, true); if ($header['format'] != 1) { CLISetup::log('file ' . $fileName . ' has unsupported format' . $debugStr, CLISetup::LOG_ERROR); return; } $offs = $header['mipsOffs'][$imgId + 1]; $size = $header['mipsSize'][$imgId + 1]; while ($imgId > 0) { $header['width'] /= 2; $header['height'] /= 2; $imgId--; } if ($size == 0) { CLISetup::log('file ' . $fileName . ' contains zeroes in a mips table' . $debugStr, CLISetup::LOG_ERROR); return; } if ($offs + $size > $fileSize) { CLISetup::log('file ' . $fileName . ' is corrupted/incomplete' . $debugStr, CLISetup::LOG_ERROR); return; } if ($header['type'] == 1) { $img = icfb1($header['width'], $header['height'], substr($data, 148, 1024), substr($data, $offs, $size)); } else { if ($header['type'] == 2) { $img = icfb2($header['width'], $header['height'], substr($data, $offs, $size), $header['alphaBits'], $header['alphaType']); } else { if ($header['type'] == 3) { $img = icfb3($header['width'], $header['height'], substr($data, $offs, $size)); } else { CLISetup::log('file ' . $fileName . ' has unsupported type' . $debugStr, CLISetup::LOG_ERROR); return; } } } return $img; }