function getFileHash($basef, $f, $outDir, $relativeDir = null) { global $g_hash; global $g_handledFiles; if ($relativeDir == null) { $relativeDir = dirname($basef); } $f0 = formatPath($relativeDir . "/{$f}"); $f1 = $outDir . "/" . $f0; $f2 = realpath($f1); if ($f2 === false || !array_key_exists($f0, $g_handledFiles)) { handleOne($f0, $outDir, true); } $f2 = realpath($f1); if ($f2 === false) { die("*** fail to find file `{$f}` base on `{$basef}` ({$f2})\n"); } @($hash = $g_hash[$f2]); if ($hash == null) { $hash = sha1_file($f2); $g_hash[$f2] = $hash; // echo("### hash {$f2}\n"); } else { // echo("### reuse hash({$f2})\n"); } return substr($hash, -6); }
// }}} // ====== main //var_dump($options); @($content = file_get_contents($infile)) or die("*** cannot open infile: `{$infile}'\n"); @($fpout = fopen($options["out"], "w")) or die("*** cannot open out file: `{$options['out']}'\n"); chdir(dirname($infile)); // 第一遍扫描,去除注释,扫描图片并以 "{{n}}" 标记,下次扫描时将填上正确内容。 $content = preg_replace_callback('/ \\/\\* (.|\\n)*? \\*\\/\\s* | background.*? url\\(\\s*(.*?)\\s*\\).*?; /x', function ($ms) { if ($ms[1]) { return ""; } global $picArr; $n = count($picArr); handleOne($ms[2]); return '{{' . $n . '}}'; }, $content); // generate sprite: icon.png, icon-16.png, etc. foreach ($inputInfo as $w => $e) { $src = ""; foreach ($e["pics"] as $pic) { $src .= $pic["file"] . " "; } $dst = getSpriteFilename($w); $cmd = "convert {$src} -append {$dst}"; #echo($cmd . "\n\n"); $lines = null; $info = exec($cmd, $lines, $rv); if ($rv != 0) { die("fail to generate sprite: {$info}\n");